Skip to content

Commit

Permalink
Merge pull request #13828 from deniszo/fix-13739_and_13512
Browse files Browse the repository at this point in the history
Handles Nothing when synthesizing CanEqual
  • Loading branch information
smarter authored Oct 27, 2021
2 parents 0eb7e61 + 3023d3a commit 2a49471
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Synthesizer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
def canComparePredefinedClasses(cls1: ClassSymbol, cls2: ClassSymbol): Boolean =

def cmpWithBoxed(cls1: ClassSymbol, cls2: ClassSymbol) =
cls2 == defn.boxedType(cls1.typeRef).symbol
cls2 == defn.NothingClass
|| cls2 == defn.boxedType(cls1.typeRef).symbol
|| cls1.isNumericValueClass && cls2.derivesFrom(defn.BoxedNumberClass)

if cls1.isPrimitiveValueClass then
Expand All @@ -129,7 +130,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
else if cls2 == defn.NullClass then
cls1.derivesFrom(defn.ObjectClass)
else
false
cls1 == defn.NothingClass || cls2 == defn.NothingClass
end canComparePredefinedClasses

/** Some simulated `CanEqual` instances for predefined types. It's more efficient
Expand Down
7 changes: 7 additions & 0 deletions tests/pos/i13512.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.language.strictEquality

class NotEquatable

def f = List(new NotEquatable) match
case Nil => ???
case _ =>
46 changes: 46 additions & 0 deletions tests/pos/i13739.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import scala.language.strictEquality

class Foo(i: Int) extends AnyVal

val _ = summon[CanEqual[Nothing, Nothing]]

val _ = summon[CanEqual[Int, Nothing]]
val _ = summon[CanEqual[Nothing, Int]]
val _ = summon[CanEqual[3, Nothing]]
val _ = summon[CanEqual[Nothing, 3]]

val _ = summon[CanEqual[Byte, Nothing]]
val _ = summon[CanEqual[Nothing, Byte]]
val _ = summon[CanEqual[Short, Nothing]]
val _ = summon[CanEqual[Nothing, Short]]
val _ = summon[CanEqual[Float, Nothing]]
val _ = summon[CanEqual[Nothing, Float]]

val _ = summon[CanEqual[Double, Nothing]]
val _ = summon[CanEqual[Nothing, Double]]
val _ = summon[CanEqual[3.0, Nothing]]
val _ = summon[CanEqual[Nothing, 3.0]]

val _ = summon[CanEqual[String, Nothing]]
val _ = summon[CanEqual[Nothing, String]]
val _ = summon[CanEqual["foo", Nothing]]
val _ = summon[CanEqual[Nothing, "foo"]]

val _ = summon[CanEqual[Char, Nothing]]
val _ = summon[CanEqual[Nothing, Char]]
val _ = summon[CanEqual['f', Nothing]]
val _ = summon[CanEqual[Nothing, 'f']]

val _ = summon[CanEqual[Boolean, Nothing]]
val _ = summon[CanEqual[Nothing, Boolean]]
val _ = summon[CanEqual[true, Nothing]]
val _ = summon[CanEqual[Nothing, true]]

val _ = summon[CanEqual[Foo, Nothing]]
val _ = summon[CanEqual[Nothing, Foo]]

val _ = summon[CanEqual[Option[Int], None.type]]
val _ = summon[CanEqual[Option[Int], Option[Nothing]]]

val _ = summon[CanEqual[Any & Nothing, Foo]]
val _ = summon[CanEqual[Nothing & Any, Foo]]

0 comments on commit 2a49471

Please sign in to comment.