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

Fix #5067: handle scrutinee of bottom type in pattern matcher #5075

Merged
merged 2 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions compiler/src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -889,9 +889,15 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
else Erasure.Boxing.adaptToType(tree, tp)

/** `tree ne null` (might need a cast to be type correct) */
def testNotNull(implicit ctx: Context): Tree =
tree.ensureConforms(defn.ObjectType)
.select(defn.Object_ne).appliedTo(Literal(Constant(null)))
def testNotNull(implicit ctx: Context): Tree = {
val receiver = if (defn.isBottomType(tree.tpe)) {
// If the receiver is of type `Nothing` or `Null`, add an ascription so that the selection
// succeeds: e.g. `null.ne(null)` doesn't type, but `(null: AnyRef).ne(null)` does.
Typed(tree, TypeTree(defn.AnyRefType))
}
else tree.ensureConforms(defn.ObjectType)
receiver.select(defn.Object_ne).appliedTo(Literal(Constant(null)))
}

/** If inititializer tree is `_', the default value of its type,
* otherwise the tree itself.
Expand Down
14 changes: 10 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,16 @@ object PatternMatcher {
patternPlan(casted, pat, onSuccess)
})
case UnApply(extractor, implicits, args) =>
val mt @ MethodType(_) = extractor.tpe.widen
var unapp = extractor.appliedTo(ref(scrutinee).ensureConforms(mt.paramInfos.head))
if (implicits.nonEmpty) unapp = unapp.appliedToArgs(implicits)
val unappPlan = unapplyPlan(unapp, args)
val unappPlan = if (defn.isBottomType(scrutinee.info)) {
// Generate a throwaway but type-correct plan.
// This plan will never execute because it'll be guarded by a `NonNullTest`.
ResultPlan(tpd.Throw(tpd.Literal(Constant(null))))
} else {
val mt @ MethodType(_) = extractor.tpe.widen
var unapp = extractor.appliedTo(ref(scrutinee).ensureConforms(mt.paramInfos.head))
if (implicits.nonEmpty) unapp = unapp.appliedToArgs(implicits)
unapplyPlan(unapp, args)
}
if (scrutinee.info.isNotNull || nonNull(scrutinee)) unappPlan
else TestPlan(NonNullTest, scrutinee, tree.span, unappPlan)
case Bind(name, body) =>
Expand Down
6 changes: 6 additions & 0 deletions tests/run/i5067.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
matches null literal
matches null literal
not implemented
match error
not implemented
match error
47 changes: 47 additions & 0 deletions tests/run/i5067.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Test that we correctly handle scrutinees with type `Null` or `Nothing`.
object Test {
def main(args: Array[String]): Unit = {
null match {
case Some(_) => println("matches Some")
case (_, _) => println("matches Pair")
case null => println("matches null literal")
}

type X = Null
(null: X) match {
case Some(_) => println("matches Some")
case (_, _) => println("matches Pair")
case null => println("matches null literal")
}

type Y = Nothing
try {
(??? : Y) match {
case Some(_) => println("matches Some")
case _ => println("matches anything")
}
} catch {
case e: NotImplementedError => println("not implemented")
}


try {
val Some(_) = null
} catch {
case e: MatchError => println("match error")
}

try {
val Some(_) = ???
} catch {
case e: NotImplementedError => println("not implemented")
}

val x: X = null
try {
val Some(_) = x
} catch {
case e: MatchError => println("match error")
}
}
}
3 changes: 3 additions & 0 deletions tests/run/i5067b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
match error
match error nested
not implemented error
34 changes: 34 additions & 0 deletions tests/run/i5067b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Test that we correctly handle scrutinees with type `Null` or `Nothing`.
object Test {
def main(args: Array[String]): Unit = {
class B[T] {}
object B {
def unapply[T](x: Any): Option[B[T]] = None
}
try {
val B(_) = null
} catch {
case e: MatchError => println("match error")
}

null match {
case null =>
try {
null match {
case Some(_) => ()
}
} catch {
case e: MatchError => println("match error nested")
}
}

try {
??? match {
case (_, _) => ()
case _ => ()
}
} catch {
case e: NotImplementedError => println("not implemented error")
}
}
}