fixed: differentiation between uninitialized and possibly uninitialized#157507
fixed: differentiation between uninitialized and possibly uninitialized#157507mazam-97 wants to merge 1 commit into
Conversation
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @tiif (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
Why was this reviewer chosen?The reviewer was selected based on:
|
c44dc16 to
5c47002
Compare
|
Thanks for the pr, can we change it commit message to something like "avoid reporting unreachable initializations for uninitialized uses" ? |
|
Also, we need to add more test i.e one for match expression, one or two if we have multiple block of if or match expressions. |
|
cc: @estebank |
| | - binding declared here but left uninitialized | ||
| ... | ||
| LL | println!("{x}"); | ||
| | ^ `x` used here but it isn't initialized |
There was a problem hiding this comment.
Here, we might want to have something like
"`x` used here, but all known initializations occur on control-flow paths that do not reach this use"
or a shorter version
`x` used here, but it is not initialized on any path leading to this point
because, x was actually initialized but, it was initilized on another branch or block which will never reach the error block.
There would probably be another else if statement in this block -> https://github.com/rust-lang/rust/blob/main/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs#L820C86-L845C40
There was a problem hiding this comment.
I think what you say makes sense. The error should be more explicit. I will add one more block.
Thanks for reviewing the PR @Unique-Usman. I will modify the commit messge as suggested. |
Sure, I will add more tests having match expression and if else blocks. |
5c47002 to
82f933b
Compare
This comment has been minimized.
This comment has been minimized.
852819d to
6e04da5
Compare
| } | ||
|
|
||
| println!("{x}"); //~ ERROR E0381 | ||
| } |
There was a problem hiding this comment.
This should be something like
fn main() {
let x;
match true {
true => x = 42,
false => println!("{x}"), //~ ERROR E0381
}
}
| } | ||
|
|
||
| println!("{x}"); //~ ERROR E0381 | ||
| } |
There was a problem hiding this comment.
if you want to have multiple match, do not nested it like this. You can have something like this.
enum PrimaryColor {
RED,
BLUE,
GREEN
}
fn main() {
let x;
let pc = PrimaryColor::RED,
match pc {
PrimaryColor::RED => x = 1,
PrimaryColor::BLUE => x = 3,
PrimaryColor::GREEN => println!("{x}"); //~ ERROR E0381
}
}
Also, something similar for the if and else i.e for multiple branch.
There was a problem hiding this comment.
Addressed. Added a test having an enum with match
6e04da5 to
5a8f234
Compare
This comment has been minimized.
This comment has been minimized.
…uses Mentored-by: Usman Akinyemi <usmanakinyemi202@gmail.com> Signed-off-by: Dilshad Azam <azam.dilshad@gmail.com>
5a8f234 to
e4cb2a6
Compare
Fix: #157267