Skip to content

fix: #23261 Distinguish 0.0 and -0.0 in ConstantType match types #23265

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -401,7 +401,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)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MEMO

I tried to modify the Constants.scala below to change the processing of the == method, but it didn't work.

override def equals(other: Any): Boolean = other match {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line should use equals, which is already correct, see equalHashValue comment.

case ConstantType(v1) => v1.value.equals(v2.value) && recur(v1.tpe, v2.tpe)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, second look says

v1 == v2 && recur(v1.tpe, v2.tpe)

Do not unwrap Constant to underlying value, use equals on Constant.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The internal tag in Constant is already checked for equality. Then recur(v1.tpe, v2.tpe) is not needed.

However, what about ClazzTag where Constant wraps a Type?

In general, the equals check is wrong. But if the underlying type is always some normalized class type, and it's comparing invariant Class[C], then the existing v1.value == v2.value is correct (and v1 == v2 is also correct).

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] =:= "_"]
}
Loading