Skip to content

Poor problem and correction messages related to assignability #60516

@bwilkerson

Description

@bwilkerson

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`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.model-messagesPoor/undesirable messaging in errors/warnings emitted by the analyzer and/or CFE.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions