Closed
Description
The spec doesn't allow const values that implement ==
to be used as keys in const maps, or as cases on switch statements.
The CFE is currently not producing a compile-time error for it.
Here is an example:
class A {
final x;
const A(this.x);
operator==(y) => x < y.x;
}
const one = const A(1);
const two = const A(2);
const map = const {one: 1, two: 2};
main() {
print(map[one]);
print(map[two]);
print(one == two);
}
Then:
> dart a.dart
'/a.dart': error: line 11 pos 20: key value must not implement operator ==
const map = const {one: 1, two: 2};
^
> dart --preview-dart-2 a.dart
2
null
true
I wanted to file a bug for this issue because it accidentally hides a problem in dart2js, that Type
cannot be used in these const contexts (see #17207), the sooner we handle this, the lower the chance we'll make a breaking change later on.