Skip to content

[analysis_server] Infer nullable type in 'Add type annotation' when assignment isn't unconditional - #64046

Open
hkarmoush wants to merge 1 commit into
dart-lang:mainfrom
hkarmoush:fix-add-type-annotation-nullable
Open

[analysis_server] Infer nullable type in 'Add type annotation' when assignment isn't unconditional#64046
hkarmoush wants to merge 1 commit into
dart-lang:mainfrom
hkarmoush:fix-add-type-annotation-nullable

Conversation

@hkarmoush

Copy link
Copy Markdown

Summary

The Add type annotation fix/assist (triggered e.g. by prefer_typing_uninitialized_variables) infers an uninitialized variable's type from assignments found anywhere in the rest of the enclosing block, without regard to whether those assignments are guaranteed to run. For a variable only assigned inside a loop body, a single-branch if, or a try block, that code might not execute, so the variable could still hold its implicit initial value, null, at the point of use — but the fix inferred a non-nullable type anyway.

int? f() {
  var result;
  for (var i in [1, 2, 3]) {
    result = i;
  }
  return result; // fix incorrectly inferred `int`, should be `int?`
}

This adds a conservative check for whether at least one of the remaining statements is guaranteed to assign the variable — accounting for if/else (both branches), try/finally, do-while (body always runs once), labeled statements, and immediately-invoked closures — and makes the inferred type nullable when it isn't.

Fixes #64015

Test plan

  • Added regression tests for the issue's for-each loop, single-branch if, and try/catch cases, plus counterexamples (if/else, do-while, try/finally) that must remain non-nullable
  • pkg/analysis_server/test/src/services/correction/fix/add_type_annotation_test.dart — 43/43 pass
  • pkg/analysis_server/test/src/services/correction/assist/add_type_annotation_test.dart — 75/75 pass
  • pkg/analysis_server/test/src/services/correction/test_all.dart (full correction suite) — 6385/6385 pass, no regressions
  • dart format / dart analyze clean on changed files

…ssignment isn't unconditional

The `Add type annotation` fix/assist (triggered e.g. by
`prefer_typing_uninitialized_variables`) infers a variable's type from
assignments found anywhere in the rest of the enclosing block, without
regard to whether those assignments are guaranteed to run. For a
variable only assigned inside a loop body, a single-branch `if`, or a
`try` block, the loop/branch/try might not execute, so the variable
could still hold its implicit initial value, `null`, at the point of
use. The fix incorrectly inferred a non-nullable type in these cases.

This adds a conservative check for whether at least one of the
remaining statements is guaranteed to assign the variable (accounting
for `if`/`else`, `try`/`finally`, `do`-`while`, labeled statements, and
immediately-invoked closures), and makes the inferred type nullable
when it isn't.

Fixes dart-lang#64015
@copybara-service

Copy link
Copy Markdown

Thank you for your contribution! This project uses Gerrit for code reviews. Your pull request has automatically been converted into a code review at:

https://dart-review.googlesource.com/c/sdk/+/536161

Please wait for a developer to review your code review at the above link; you can speed up the review if you sign into Gerrit and manually add a reviewer that has recently worked on the relevant code. See CONTRIBUTING.md to learn how to upload changes to Gerrit directly.

Additional commits pushed to this PR will update both the PR and the corresponding Gerrit CL. After the review is complete on the CL, your reviewer will merge the CL (automatically closing this PR).

@copybara-service

Copy link
Copy Markdown

Gerrit CL has been approved, please wait for a reviewer to merge it.

There are also new comments on the CL, please review them and respond if necessary because reviewer might have requested clarifications or possibly some final changes and will not merge the CL until their questions have been answered.

@copybara-service

Copy link
Copy Markdown

Gerrit CL has been approved, please wait for a reviewer to merge it.

There are also new comments on the CL, please review them and respond if necessary because reviewer might have requested clarifications or possibly some final changes and will not merge the CL until their questions have been answered.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

prefer_typing_uninitialized_variables fix Add type annotation incorrect type - loops/ifs/catches

1 participant