Skip to content

Error messages with lack of property promotion could be improved #47588

Open
@stereotype441

Description

@stereotype441

@mit-mit points out that the errors that occur when the user attempts to promote a property could be clearer. For example, this code:

class C {
  int? x;
  test() {
    if (x != null) {
      print(x.isEven);
    }
  }
}

Produces the error message:

The property 'isEven' can't be unconditionally accessed because the receiver can be 'null'.

with the following suggested fix:

Try making the access conditional (using '?.') or adding a null check to the target ('!').

and then the following context message (pointing to the declaration int? x;):

'x' refers to a property so it couldn't be promoted.  See http://dart.dev/go/non-promo-property

Two things are unfortunate about this:
(1) The user has to read through a lot of error message text before they get to the meat of the problem (properties can't be promoted).
(2) The suggested fix isn't great.

It would be better to have an error message like this:

Cannot access 'isEven' because 'x' can be null. Because 'x' is a property, it cannot be promoted to non-null.  See http://dart.dev/go/non-promo-property

and a suggested fix like this:

Try assigning 'x' to a local variable, for example: final x = this.x;

It probably would still be useful to have a context message pointing to the declaration int? x;. Maybe it could say something like:

The property 'x' is declared here.

Note that this isn't the only error message that could potentially be improved; it's just an example.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work onarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-uxtype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions