Skip to content

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

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
stereotype441 opened this issue Nov 1, 2021 · 5 comments
Open
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-ux P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@stereotype441
Copy link
Member

stereotype441 commented Nov 1, 2021

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

@stereotype441 stereotype441 added the legacy-area-analyzer Use area-devexp instead. label Nov 1, 2021
@stereotype441 stereotype441 self-assigned this Nov 1, 2021
@jcollins-g jcollins-g added the P2 A bug or feature request we're likely to work on label Nov 1, 2021
@InMatrix
Copy link
Collaborator

Here is how this error message shows up in VS Code:

image

Only the first part of the full message is included for some reason.

@stereotype441
Copy link
Member Author

Assigning to Konstantin. As discussed in the meeting, it would also be good to have a quick fix (or assist) that introduces a local variable.

@scheglov
Copy link
Contributor

Hm... Currently we report "not promoted" as attached DiagnosticMessages. This way we can use a smaller number of error codes, and attached reasons why the receiver was not promoted to non-nullable. We could specialize PropertyNotPromoted to report a more specific ErrorCode with a better message, although it looks that we would have to move the attached message into the main message (the "because" part). Is this what you have in mind?

PropertyNotPromoted does not tell us where the promotion was attempted. Or do I miss it? If it is not there, we still can support a quick fix for probably the most often case of if (o.x != null) { o.x.foo }.

@stereotype441
Copy link
Member Author

PropertyNotPromoted does not tell us where the promotion was attempted.

You're right. I'll do some digging to see how difficult it would be to add this functionality.

@scheglov
Copy link
Contributor

https://dart-review.googlesource.com/c/sdk/+/227900 will add a fix for a few cases.

copybara-service bot pushed a commit that referenced this issue Jan 13, 2022
…LABLE_VALUE.

Bug: #47588
Change-Id: Ice823f239b3b152d9583cecb143ea78a62361539
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/227900
Reviewed-by: Samuel Rawlins <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
@srawlins srawlins added the type-enhancement A request for a change that isn't a bug label Mar 14, 2024
@bwilkerson bwilkerson added area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. and removed legacy-area-analyzer Use area-devexp instead. labels Feb 27, 2025
@scheglov scheglov removed their assignment Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-ux P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants