Skip to content

Commit

Permalink
Merge pull request #3161 from retronym/ticket/7984
Browse files Browse the repository at this point in the history
 Issue unchecked warning for type aliases
  • Loading branch information
retronym committed Nov 27, 2013
2 parents 7cd50ba + 0271a4a commit cb58334
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 14 deletions.
12 changes: 7 additions & 5 deletions src/compiler/scala/tools/nsc/typechecker/Checkable.scala
Expand Up @@ -259,10 +259,12 @@ trait Checkable {
if (uncheckedOk(P0)) return
def where = if (inPattern) "pattern " else ""

// singleton types not considered here
val P = P0.widen
// singleton types not considered here, dealias the pattern for SI-XXXX
val P = P0.dealiasWiden
val X = X0.widen

def PString = if (P eq P0) P.toString else s"$P (the underlying of $P0)"

P match {
// Prohibit top-level type tests for these, but they are ok nested (e.g. case Foldable[Nothing] => ... )
case TypeRef(_, NothingClass | NullClass | AnyValClass, _) =>
Expand All @@ -282,12 +284,12 @@ trait Checkable {

if (checker.neverMatches) {
val addendum = if (checker.neverSubClass) "" else " (but still might match its erasure)"
getContext.unit.warning(tree.pos, s"fruitless type test: a value of type $X cannot also be a $P$addendum")
getContext.unit.warning(tree.pos, s"fruitless type test: a value of type $X cannot also be a $PString$addendum")
}
else if (checker.isUncheckable) {
val msg = (
if (checker.uncheckableType =:= P) s"abstract type $where$P"
else s"${checker.uncheckableMessage} in type $where$P"
if (checker.uncheckableType =:= P) s"abstract type $where$PString"
else s"${checker.uncheckableMessage} in type $where$PString"
)
getContext.unit.warning(tree.pos, s"$msg is unchecked since it is eliminated by erasure")
}
Expand Down
6 changes: 3 additions & 3 deletions test/files/neg/t3692-new.check
@@ -1,10 +1,10 @@
t3692-new.scala:14: warning: non-variable type argument Int in type pattern Map[Int,Int] is unchecked since it is eliminated by erasure
t3692-new.scala:14: warning: non-variable type argument Int in type pattern scala.collection.immutable.Map[Int,Int] (the underlying of Map[Int,Int]) is unchecked since it is eliminated by erasure
case m0: Map[Int, Int] => new java.util.HashMap[Integer, Integer]
^
t3692-new.scala:15: warning: non-variable type argument Int in type pattern Map[Int,V] is unchecked since it is eliminated by erasure
t3692-new.scala:15: warning: non-variable type argument Int in type pattern scala.collection.immutable.Map[Int,V] (the underlying of Map[Int,V]) is unchecked since it is eliminated by erasure
case m1: Map[Int, V] => new java.util.HashMap[Integer, V]
^
t3692-new.scala:16: warning: non-variable type argument Int in type pattern Map[T,Int] is unchecked since it is eliminated by erasure
t3692-new.scala:16: warning: non-variable type argument Int in type pattern scala.collection.immutable.Map[T,Int] (the underlying of Map[T,Int]) is unchecked since it is eliminated by erasure
case m2: Map[T, Int] => new java.util.HashMap[T, Integer]
^
t3692-new.scala:15: warning: unreachable code
Expand Down
6 changes: 6 additions & 0 deletions test/files/neg/t7984.check
@@ -0,0 +1,6 @@
t7984.scala:4: warning: non-variable type argument Int in type pattern List[Int] (the underlying of Test.this.ListInt) is unchecked since it is eliminated by erasure
case is: ListInt => is.head
^
error: No warnings can be incurred under -Xfatal-warnings.
one warning found
one error found
1 change: 1 addition & 0 deletions test/files/neg/t7984.flags
@@ -0,0 +1 @@
-Xfatal-warnings
7 changes: 7 additions & 0 deletions test/files/neg/t7984.scala
@@ -0,0 +1,7 @@
class Test {
type ListInt = List[Int]
List[Any]("") match {
case is: ListInt => is.head
case _ =>
}
}
4 changes: 2 additions & 2 deletions test/files/neg/unchecked-suppress.check
@@ -1,7 +1,7 @@
unchecked-suppress.scala:4: warning: non-variable type argument Int in type pattern Set[Int] is unchecked since it is eliminated by erasure
unchecked-suppress.scala:4: warning: non-variable type argument Int in type pattern scala.collection.immutable.Set[Int] (the underlying of Set[Int]) is unchecked since it is eliminated by erasure
case xs: Set[Int] => xs.head // unchecked
^
unchecked-suppress.scala:5: warning: non-variable type argument String in type pattern Map[String @unchecked,String] is unchecked since it is eliminated by erasure
unchecked-suppress.scala:5: warning: non-variable type argument String in type pattern scala.collection.immutable.Map[String @unchecked,String] (the underlying of Map[String @unchecked,String]) is unchecked since it is eliminated by erasure
case xs: Map[String @unchecked, String] => xs.head // one unchecked, one okay
^
unchecked-suppress.scala:7: warning: non-variable type argument Int in type pattern (Int, Int) => Int is unchecked since it is eliminated by erasure
Expand Down
6 changes: 3 additions & 3 deletions test/files/neg/unchecked.check
@@ -1,10 +1,10 @@
unchecked.scala:18: warning: non-variable type argument String in type pattern Iterable[String] is unchecked since it is eliminated by erasure
unchecked.scala:18: warning: non-variable type argument String in type pattern Iterable[String] (the underlying of Iterable[String]) is unchecked since it is eliminated by erasure
case xs: Iterable[String] => xs.head // unchecked
^
unchecked.scala:22: warning: non-variable type argument Any in type pattern Set[Any] is unchecked since it is eliminated by erasure
unchecked.scala:22: warning: non-variable type argument Any in type pattern scala.collection.immutable.Set[Any] (the underlying of Set[Any]) is unchecked since it is eliminated by erasure
case xs: Set[Any] => xs.head // unchecked
^
unchecked.scala:26: warning: non-variable type argument Any in type pattern Map[Any,Any] is unchecked since it is eliminated by erasure
unchecked.scala:26: warning: non-variable type argument Any in type pattern scala.collection.immutable.Map[Any,Any] (the underlying of Map[Any,Any]) is unchecked since it is eliminated by erasure
case xs: Map[Any, Any] => xs.head // unchecked
^
unchecked.scala:35: warning: non-variable type argument List[Nothing] in type pattern Test.Contra[List[Nothing]] is unchecked since it is eliminated by erasure
Expand Down
2 changes: 1 addition & 1 deletion test/files/neg/unchecked3.check
Expand Up @@ -31,7 +31,7 @@ unchecked3.scala:62: warning: non-variable type argument Array[String] in type p
unchecked3.scala:63: warning: non-variable type argument String in type pattern Array[Array[List[String]]] is unchecked since it is eliminated by erasure
/* warn */ case _: Array[Array[List[String]]] => ()
^
unchecked3.scala:75: warning: abstract type A in type pattern Set[Q.this.A] is unchecked since it is eliminated by erasure
unchecked3.scala:75: warning: abstract type A in type pattern scala.collection.immutable.Set[Q.this.A] (the underlying of Set[Q.this.A]) is unchecked since it is eliminated by erasure
/* warn */ case xs: Set[A] => xs.head
^
unchecked3.scala:62: warning: unreachable code
Expand Down

0 comments on commit cb58334

Please sign in to comment.