Skip to content

Commit bf2fd57

Browse files
committed
Address review comments
Flip some conditions. Remove isSubtypeOfBottom. Add new tests.
1 parent 2007ad2 commit bf2fd57

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -890,12 +890,12 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
890890

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

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,11 +1004,6 @@ class Definitions {
10041004
def isBottomType(tp: Type): Boolean =
10051005
tp.derivesFrom(NothingClass) || tp.derivesFrom(NullClass)
10061006

1007-
/** Is `tp` a subtype of `Nothing` or `Null`? Unlike `isBottomType`, this uses subtyping instead of inheritance. */
1008-
def isSubtypeOfBottom(tp: Type): Boolean = {
1009-
tp.frozen_<:<(NothingType) || tp.frozen_<:<(NullType)
1010-
}
1011-
10121007
/** Is a function class.
10131008
* - FunctionXXL
10141009
* - FunctionN for N >= 0

compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,15 +335,15 @@ object PatternMatcher {
335335
patternPlan(casted, pat, onSuccess)
336336
})
337337
case UnApply(extractor, implicits, args) =>
338-
val unappPlan = if (!defn.isSubtypeOfBottom(scrutinee.info)) {
338+
val unappPlan = if (defn.isBottomType(scrutinee.info)) {
339+
// Generate a throwaway but type-correct plan.
340+
// This plan will never execute because it'll be guarded by a `NonNullTest`.
341+
ResultPlan(tpd.Throw(tpd.Literal(Constant(null))))
342+
} else {
339343
val mt @ MethodType(_) = extractor.tpe.widen
340344
var unapp = extractor.appliedTo(ref(scrutinee).ensureConforms(mt.paramInfos.head))
341345
if (implicits.nonEmpty) unapp = unapp.appliedToArgs(implicits)
342346
unapplyPlan(unapp, args)
343-
} else {
344-
// Generate a throwaway but type-correct plan.
345-
// This plan will never execute because it'll be guarded by a `NonNullTest`.
346-
ResultPlan(tpd.Throw(tpd.Literal(Constant(null))))
347347
}
348348
if (scrutinee.info.isNotNull || nonNull(scrutinee)) unappPlan
349349
else TestPlan(NonNullTest, scrutinee, tree.span, unappPlan)

tests/run/i5067.check

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
matches null literal
22
matches null literal
33
not implemented
4+
match error
5+
not implemented
6+
match error

tests/run/i5067.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,31 @@ object Test {
1717
type Y = Nothing
1818
try {
1919
(??? : Y) match {
20+
case Some(_) => println("matches Some")
2021
case _ => println("matches anything")
2122
}
2223
} catch {
2324
case e: NotImplementedError => println("not implemented")
2425
}
26+
27+
28+
try {
29+
val Some(_) = null
30+
} catch {
31+
case e: MatchError => println("match error")
32+
}
33+
34+
try {
35+
val Some(_) = ???
36+
} catch {
37+
case e: NotImplementedError => println("not implemented")
38+
}
39+
40+
val x: X = null
41+
try {
42+
val Some(_) = x
43+
} catch {
44+
case e: MatchError => println("match error")
45+
}
2546
}
2647
}

0 commit comments

Comments
 (0)