@@ -143,7 +143,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
143143 }
144144
145145 /** Is an `CanEqual[cls1, cls2]` instance assumed for predefined classes `cls1`, cls2`? */
146- def canComparePredefinedClasses (cls1 : ClassSymbol , cls2 : ClassSymbol ): Boolean =
146+ def canComparePredefinedClasses (cls1 : ClassSymbol , cls2 : ClassSymbol )( using Context ) : Boolean =
147147
148148 def cmpWithBoxed (cls1 : ClassSymbol , cls2 : ClassSymbol ) =
149149 cls2 == defn.NothingClass
@@ -168,7 +168,8 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
168168 // val x: String = null.asInstanceOf[String]
169169 // if (x == null) {} // error: x is non-nullable
170170 // if (x.asInstanceOf[String|Null] == null) {} // ok
171- cls1 == defn.NullClass && cls1 == cls2
171+ if cls1 == defn.NullClass then cls1 == cls2
172+ else cls1 == defn.NothingClass || cls2 == defn.NothingClass
172173 else if cls1 == defn.NullClass then
173174 cls1 == cls2 || cls2.derivesFrom(defn.ObjectClass )
174175 else if cls2 == defn.NullClass then
@@ -182,9 +183,12 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
182183 * interpret.
183184 */
184185 def canComparePredefined (tp1 : Type , tp2 : Type ) =
186+ val checkCtx = if ctx.explicitNulls
187+ && (tp1.hasAnnotation(defn.CanEqualNullAnnot ) || tp2.hasAnnotation(defn.CanEqualNullAnnot ))
188+ then ctx.retractMode(Mode .SafeNulls ) else ctx
185189 tp1.classSymbols.exists(cls1 =>
186190 tp2.classSymbols.exists(cls2 =>
187- canComparePredefinedClasses(cls1, cls2)))
191+ canComparePredefinedClasses(cls1, cls2)( using checkCtx) ))
188192
189193 formal.argTypes match
190194 case args @ (arg1 :: arg2 :: Nil ) =>
0 commit comments