Skip to content

Commit 4e7bf2f

Browse files
author
Adriaan Moors
committed
Merge pull request scala#973 from paulp/topic/unchecked-hk
Improve unchecked warnings.
2 parents 078a1c5 + 186f57a commit 4e7bf2f

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

src/compiler/scala/tools/nsc/typechecker/Infer.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,8 +1278,10 @@ trait Infer {
12781278
} else {
12791279
for (arg <- args) {
12801280
if (sym == ArrayClass) check(arg, bound)
1281-
else if (arg.typeArgs.nonEmpty) () // avoid spurious warnings with higher-kinded types
1282-
else if (sym == NonLocalReturnControlClass) () // no way to suppress unchecked warnings on try/catch
1281+
// avoid spurious warnings with higher-kinded types
1282+
else if (arg.typeArgs exists (_.typeSymbol.isTypeParameterOrSkolem)) ()
1283+
// no way to suppress unchecked warnings on try/catch
1284+
else if (sym == NonLocalReturnControlClass) ()
12831285
else arg match {
12841286
case TypeRef(_, sym, _) if isLocalBinding(sym) =>
12851287
;
@@ -1423,7 +1425,7 @@ trait Infer {
14231425
)
14241426

14251427
// Intentionally *not* using `Type#typeSymbol` here, which would normalize `tp`
1426-
// and collect symbols from the result type of any resulting `PolyType`s, which
1428+
// and collect symbols from the result type of any resulting `PolyType`s, which
14271429
// are not free type parameters of `tp`.
14281430
//
14291431
// Contrast with `isFreeTypeParamNoSkolem`.
@@ -1456,7 +1458,7 @@ trait Infer {
14561458
def inferExprAlternative(tree: Tree, pt: Type) = tree.tpe match {
14571459
case OverloadedType(pre, alts) => tryTwice { isSecondTry =>
14581460
val alts0 = alts filter (alt => isWeaklyCompatible(pre.memberType(alt), pt))
1459-
val noAlternatives = alts0.isEmpty
1461+
val noAlternatives = alts0.isEmpty
14601462
val alts1 = if (noAlternatives) alts else alts0
14611463

14621464
//println("trying "+alts1+(alts1 map (_.tpe))+(alts1 map (_.locationString))+" for "+pt)
@@ -1614,7 +1616,7 @@ trait Infer {
16141616
val saved = context.state
16151617
var fallback = false
16161618
context.setBufferErrors()
1617-
// We cache the current buffer because it is impossible to
1619+
// We cache the current buffer because it is impossible to
16181620
// distinguish errors that occurred before entering tryTwice
16191621
// and our first attempt in 'withImplicitsDisabled'. If the
16201622
// first attempt fails we try with implicits on *and* clean

test/files/neg/unchecked2.check

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
unchecked2.scala:2: error: non variable type-argument Int in type Option[Int] is unchecked since it is eliminated by erasure
2+
Some(123).isInstanceOf[Option[Int]]
3+
^
4+
unchecked2.scala:3: error: non variable type-argument String in type Option[String] is unchecked since it is eliminated by erasure
5+
Some(123).isInstanceOf[Option[String]]
6+
^
7+
unchecked2.scala:4: error: non variable type-argument List[String] in type Option[List[String]] is unchecked since it is eliminated by erasure
8+
Some(123).isInstanceOf[Option[List[String]]]
9+
^
10+
unchecked2.scala:5: error: non variable type-argument List[Int => String] in type Option[List[Int => String]] is unchecked since it is eliminated by erasure
11+
Some(123).isInstanceOf[Option[List[Int => String]]]
12+
^
13+
unchecked2.scala:6: error: non variable type-argument (String, Double) in type Option[(String, Double)] is unchecked since it is eliminated by erasure
14+
Some(123).isInstanceOf[Option[(String, Double)]]
15+
^
16+
unchecked2.scala:7: error: non variable type-argument String => Double in type Option[String => Double] is unchecked since it is eliminated by erasure
17+
Some(123).isInstanceOf[Option[String => Double]]
18+
^
19+
6 errors found

test/files/neg/unchecked2.flags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-unchecked -Xfatal-warnings

test/files/neg/unchecked2.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object Test {
2+
Some(123).isInstanceOf[Option[Int]]
3+
Some(123).isInstanceOf[Option[String]]
4+
Some(123).isInstanceOf[Option[List[String]]]
5+
Some(123).isInstanceOf[Option[List[Int => String]]]
6+
Some(123).isInstanceOf[Option[(String, Double)]]
7+
Some(123).isInstanceOf[Option[String => Double]]
8+
}

test/files/pos/t1439.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
class View[C[A]] { }
33

44
object Test {
5-
null match {
5+
(null: Any) match {
66
case v: View[_] =>
77
}
88
}

0 commit comments

Comments
 (0)