Skip to content

Backport "fix: #23261 Distinguish 0.0 and -0.0 in ConstantType match types" to 3.3 LTS #499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
compareErasedValueType
case ConstantType(v2) =>
tp1 match {
case ConstantType(v1) => v1.value == v2.value && recur(v1.tpe, v2.tpe)
case ConstantType(v1) => v1 == v2 && recur(v1.tpe, v2.tpe)
case _ => secondTry
}
case tp2: AnyConstantType =>
Expand Down
7 changes: 7 additions & 0 deletions tests/neg/i23261.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@main def main(): Unit =
summon[0.0 =:= -0.0] // error: Cannot prove that (0.0: Double) =:= (-0.0: Double).
val d: 0.0 = -0.0 // error: Cannot prove that (0.0: Double) =:= (-0.0: Double).
val d2: -0.0 = 0.0 // error: Cannot prove that (-0.0: Double) =:= (0.0: Double).
summon[0.0f =:= -0.0f] // error: Cannot prove that (0.0f: Float) =:= (-0.0f: Float).
val f: 0.0f = -0.0f // error: Cannot prove that (0.0f: Float) =:= (-0.0f: Float).
val f2: -0.0f = 0.0f // error: Cannot prove that (-0.0f: Float) =:= (0.0f: Float).
46 changes: 46 additions & 0 deletions tests/pos/i23261.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
type DoubleToString[D <: Double] <: String = D match
case 0.0 => "0.0"
case -0.0 => "-0.0"
case _ => "_"

type DoubleToString2[D <: Double] <: String = D match
case 0.0 => "0.0"
case _ => "_"

type DoubleToString3[D <: Double] <: String = D match
case -0.0 => "-0.0"
case _ => "_"

type FloatToString[F <: Float] <: String = F match
case 0.0f => "0.0f"
case -0.0f => "-0.0f"
case _ => "_"

type FloatToString2[F <: Float] <: String = F match
case 0.0f => "0.0f"
case _ => "_"

type FloatToString3[F <: Float] <: String = F match
case -0.0f => "-0.0f"
case _ => "_"

@main def main(): Unit = {
summon[0.0 =:= 0.0]
summon[-0.0 =:= -0.0]
summon[DoubleToString[0.0] =:= "0.0"]
summon[DoubleToString[-0.0] =:= "-0.0"]
summon[DoubleToString[3.14] =:= "_"]
summon[DoubleToString2[0.0] =:= "0.0"]
summon[DoubleToString2[-0.0] =:= "_"]
summon[DoubleToString3[-0.0] =:= "-0.0"]
summon[DoubleToString3[0.0] =:= "_"]
summon[0.0f =:= 0.0f]
summon[-0.0f =:= -0.0f]
summon[FloatToString[0.0f] =:= "0.0f"]
summon[FloatToString[-0.0f] =:= "-0.0f"]
summon[FloatToString[3.14f] =:= "_"]
summon[FloatToString2[0.0f] =:= "0.0f"]
summon[FloatToString2[-0.0f] =:= "_"]
summon[FloatToString3[-0.0f] =:= "-0.0f"]
summon[FloatToString3[0.0f] =:= "_"]
}