Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SI-8546 Pattern matcher analysis foiled by over-widening #3705

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,15 @@ trait TreeAndTypeAnalysis extends Debugging {
object typeArgsToWildcardsExceptArray extends TypeMap {
// SI-6771 dealias would be enough today, but future proofing with the dealiasWiden.
// See neg/t6771b.scala for elaboration
def apply(tp: Type): Type = tp.dealiasWiden match {
def apply(tp: Type): Type = tp.dealias match {
case TypeRef(pre, sym, args) if args.nonEmpty && (sym ne ArrayClass) =>
TypeRef(pre, sym, args map (_ => WildcardType))
case _ =>
mapOver(tp)
}
}
debug.patmatResult(s"checkableType($tp)")(typeArgsToWildcardsExceptArray(tp))
val result = typeArgsToWildcardsExceptArray(tp)
debug.patmatResult(s"checkableType($tp)")(result)
}

// a type is "uncheckable" (for exhaustivity) if we don't statically know its subtypes (i.e., it's unsealed)
Expand Down
2 changes: 0 additions & 2 deletions test/files/neg/t6771b.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// But, to the intrepid hacker who works on this, a few notes:
// You'll have to look into places in the pattern matcher that
// call `dealias`, and see if they need to be `dealiasWiden`.
// For example, if `checkableType` used only `dealias`, `pos/t6671.scala`
// would fail.
object Test {
val a = ""; var b: a.type = a

Expand Down
1 change: 1 addition & 0 deletions test/files/pos/t8546.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Xfatal-warnings
49 changes: 49 additions & 0 deletions test/files/pos/t8546.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package test

class F1() {
private sealed abstract class T
private case class A(m: Int) extends T
private case class B() extends T
private case object C extends T

// No warnings here
private def foo(t: T) = t match {
case A(m) => println("A:" + m)
case B() => println("B")
case C => println("C")
}

def test(m: Int): Unit = {
foo(A(m))
foo(B())
foo(C)
}
}

class F2[M]() {
private sealed abstract class T
private case class A(m: M) extends T
private case class B() extends T
private case object C extends T

// match may not be exhaustive. It would fail on the following input: C
private def foo(t: T) = t match {
case A(m) => println("A:" + m)
case B() => println("B")
case C => println("C")
}

def test(m: M): Unit = {
foo(A(m))
foo(B())
foo(C)
}

}

object Test {
def main(args: Array[String]): Unit = {
new F1().test(1)
new F2[Int]().test(1)
}
}