8000 Poor problem and correction messages related to assignability · Issue #60516 · dart-lang/sdk · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Poor problem and correction messages related to assignability #60516

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

Open
bwilkerson opened this issue Apr 10, 2025 · 1 comment
Open

Poor problem and correction messages related to assignability #60516

bwilkerson opened this issue Apr 10, 2025 · 1 comment
Labels
area-dart-model For issues related to conformance to the language spec in the parser, compilers or the CLI analyzer. model-messages Poor/undesirable messaging in errors/warnings emitted by the analyzer and/or CFE. P3 A lower priority bug or feature request

Comments

@bwilkerson
Copy link
Member

Consider the following code:

class A {}

class B implements A {}

class C {
  late A a;

  void m(bool b) {
    a = b ? B() : null; // <--
  }
}

On the marked line there will be a diagnostic of the following form:

A value of type 'B?' can't be assigned to a variable of type 'A'. (invalid_assignment)
Try changing the type of the variable, or casting the right-hand type to 'A'.

While the problem message is technically correct, it would be helpful to have a slightly different message for this particular case, perhaps something of the form

A value of type 'B?' can't be assigned to a variable of type 'A' because it is a nullable type.

The correction, or the second half of it, is not correct. Casting the right-hand side will silence the diagnostic by turning it into a runtime exception. Better advice would be something along the lines of

Try making the variable nullable or change the right-hand to not produce `null`.
@bwilkerson bwilkerson added area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. P3 A lower priority bug or feature request area-dart-model For issues related to conformance to the language spec in the parser, compilers or the CLI analyzer. and removed area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. labels Apr 10, 2025
@johnniwinther johnniwinther added the model-messages Poor/undesirable messaging in errors/warnings emitted by the analyzer and/or CFE. label May 8, 2025
@johnniwinther
Copy link
Member

cc @chloestefantsova

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dart-model For issues related to conformance to the language spec in the parser, compilers or the CLI analyzer. model-messages Poor/undesirable messaging in errors/warnings emitted by the analyzer and/or CFE. P3 A lower priority bug or feature request
Projects
None 3850 yet
Development

No branches or pull requests

2 participants
0