From 9482449322cd374f783d4ffa28d1a3a32f9b8f90 Mon Sep 17 00:00:00 2001 From: noti0na1 Date: Wed, 28 Aug 2024 16:37:05 +0200 Subject: [PATCH] Fix canComparePredefined(Nothing, T) in explicit nulls [Cherry-picked 2f9f371d5bd5a136eec134d1a2242121efb31c81] --- .../src/dotty/tools/dotc/typer/Synthesizer.scala | 3 ++- tests/explicit-nulls/pos/i21392.scala | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 tests/explicit-nulls/pos/i21392.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Synthesizer.scala b/compiler/src/dotty/tools/dotc/typer/Synthesizer.scala index f19f97e24c92..dca270f683c9 100644 --- a/compiler/src/dotty/tools/dotc/typer/Synthesizer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Synthesizer.scala @@ -186,7 +186,8 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context): // val x: String = null.asInstanceOf[String] // if (x == null) {} // error: x is non-nullable // if (x.asInstanceOf[String|Null] == null) {} // ok - cls1 == defn.NullClass && cls1 == cls2 + if cls1 == defn.NullClass || cls2 == defn.NullClass then cls1 == cls2 + else cls1 == defn.NothingClass || cls2 == defn.NothingClass else if cls1 == defn.NullClass then cls1 == cls2 || cls2.derivesFrom(defn.ObjectClass) else if cls2 == defn.NullClass then diff --git a/tests/explicit-nulls/pos/i21392.scala b/tests/explicit-nulls/pos/i21392.scala new file mode 100644 index 000000000000..0266199b7831 --- /dev/null +++ b/tests/explicit-nulls/pos/i21392.scala @@ -0,0 +1,16 @@ +//> using options -language:strictEquality + +import scala.collection.LinearSeq + +def foo[T](a: LinearSeq[T]) = a match + case Nil => -1 + case head +: tail => head + +enum Foo derives CanEqual: + case Bar + case Baz(x: String) + + +def foo(a: Foo) = a match + case Foo.Bar => -1 + case _ => 0 \ No newline at end of file