Skip to content

dart2js generates error when using types as keys in a const map. #23009

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
stereotype441 opened this issue Mar 26, 2015 · 2 comments
Closed

dart2js generates error when using types as keys in a const map. #23009

stereotype441 opened this issue Mar 26, 2015 · 2 comments
Labels
closed-duplicate Closed in favor of an existing report web-dart2js

Comments

@stereotype441
Copy link
Member

The following source code compiles and runs without error on the VM:

import "package:expect/expect.dart";
main() {
  const m = const { dynamic: 1, int: 2 };
  Expect.equals(1, m[dynamic]);
  Expect.equals(2, m[int]);
}

But with dart2js it produces the compile-time error "Const-map key type 'TypeImpl' overrides 'operator =='."

I believe the VM behavior is correct. Since the definition of the class "Type" in the SDK is:

  abstract class Type {}

it is reasonable for the user to expect that they can use Type objects as keys in constant maps.

@lrhn
Copy link
Member

lrhn commented Mar 26, 2015

This is a tricky issue (and not the first time it has cropped up).
The specification for const maps requires the key objects to not override operator==. The objects are clearly not instances of just Type since that class is abstract. They are instances of some other class that implements Type, and it turns out that this type does override operator== in dart2js.
I agree that the user has no chance of knowing this. The solution used for Symbol was to add an abstract operator== to the abstract class to make it clear.

It might be possible to do better with types, though.

We do not promise to canonicalize all Type objects. There isn't necessarily a finite number of different Type objects during a program's life, so that could leak memory like crazy. It may be possible to canonicalize only the types that can occur as literals (pure class types with no generic parameters), and use a different class for types with type parameters since there is at most one such unparameterized type per class in the language. That would mean that type compile-time constant type objects does not need to override operator==.


cc @karlklose.
cc @johnniwinther.

@floitschG
Copy link
Contributor

Added Duplicate label.
Marked as being merged into #17207.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-duplicate Closed in favor of an existing report web-dart2js
Projects
None yet
Development

No branches or pull requests

4 participants