Skip to content

Expose Dart's "why not promoted" information in VS Code #3136

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

Closed
devoncarew opened this issue Feb 9, 2021 · 5 comments
Closed

Expose Dart's "why not promoted" information in VS Code #3136

devoncarew opened this issue Feb 9, 2021 · 5 comments

Comments

@devoncarew
Copy link
Contributor

As of dart-lang/sdk@a42244f, the analyzer now generates context messages in some circumstances explaining why type promotion failed. We would like these messages to show up in VS Code in a way that's easily discoverable.

See also dart-lang/sdk#44897.

(Danny, I'll give you some context on this when we sync up next)

@devoncarew
Copy link
Contributor Author

Ah, this work might already have been done?

Screen Shot 2021-02-09 at 4 49 59 PM

@DanTup, I'll close this issue (but please re-open if you think there's something to do here).

@DanTup
Copy link
Member

DanTup commented Feb 10, 2021

If the info is in the correction or contextMessages then yep, it shows up here. The correction is appended to the main message, and the context messages are relatedInformation.

Non-LSP:

if (error.correction)
diag.message += `\n${error.correction}`;
if (error.contextMessages && error.contextMessages.length)
diag.relatedInformation = error.contextMessages.map(DartDiagnosticProvider.createRelatedInformation);

LSP:
https://github.com/dart-lang/sdk/blob/f306f4c4d41c8f2af2a3f3f30880703d43a8c5af/pkg/analysis_server/lib/src/lsp/mapping.dart#L935-L945

I'm not totally familiar with the "why not promoted" info (your screenshot doesn't look like it's that?) but if there's an example I can use to trigger it I can test in both LSP/non-LSP.

@stereotype441
Copy link

@DanTup

I'm not totally familiar with the "why not promoted" info (your screenshot doesn't look like it's that?) but if there's an example I can use to trigger it I can test in both LSP/non-LSP.

You should be able to test using this example (it works with the command-line analyzer as of dart-lang/sdk@a42244f):

f(int? i, int? j) {
  if (i == null) return; // (1)
  i = j;                 // (2)
  i.isEven;              // (3)
}

On the line marked (3) there should be an error message saying The property 'isEven' can't be unconditionally accessed because the receiver can be 'null'. There should be an associated context message for the line marked (2) saying Variable 'i' could be null due to a write occurring here.

Two minor notes:

@DanTup
Copy link
Member

DanTup commented Feb 10, 2021

@stereotype441 thanks! In that case, it appears to be working as expected (both under LSP and the original protocol):

Screenshot 2021-02-10 at 15 40 11

Clicking on the main error ("The property 'isEven'") navigates to line (3) were the red squiggle is, and clicking the nested message "Variable 'i' could be null..." navigates to line (2).

@stereotype441
Copy link

@DanTup Great! Thanks for checking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants