Skip to content

Commit 4ab5ca6

Browse files
committed
fix: #23261 Distinguish 0.0 and -0.0 in ConstantType match types
- Ensure match types distinguish between 0.0 and -0.0 in pattern matching. - This enables accurate reduction in match types involving Double constants. Close #23261
1 parent a5a7644 commit 4ab5ca6

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
401401
compareErasedValueType
402402
case ConstantType(v2) =>
403403
tp1 match {
404-
case ConstantType(v1) => v1.value == v2.value && recur(v1.tpe, v2.tpe)
404+
case ConstantType(v1) => v1 == v2 && recur(v1.tpe, v2.tpe)
405405
case _ => secondTry
406406
}
407407
case tp2: AnyConstantType =>

tests/neg/i23261.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@main def main(): Unit =
2+
summon[0.0 =:= -0.0] // error: Cannot prove that (0.0: Double) =:= (-0.0: Double).
3+
val d: 0.0 = -0.0 // error: Cannot prove that (0.0: Double) =:= (-0.0: Double).
4+
val d2: -0.0 = 0.0 // error: Cannot prove that (-0.0: Double) =:= (0.0: Double).
5+
summon[0.0f =:= -0.0f] // error: Cannot prove that (0.0f: Float) =:= (-0.0f: Float).
6+
val f: 0.0f = -0.0f // error: Cannot prove that (0.0f: Float) =:= (-0.0f: Float).
7+
val f2: -0.0f = 0.0f // error: Cannot prove that (-0.0f: Float) =:= (0.0f: Float).

tests/pos/i23261.scala

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
type DoubleToString[D <: Double] <: String = D match
2+
case 0.0 => "0.0"
3+
case -0.0 => "-0.0"
4+
case _ => "_"
5+
6+
type DoubleToString2[D <: Double] <: String = D match
7+
case 0.0 => "0.0"
8+
case _ => "_"
9+
10+
type DoubleToString3[D <: Double] <: String = D match
11+
case -0.0 => "-0.0"
12+
case _ => "_"
13+
14+
type FloatToString[F <: Float] <: String = F match
15+
case 0.0f => "0.0f"
16+
case -0.0f => "-0.0f"
17+
case _ => "_"
18+
19+
type FloatToString2[F <: Float] <: String = F match
20+
case 0.0f => "0.0f"
21+
case _ => "_"
22+
23+
type FloatToString3[F <: Float] <: String = F match
24+
case -0.0f => "-0.0f"
25+
case _ => "_"
26+
27+
@main def main(): Unit = {
28+
summon[0.0 =:= 0.0]
29+
summon[-0.0 =:= -0.0]
30+
summon[DoubleToString[0.0] =:= "0.0"]
31+
summon[DoubleToString[-0.0] =:= "-0.0"]
32+
summon[DoubleToString[3.14] =:= "_"]
33+
summon[DoubleToString2[0.0] =:= "0.0"]
34+
summon[DoubleToString2[-0.0] =:= "_"]
35+
summon[DoubleToString3[-0.0] =:= "-0.0"]
36+
summon[DoubleToString3[0.0] =:= "_"]
37+
summon[0.0f =:= 0.0f]
38+
summon[-0.0f =:= -0.0f]
39+
summon[FloatToString[0.0f] =:= "0.0f"]
40+
summon[FloatToString[-0.0f] =:= "-0.0f"]
41+
summon[FloatToString[3.14f] =:= "_"]
42+
summon[FloatToString2[0.0f] =:= "0.0f"]
43+
summon[FloatToString2[-0.0f] =:= "_"]
44+
summon[FloatToString3[-0.0f] =:= "-0.0f"]
45+
summon[FloatToString3[0.0f] =:= "_"]
46+
}

0 commit comments

Comments
 (0)