Skip to content

Commit 961b9ef

Browse files
committed
Fix comparisons for primitive types
Need reflexive instances so that eqAny does not kick in.
1 parent 613ff30 commit 961b9ef

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

compiler/src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ trait Implicits { self: Typer =>
656656
if (!ctx.isAfterTyper && !assumedCanEqual(ltp, rtp)) {
657657
val res = inferImplicitArg(
658658
defn.EqType.appliedTo(ltp, rtp), msgFun => ctx.error(msgFun(""), pos), pos)
659-
implicits.println(i"Eq witness found: $res: ${res.tpe}")
659+
implicits.println(i"Eq witness found for $ltp / $rtp: $res: ${res.tpe}")
660660
}
661661

662662
/** Find an implicit parameter or conversion.

library/src/dotty/DottyPredef.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ object DottyPredef {
1616

1717
implicit def eqNumber : Eq[Number, Number] = Eq
1818
implicit def eqString : Eq[String, String] = Eq
19+
implicit def eqBoolean : Eq[Boolean, Boolean] = Eq
20+
implicit def eqByte : Eq[Byte, Byte] = Eq
21+
implicit def eqShort : Eq[Short, Short] = Eq
22+
implicit def eqChar : Eq[Char, Char] = Eq
23+
implicit def eqInt : Eq[Int, Int] = Eq
24+
implicit def eqLong : Eq[Long, Long] = Eq
25+
implicit def eqFloat : Eq[Float, Float] = Eq
26+
implicit def eqDouble : Eq[Double, Double] = Eq
27+
implicit def eqUnit : Eq[Unit, Unit] = Eq
1928

2029
// true asymmetry, modeling the (somewhat problematic) nature of equals on Proxies
2130
implicit def eqProxy : Eq[Proxy, Any] = Eq

tests/neg/equality2.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
object equality2 {
2+
class A
3+
4+
val x: Int = 3
5+
x == new A // error: cannot compare
6+
new A == x // error: cannot compare
7+
1 == new A // error: cannot compare
8+
new A == 1 // error: cannot compare
9+
10+
true == new A // error: cannot compare
11+
new A == true // error: cannot compare
12+
}

0 commit comments

Comments
 (0)