Skip to content

Commit 6095a12

Browse files
committed
Fix generated testNotNull
1 parent f5c01c1 commit 6095a12

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,12 +1010,17 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
10101010

10111011
/** `tree ne null` (might need a cast to be type correct) */
10121012
def testNotNull(using Context): Tree = {
1013-
val receiver = if (tree.tpe.isBottomType)
1014-
// If the receiver is of type `Nothing` or `Null`, add an ascription so that the selection
1015-
// succeeds: e.g. `null.ne(null)` doesn't type, but `(null: AnyRef).ne(null)` does.
1016-
Typed(tree, TypeTree(defn.AnyRefType))
1017-
else tree.ensureConforms(defn.ObjectType)
1018-
receiver.select(defn.Object_ne).appliedTo(nullLiteral).withSpan(tree.span)
1013+
// If the receiver is of type `Nothing` or `Null`, add an ascription or cast
1014+
// so that the selection succeeds.
1015+
// e.g. `null.ne(null)` doesn't type, but `(null: AnyRef).ne(null)` does.
1016+
val receiver =
1017+
if tree.tpe.isBottomType then
1018+
if ctx.explicitNulls then tree.cast(defn.AnyRefType)
1019+
else Typed(tree, TypeTree(defn.AnyRefType))
1020+
else tree.ensureConforms(defn.ObjectType)
1021+
// also need to cast the null literal to AnyRef in explicit nulls
1022+
val nullLit = if ctx.explicitNulls then nullLiteral.cast(defn.AnyRefType) else nullLiteral
1023+
receiver.select(defn.Object_ne).appliedTo(nullLit).withSpan(tree.span)
10191024
}
10201025

10211026
/** If inititializer tree is `_`, the default value of its type,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// testNotNull can be inserted during PatternMatcher
2+
def f(xs: List[String]) =
3+
xs.zipWithIndex.collect {
4+
case (arg, idx) => idx
5+
}

0 commit comments

Comments
 (0)