-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Clarify E0381 diagnostics for branch conditions #156603
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
eval-exec
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
eval-exec:e0381-diagnostic-condition-note
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| //@ edition: 2024 | ||
|
|
||
| fn main() { | ||
| let v1: Vec<i32> = vec![1, 2, 3]; | ||
| let mut iter = v1.iter(); | ||
|
|
||
| let mut first_encounter = true; | ||
| let mut token: i32; | ||
| let mut following_token: i32; | ||
| loop { | ||
| if first_encounter { | ||
| if let Some(token) = iter.next() { | ||
| match iter.next() { | ||
| Some(temp) => { | ||
| following_token = *temp; | ||
| first_encounter = false; | ||
| println!("{} followed by {}", token, following_token); | ||
| } | ||
| _ => { | ||
| println!("{} is last", token); | ||
| } | ||
| } | ||
| } else { | ||
| break; | ||
| } | ||
| } else { | ||
| token = following_token; //~ ERROR used binding `following_token` isn't initialized | ||
| first_encounter = true; | ||
| match iter.next() { | ||
| Some(temp) => { | ||
| following_token = *temp; | ||
| first_encounter = false; | ||
| println!("{} followed by {}", token, following_token); | ||
| } | ||
| _ => { | ||
| println!("{} is last", token); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn guarded_match(value: i32, condition: bool) { | ||
| let y; | ||
| match value { | ||
| 0 => { | ||
| y = 1; | ||
| } | ||
| _ if condition => {} | ||
| _ => { | ||
| y = 2; | ||
| } | ||
| } | ||
| let _z = y; //~ ERROR used binding `y` is possibly-uninitialized | ||
| } |
38 changes: 38 additions & 0 deletions
38
tests/ui/borrowck/branch-condition-flow-issue-156494.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| error[E0381]: used binding `following_token` isn't initialized | ||
| --> $DIR/branch-condition-flow-issue-156494.rs:27:21 | ||
| | | ||
| LL | let mut following_token: i32; | ||
| | ------------------- binding declared here but left uninitialized | ||
| ... | ||
| LL | _ => { | ||
| | - if this pattern is matched, `following_token` is not initialized | ||
| ... | ||
| LL | } else { | ||
| | ------ if the `if` condition is `false` and this `else` arm is executed, `following_token` is not initialized | ||
| ... | ||
| LL | token = following_token; | ||
| | ^^^^^^^^^^^^^^^ `following_token` used here but it isn't initialized | ||
| | | ||
| = note: when checking initialization, the compiler describes possible control-flow paths without evaluating whether branch conditions can actually have the values shown | ||
| help: consider assigning a value | ||
| | | ||
| LL | let mut following_token: i32 = 42; | ||
| | ++++ | ||
|
|
||
| error[E0381]: used binding `y` is possibly-uninitialized | ||
| --> $DIR/branch-condition-flow-issue-156494.rs:54:14 | ||
| | | ||
| LL | let y; | ||
| | - binding declared here but left uninitialized | ||
| ... | ||
| LL | _ if condition => {} | ||
| | -------------- if this pattern and condition are matched, `y` is not initialized | ||
| ... | ||
| LL | let _z = y; | ||
| | ^ `y` used here but it is possibly-uninitialized | ||
| | | ||
| = note: when checking initialization, the compiler describes possible control-flow paths without evaluating whether branch conditions can actually have the values shown | ||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0381`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.