Skip to content

fixed: differentiation between uninitialized and possibly uninitialized#157507

Open
mazam-97 wants to merge 1 commit into
rust-lang:mainfrom
mazam-97:fix-uninit-vs-possibly-uninitialized
Open

fixed: differentiation between uninitialized and possibly uninitialized#157507
mazam-97 wants to merge 1 commit into
rust-lang:mainfrom
mazam-97:fix-uninit-vs-possibly-uninitialized

Conversation

@mazam-97
Copy link
Copy Markdown

@mazam-97 mazam-97 commented Jun 5, 2026

Fix: #157267

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 5, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 5, 2026

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 (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: borrowck, compiler
  • borrowck, compiler expanded to 73 candidates
  • Random selection from 21 candidates

@mazam-97 mazam-97 force-pushed the fix-uninit-vs-possibly-uninitialized branch from c44dc16 to 5c47002 Compare June 5, 2026 19:49
@Unique-Usman
Copy link
Copy Markdown
Contributor

Thanks for the pr, can we change it commit message to something like "avoid reporting unreachable initializations for uninitialized uses" ?

@Unique-Usman
Copy link
Copy Markdown
Contributor

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.

@Unique-Usman
Copy link
Copy Markdown
Contributor

cc: @estebank

| - binding declared here but left uninitialized
...
LL | println!("{x}");
| ^ `x` used here but it isn't initialized
Copy link
Copy Markdown
Contributor

@Unique-Usman Unique-Usman Jun 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what you say makes sense. The error should be more explicit. I will add one more block.

@mazam-97
Copy link
Copy Markdown
Author

mazam-97 commented Jun 6, 2026

Thanks for the pr, can we change it commit message to something like "avoid reporting unreachable initializations for uninitialized uses" ?

Thanks for reviewing the PR @Unique-Usman. I will modify the commit messge as suggested.

@mazam-97
Copy link
Copy Markdown
Author

mazam-97 commented Jun 6, 2026

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.

Sure, I will add more tests having match expression and if else blocks.

@mazam-97 mazam-97 force-pushed the fix-uninit-vs-possibly-uninitialized branch from 5c47002 to 82f933b Compare June 6, 2026 23:35
@rust-log-analyzer

This comment has been minimized.

@mazam-97 mazam-97 force-pushed the fix-uninit-vs-possibly-uninitialized branch 2 times, most recently from 852819d to 6e04da5 Compare June 7, 2026 01:36
}

println!("{x}"); //~ ERROR E0381
}
Copy link
Copy Markdown
Contributor

@Unique-Usman Unique-Usman Jun 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be something like

fn main() {
    let x;

    match true {
        true => x = 42,
        false =>  println!("{x}"), //~ ERROR E0381
    }
}

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

}

println!("{x}"); //~ ERROR E0381
}
Copy link
Copy Markdown
Contributor

@Unique-Usman Unique-Usman Jun 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed. Added a test having an enum with match

@mazam-97 mazam-97 force-pushed the fix-uninit-vs-possibly-uninitialized branch from 6e04da5 to 5a8f234 Compare June 7, 2026 02:22
@rust-log-analyzer

This comment has been minimized.

…uses

Mentored-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
Signed-off-by: Dilshad Azam <azam.dilshad@gmail.com>
@mazam-97 mazam-97 force-pushed the fix-uninit-vs-possibly-uninitialized branch from 5a8f234 to e4cb2a6 Compare June 7, 2026 02:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect differentiation of uninitialized and possibly-uninitialized

5 participants