Skip to content

fix #23261 Handle ConstantType comparison for floating-point 0.0 with sign sensitivity #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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kijuky
Copy link

@kijuky kijuky commented May 25, 2025

Fixes #23261

… with sign sensitivity

- Ensure match types distinguish between 0.0 and -0.0 in pattern matching.
- This enables accurate reduction in match types involving Double constants.
@@ -401,7 +401,13 @@ 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.

case ConstantType(v1) =>
(v1.value, v2.value) match {
// To distinguish 0.0 and -0.0, floating-point numbers use the equals method of their boxing type.
case (d1: Double, d2: Double) => java.lang.Double.valueOf(d1).equals(java.lang.Double.valueOf(d2)) && 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

The equals method of a wrapper type is useful for distinguishing between 0.0 and -0.0.

https://ja.wikipedia.org/wiki/IEEE_754%E3%81%AB%E3%81%8A%E3%81%91%E3%82%8B%E8%B2%A0%E3%81%AE%E3%82%BC%E3%83%AD#%E6%AF%94%E8%BC%83

@@ -0,0 +1,7 @@
@main def main(): Unit =
summon[0.0 =:= -0.0]
Copy link
Contributor

Choose a reason for hiding this comment

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

summon[0.0 =:= -0.0] // error
to annotate an expected error on that line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Match types do not treat 0.0d and -0.0d as equivalent constants
2 participants