[analyzer] Fix comparison of doubles in constant evaluation.

Reland of https://dart-review.googlesource.com/c/sdk/+/96840

Bug: https://github.com/dart-lang/sdk/issues/36188
Change-Id: Iebfde151f098cb2352ddb5cb9368861b313fed4e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/323230
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Kallen Tu 2023-08-31 19:35:01 +00:00
parent 039640fad1
commit 74794c81b2
3 changed files with 23 additions and 3 deletions

View file

@ -1020,7 +1020,7 @@ class DoubleState extends NumState {
@override
bool operator ==(Object other) =>
other is DoubleState && (value == other.value);
other is DoubleState && identical(value, other.value);
@override
NumState add(InstanceState rightOperand) {
@ -1156,13 +1156,13 @@ class DoubleState extends NumState {
if (rightValue == null) {
return BoolState.UNKNOWN_VALUE;
}
return BoolState.from(value == rightValue);
return BoolState.from(identical(value, rightValue));
} else if (rightOperand is IntState) {
var rightValue = rightOperand.value;
if (rightValue == null) {
return BoolState.UNKNOWN_VALUE;
}
return BoolState.from(value == rightValue.toDouble());
return BoolState.from(identical(value, rightValue.toDouble()));
}
return BoolState.FALSE_STATE;
}

View file

@ -1870,6 +1870,22 @@ Record(int, String, {bool c})
''');
}
test_visitSetOrMapLiteral_double_zeros() async {
await assertNoErrorsInCode(r'''
class C {
final double x;
const C(this.x);
}
const cp0 = C(0.0);
const cm0 = C(-0.0);
main(){
print(const{cp0, cm0});
}
''');
}
test_visitSetOrMapLiteral_map_forElement() async {
await assertErrorsInCode(r'''
const x = {1: null, for (final i in const []) i: null};

View file

@ -366,6 +366,10 @@ class DartObjectImplTest {
_assertEqualEqual(_boolValue(false), _doubleValue(2.0), _doubleValue(4.0));
}
void test_equalEqual_double_false_zeros() {
_assertEqualEqual(_boolValue(false), _doubleValue(0.0), _doubleValue(-0.0));
}
void test_equalEqual_double_true() {
_assertEqualEqual(_boolValue(true), _doubleValue(2.0), _doubleValue(2.0));
}