Skip to content

dartanalyzer does not warn/error when using symbols in case expressions #21580

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
mkustermann opened this issue Nov 12, 2014 · 4 comments
Closed
Labels
type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js

Comments

@mkustermann
Copy link
Member

The analyzer differes in behaviour from dart2js/VM. It's a compile-time error for dart2js/VM.

$ cat test.dart
main() {
  switch(#abc) {
    case #abc:
      print('abc');
      break;
    case #xyz:
      print('xyz');
      break;
  }
}

$ dartanalyzer test.dart
Analyzing [test.dart]...
No issues found

$ dart2js test.dart
test.dart:3:10:
Error: 'case' expression type 'Symbol' overrides 'operator =='.
    case #abc:
         ^^^^
Error: Compilation failed.

$ dart test.dart
'file:///usr/local/google/home/kustermann/repositories/gcloud/test.dart': error: line 3 pos 11: type class of case expression must not implement operator ==
    case #abc:
          ^

Slightly related to Issue #15445.

@lrhn
Copy link
Member

lrhn commented Nov 12, 2014

In the prior discussions (Issue #15445 and Issue #15455) the conclusion was that the spec should allow symbols in constant maps and switch cases. The spec was changed for constant maps, but not for switch cases.

Neither the VM nor dart2js have implemented the const Map change, and both still reject symbols as both const map keys and switch case expressions.

The analyzer allows both.


cc @gbracha.

@bwilkerson
Copy link
Member

We should also create a VM version of this issue.


Removed Area-Analyzer label.
Added Area-Dart2JS label.

@gbracha
Copy link
Contributor

gbracha commented Nov 12, 2014

The spec was changed for both.

@kevmoo kevmoo added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) and removed triaged labels Mar 1, 2016
@srawlins
Copy link
Member

@gbracha is correct:

The switch statement supports dispatching control among a large number of cases.

It is a compile-time error if the values of the expressions e_k are not either [...]
instances of the same class C, for all k in 1 .. n, or [...]

It is a compile-time error if the class C has an implementation of the
operator == other than the one inherited from Object unless the value
of the expression is a string, an integer, literal symbol or the result of
invoking a constant constructor of class Symbol.

As well, the VM, dart2js, CFE, and analyzer all now agree (use dartpad for dart2js):

$ cat 21580.dart 
main() {
  switch(#abc) {
    case #abc:
      print('abc');
      break;
    case #xyz:
      print('xyz');
      break;
  }
}
$ dart --version
Dart VM version: 2.0.0-dev.63.0 (Fri Jun 15 00:42:43 2018 +0200) on "macos_x64"
$ dart 21580.dart 
abc
$ dart --preview-dart-2 21580.dart 
abc
$ dartanalyzer 21580.dart 
Analyzing 21580.dart...
No issues found!
$ dartanalyzer --preview-dart-2 21580.dart 
Analyzing 21580.dart...
No issues found!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js
Projects
None yet
Development

No branches or pull requests

6 participants