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

Analyzer fails to report an error when key in a constant map overrides operator == #42356

Closed
spkersten opened this issue Mar 6, 2020 · 8 comments
Assignees
Labels
analyzer-spec Issues with the analyzer's implementation of the language spec area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@spkersten
Copy link
Contributor

In the following code:

void main() {
  final foo1 = Bar({const Foo(): ""});  // 1
  final foo2 = const Bar({Foo(): ""});  // 2
}

class Foo {
  const Foo();
  
  bool operator==(dynamic other) => false;
  
  int get hashCode => 1;
}

class Bar {
  const Bar(Map<Foo, String> x);
}

I get a warning for the foo1:

info: Prefer const with constant constructors. (prefer_const_constructors ...

But, with const at foo2, there is an error:

Error compiling to JavaScript:
main.dart:4:22:
Error: Constant evaluation error:
final foo2 = const Bar({Foo(): ""});
^
main.dart:4:27:
Info: The key 'Foo {}' does not have a primitive operator '=='.

  • 'Foo' is from 'main.dart'.
    final foo2 = const Bar({Foo(): ""});
    ^
    Error: Compilation failed.

Dart 2.7.0

@bwilkerson
Copy link
Member

It appears that the analyzer has failed to provide a required error at foo2. The specification says

It is a compile-time error if the operator ‘==’ of a key in a constant map literal is not primitive (10.1.3).

The analyzer should have produced this error, but didn't.

But the code that produces the prefer_const_constructors lint correctly suppresses that lint if the constructor invocation is implicitly const, which it is in the case of the Foo in foo2.

@spkersten
Copy link
Contributor Author

spkersten commented Mar 19, 2020

A very similar issue:

class Foo {
  const Foo(List<int> numbers)
      : assert(numbers.length > 0),
        assert(numbers.isNotEmpty);
}

The first assert results in a warning say:

info: Use isNotEmpty instead of length (prefer_is_empty at [...] ...)

But following that suggestion (the second assert) is an error:

error: Invalid constant value. (invalid_constant at [...] ...)

@srawlins
Copy link
Member

@spkersten That issue is different enough to warrant a new issue, if you'd like to open it.

For the record, neither of those assertions are legal. dart will report each. The analyzer is failing to report the first one.

@spkersten
Copy link
Contributor Author

@srawlins Created dart-lang/sdk#42354

@mraleph mraleph transferred this issue from dart-lang/sdk Jun 16, 2020
@bwilkerson
Copy link
Member

@mraleph This is actually a bug in analyzer, not in the linter. Could you please move it back to the sdk repo?

@mraleph mraleph transferred this issue from dart-lang/linter Jun 16, 2020
@mraleph mraleph added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Jun 16, 2020
@bwilkerson
Copy link
Member

Thanks!

@scheglov Looks like analyzer is failing to report an error.

@bwilkerson bwilkerson added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Jun 16, 2020
@srawlins srawlins added the analyzer-spec Issues with the analyzer's implementation of the language spec label Jun 16, 2020
@scheglov scheglov self-assigned this Jun 16, 2020
@mraleph mraleph changed the title Incorrect prefer_const_constructors warning Analyzer fails to report an error when key in constant map overrides operator == Jun 17, 2020
@mraleph mraleph changed the title Analyzer fails to report an error when key in constant map overrides operator == Analyzer fails to report an error when key in a constant map overrides operator == Jun 17, 2020
@mraleph
Copy link
Member

mraleph commented Jun 17, 2020

@bwilkerson I took a liberty to rename the issue to reflect the actual bug - previous title confused me (that's why I moved it to linter).

@scheglov
Copy link
Contributor

dart-bot pushed a commit that referenced this issue Jun 17, 2020
…e creations.

Bug: #42356
Change-Id: I674878c12b82af229d70e7a6eca5cd662ec29394
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151623
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-spec Issues with the analyzer's implementation of the language spec area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants