fixed: differentiation between uninitialized and possibly uninitialized#157507
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.
5a8f234 to
e4cb2a6
Compare
This comment has been minimized.
This comment has been minimized.
e4cb2a6 to
c868e83
Compare
This comment has been minimized.
This comment has been minimized.
c868e83 to
29826f9
Compare
|
As follow up work, we should point at the places where when a condition is true, the binding would be initialized, explaining that if that condition isn't true, the binding wouldn't be initialized. This is because there are some code patterns like state machines where you are doing work on a loop, and the first loop cycle a certain condition would be true, inicializing the binding, but the compiler/type system can't see that. The person that wrote the code might get confused when looking at the other branches and seeing that the binding is initialized elsewhere, so lets be proactive and point at the conditions. Given: let x;
if foo { x = 1; }
println!("{x}");We should point at Doing a review on the code now, I feel like this is likely in a mergeable state already. |
…uses Mentored-by: Usman Akinyemi <usmanakinyemi202@gmail.com> Signed-off-by: Dilshad Azam <azam.dilshad@gmail.com>
29826f9 to
417015d
Compare
I agree, It makes sense. Pointing at the conditions that guard initialization would likely make the diagnostic more actionable. We should work on it as a follow up. |
|
pinging: @estebank |
|
Thanks for your contribution! I took a close look at this PR, and it seems correct to me. But unfortunately I am not familiar enough with this part of the compiler to merge it, so r? @estebank since you took a look at it. |
|
Also, since this is an extra analysis, it might have perf impact. I will run perf just in case. @bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
…, r=<try> fixed: differentiation between uninitialized and possibly uninitialized
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (8a523a6): comparison URL. Overall result: no relevant changes - no action neededBenchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up. @rustbot label: -S-waiting-on-perf -perf-regression Instruction countThis perf run didn't have relevant results for this metric. Max RSS (memory usage)Results (primary -5.6%, secondary -1.5%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesThis perf run didn't have relevant results for this metric. Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 522.814s -> 519.939s (-0.55%) |
|
@bors r+ Thank you for the contribution! |
…initialized, r=estebank fixed: differentiation between uninitialized and possibly uninitialized Fix: rust-lang#157267
…initialized, r=estebank fixed: differentiation between uninitialized and possibly uninitialized Fix: rust-lang#157267
Rollup of 13 pull requests Successful merges: - #152544 (Stabilize `int_format_into` feature) - #157507 (fixed: differentiation between uninitialized and possibly uninitialized) - #155750 (Document that `ManuallyDrop`'s `Box` interaction has been fixed) - #157075 (lower edition requirements for some async-closure test helpers) - #157627 (remove LLVM `va_end` calls) - #157660 (normalize instead of evaluating type const patterns) - #157692 (Don't emit `unused_parens` suggestion for proc-macro-synthesized parens around bounds) - #157908 (fix binding const argument to assoc type suggestion) - #157915 (scalable vecs size incl. num vecs + no sret for scalable vecs) - #157916 (Avoid ICE on invalid crate-level cfg_attr predicates) - #157919 (mention in the `extern "tail"` error that it's supported on x86) - #157920 (mailmap: add mu001999) - #157924 (Update books)
View all comments
Fix: #157267