Skip to content
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

Literal symbols are not accepted as const map keys. #15442

Closed
DartBot opened this issue Dec 4, 2013 · 10 comments
Closed

Literal symbols are not accepted as const map keys. #15442

DartBot opened this issue Dec 4, 2013 · 10 comments
Assignees
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
Milestone

Comments

@DartBot
Copy link

DartBot commented Dec 4, 2013

This issue was originally filed by tiagom...@gmail.com


What steps will reproduce the problem?

class A {}
class B {}

main() {
  var map = const <Symbol, int>{#A:0, #B:1};
  print(map);
}

the Editor shows this warning 'Keys in a map cannot be equal'
and running the code produces the error: 'key value must not implement operator =='.

The same behavior occurs with var map = const <Symbol, int>{const Symbol('A'):0, const Symbol('B'):1}

It works removing the const keyword but the DartEditor still shows the warning.

What version of the product are you using? On what operating system?

Editor: 1.0.1_r30657 (2013-11-25)
OS: Mac OS X - x86_64 (10.9)

@peter-ahe-google
Copy link
Contributor

Added Area-Analyzer, Triaged labels.

@peter-ahe-google
Copy link
Contributor

Two bugs here: an analyzer bug, and a VM bug.

I've dedicated this bug to the analyzer and the corresponding VM bug is issue #15445.

@DartBot
Copy link
Author

DartBot commented Dec 4, 2013

This comment was originally written by tiagomz...@gmail.com


Same warning with class literals.

class A {}
class B {}
main() {
  var map = const <Type, int>{A:0, B:1};
  print(map);
}

@bwilkerson
Copy link
Member

Added this to the 1.1 milestone.
Removed Priority-Unassigned label.
Added Priority-Medium label.

@lrhn
Copy link
Member

lrhn commented Dec 6, 2013

Class literals (Type objects) suffer from the same problem as symbols: They override the operator== method of Object, so they are not allowed as const map keys.

They override equals because we didn't want to canonicalize the potentially infinite number of Type objects that a program can create due to risk of memory leaking, but we still want the Type objects to be equal if they represent the same Type.

@yjbanov
Copy link

yjbanov commented Dec 12, 2013

Duplicate? See: #­13245

@bwilkerson
Copy link
Member

https://codereview.chromium.org/113143004/ (bleeding edge revision 31303)


Set owner to @bwilkerson.
Added Fixed label.

@lrhn
Copy link
Member

lrhn commented Feb 23, 2014

If "fixed" means not giving a warning, then it's a bug.
The symbol constant objects still implement operator==, so they are not allowed in a switch case or const map literal. The analyzer doesn't warn, but the code still doesn't run in the VM.

See: https://groups.google.com/a/dartlang.org/forum/?fromgroups#!topic/misc/TPe9-SfDRps


Added Triaged label.

@bwilkerson
Copy link
Member

Removed this from the 1.1 milestone.
Added this to the 1.3 milestone.

@bwilkerson
Copy link
Member

"The static type of a symbol literal is Symbol."
"If d is a class, type alias or type parameter the static type of e is Type."

Neither dart:core::Symbol nor dart:core::Type implements nor inherits an implementation of == other than the one in Object. (They are both abstract classes with no explicit superclass, mixins, or interfaces.)

Therefore, based on the static type information, I don't believe that there is suppose to be an error here. The analyzer cannot be expected to use the run-time type of a literal when it isn't given in the specification. Please let me know if my reasoning is faulty.

(I would almost argue that it's a bug in the VM that the internal Symbol defines ==, but user code can have the same problem too. Perhaps we should define == on dart:core::Symbol in order to make it possible to catch this problem in the analyzer.)


Added Fixed label.

@DartBot DartBot added Type-Defect area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. labels Mar 31, 2014
@DartBot DartBot added this to the 1.3 milestone Mar 31, 2014
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
Projects
None yet
Development

No branches or pull requests

5 participants