@@ -1010,12 +1010,17 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
1010
1010
1011
1011
/** `tree ne null` (might need a cast to be type correct) */
1012
1012
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)
1019
1024
}
1020
1025
1021
1026
/** If inititializer tree is `_`, the default value of its type,
0 commit comments