Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db98f29

Browse files
committed
Dealias nestly for checkability of array
1 parent c7f0f2b commit db98f29

5 files changed

Lines changed: 33 additions & 5 deletions

File tree

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,24 @@ trait Checkable {
312312
if (P0.typeSymbol == SingletonClass)
313313
context.warning(tree.pos, s"fruitless type test: every non-null value will be a Singleton dynamically", WarningCategory.Other)
314314
else {
315+
def deepDealias(tp: Type): Type =
316+
tp.dealiasWiden match {
317+
case TypeRef(pre, ArrayClass, args) => TypeRef(pre, ArrayClass, args.map(deepDealias))
318+
case tp => tp
319+
}
315320
// singleton types not considered here, dealias the pattern
316-
val P = P0.dealiasWiden
321+
val P = deepDealias(P0)
317322
val X = X0.widen
318323

319-
def PString = if (P eq P0) P.toString else s"$P (the underlying of $P0)"
324+
def PString = {
325+
val pstr = P.toString
326+
if (P eq P0) pstr
327+
else {
328+
val ostr = P0.toString
329+
if (pstr == ostr) pstr
330+
else s"$pstr (the underlying of $ostr)"
331+
}
332+
}
320333

321334
P match {
322335
// Prohibit top-level type tests for these, but they are ok nested (e.g. case Foldable[Nothing] => ... )

test/files/neg/t11843.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ t11843.scala:11: error: value $asInstanceOf is not a member of Symbol
1414
did you mean asInstanceOf? or perhaps isInstanceOf?
1515
ss.$asInstanceOf[String]
1616
^
17-
t11843.scala:8: warning: fruitless type test: a value of type Symbol cannot also be a String (the underlying of String)
17+
t11843.scala:8: warning: fruitless type test: a value of type Symbol cannot also be a String
1818
ss.isInstanceOf[String]
1919
^
2020
1 warning

test/files/neg/t12408.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ t12408.scala:6: warning: abstract type pattern B is unchecked since it is elimin
44
t12408.scala:7: warning: abstract type B in type pattern t12408.Renderer[B] is unchecked since it is eliminated by erasure
55
def f2[B] = a match { case _: Renderer[B] => } // warn
66
^
7-
t12408.scala:8: warning: non-variable type argument Int in type pattern List[Int] (the underlying of List[Int]) is unchecked since it is eliminated by erasure
7+
t12408.scala:8: warning: non-variable type argument Int in type pattern List[Int] is unchecked since it is eliminated by erasure
88
def f3[B](xs: List[A]) = xs match { case _: List[Int] => } // warn
99
^
1010
t12408.scala:9: warning: abstract type A in type pattern t12408.Renderer[A] is unchecked since it is eliminated by erasure

test/files/neg/unchecked.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
unchecked.scala:20: warning: non-variable type argument String in type pattern Iterable[String] (the underlying of Iterable[String]) is unchecked since it is eliminated by erasure
1+
unchecked.scala:20: warning: non-variable type argument String in type pattern Iterable[String] is unchecked since it is eliminated by erasure
22
case xs: Iterable[String] => xs.head // unchecked
33
^
44
unchecked.scala:24: 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

test/files/pos/t13167.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//> using options -Werror
2+
3+
object Test extends App {
4+
type A[X] = scala.Array[X]
5+
def test(x: Any) =
6+
x match {
7+
case _: A[A[Int]] => println("int") // nowarn
8+
case _: A[A[String]] => println("str")
9+
case _: A[A[AnyRef]] => println("yes")
10+
case _: Array[String] => println("arr")
11+
case _ => println("no")
12+
}
13+
test(new A[A[String]](0))
14+
test(new A[String](0))
15+
}

0 commit comments

Comments
 (0)