Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `Some(123).isInstanceOf[Option[List[String]]]` *not* give an unchecked warning?

When using .isInstanceOf[GenericType[SomeOtherType]], where GenericType and SomeOtherType are arbitrary types (of suitable kind), the Scala compiler gives an unchecked warning due to type erasure:

scala> Some(123).isInstanceOf[Option[Int]]
<console>:8: warning: non variable type-argument Int in type Option[Int] is unchecked since it is eliminated by erasure
              Some(123).isInstanceOf[Option[Int]]
                                    ^
res0: Boolean = true

scala> Some(123).isInstanceOf[Option[String]]
<console>:8: warning: non variable type-argument String in type Option[String] is unchecked since it is eliminated by erasure
              Some(123).isInstanceOf[Option[String]]
                                    ^
res1: Boolean = true

However, if SomeOtherType is itself a generic type (e.g. List[String]), no warning is emitted:

scala> Some(123).isInstanceOf[Option[List[String]]]
res2: Boolean = true

scala> Some(123).isInstanceOf[Option[Option[Int]]]
res3: Boolean = true

scala> Some(123).isInstanceOf[Option[List[Int => String]]]
res4: Boolean = true

scala> Some(123).isInstanceOf[Option[(String, Double)]]
res5: Boolean = true

scala> Some(123).isInstanceOf[Option[String => Double]]
res6: Boolean = true

(recall that tuples and => are syntactic sugar for Tuple2[] and Function2[] generic types)

Why is no warning emitted? (All these are in the Scala REPL 2.9.1, with the -unchecked option.)

like image 479
Mechanical snail Avatar asked Jul 19 '12 05:07

Mechanical snail


1 Answers

I had a look to the Scala compiler sources, and I discovered something interesting looking at

scala.tools.nsc.typechecker.Infer

which is where you find the warning. If you look carefully at line 1399 to:

def checkCheckable(pos: Position, tp: Type, kind: String)

which is where the the the warning are generated, you see some nested methods including the check method:

 def check(tp: Type, bound: List[Symbol]) {
        def isLocalBinding(sym: Symbol) =
          sym.isAbstractType &&
          ((bound contains sym) ||
           sym.name == tpnme.WILDCARD || {
            val e = context.scope.lookupEntry(sym.name)
            (e ne null) && e.sym == sym && !e.sym.isTypeParameterOrSkolem && e.owner == context.scope
          })
        tp match {
          case SingleType(pre, _) =>
            check(pre, bound)
          case TypeRef(pre, sym, args) =>
            if (sym.isAbstractType) {
              if (!isLocalBinding(sym)) patternWarning(tp, "abstract type ")
            } else if (sym.isAliasType) {
              check(tp.normalize, bound)
            } else if (sym == NothingClass || sym == NullClass || sym == AnyValClass) {
              error(pos, "type "+tp+" cannot be used in a type pattern or isInstanceOf test")
            } else {
              for (arg <- args) {
                if (sym == ArrayClass) check(arg, bound)
                else if (arg.typeArgs.nonEmpty) ()   // avoid spurious warnings with higher-kinded types
                else arg match {
                  case TypeRef(_, sym, _) if isLocalBinding(sym) =>
                    ;
                  case _ =>
                    patternWarning(arg, "non variable type-argument ")
                }
              }
            }
            check(pre, bound)
          case RefinedType(parents, decls) =>
            if (decls.isEmpty) for (p <- parents) check(p, bound)
            else patternWarning(tp, "refinement ")
          case ExistentialType(quantified, tp1) =>
            check(tp1, bound ::: quantified)
          case ThisType(_) =>
            ;
          case NoPrefix =>
            ;
          case _ =>
            patternWarning(tp, "type ")
        }
  }

While I am not expert in the Scala compiler, we should all thanks the guys to making the code so self-explanatory. Let's look inside the tp match block and the treated cases:

  • If its a single type
  • If it is a type ref
    • If is abstract type
    • If is an alias type
    • If its Null, Nothing, or Anyval
    • All other cases

If you look to all other cases, there is a line which is also commented:

else if (arg.typeArgs.nonEmpty) ()   // avoid spurious warnings with higher-kinded types

That tells you exactly what happen if your type has other type parameter (as Function2, or Tuple2). The check function returns unit without performing any test.

I do not for which reason this has been done this way, but you might want to open a bug at https://issues.scala-lang.org/browse/SI providing the code you posted here as an excellent test case, and reference to the Infer.scala source which I copied above.

like image 124
Edmondo1984 Avatar answered Jan 21 '23 01:01

Edmondo1984