Skip to content
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

"Binding is possibly uninitalized" reports some additional spans incorrectly in addition to the correct one #126133

Open
Arnavion opened this issue Jun 7, 2024 · 1 comment
Assignees
Labels
A-control-flow Area: Control flow A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Arnavion
Copy link

Arnavion commented Jun 7, 2024

Code

enum E {
    A,
    B,
    C,
}

fn foo(e: E) {
    let bar;
    match e {
        E::A => return,

        E::B => {}

        E::C => {
            bar = 5;
        }
    }

    let _baz = bar;
}

Current output

error[E0381]: used binding `bar` is possibly-uninitialized
  --> src/lib.rs:19:16
   |
8  |     let bar;
   |         --- binding declared here but left uninitialized
9  |     match e {
10 |         E::A => return,
   |         ---- if this pattern is matched, `bar` is not initialized
11 |
12 |         E::B => {}
   |         ---- if this pattern is matched, `bar` is not initialized
...
19 |     let _baz = bar;
   |                ^^^ `bar` used here but it is possibly-uninitialized

For more information about this error, try `rustc --explain E0381`.

Desired output

error[E0381]: used binding `bar` is possibly-uninitialized
  --> src/lib.rs:19:16
   |
8  |     let bar;
   |         --- binding declared here but left uninitialized
9  |     match e {
10 |         E::A => return,
11 |
12 |         E::B => {}
   |         ---- if this pattern is matched, `bar` is not initialized
...
19 |     let _baz = bar;
   |                ^^^ `bar` used here but it is possibly-uninitialized

For more information about this error, try `rustc --explain E0381`.

Rationale and extra context

Yes bar is not initialized in the E::A arm, but the goal of the diagnostic should be to print the spans that the user needs to *fix*. The E::A arm diverges so the user only needs to fix the E::B arm. So the report for E::B is correct, but the one for E::A should not be emitted.

Other cases

My original code that triggered this was similar to wrapping the whole body of foo in a loop {} and using break instead of return. The rationale is the same.

Rust Version

rustc 1.80.0-nightly (a330e4959 2024-06-04)
binary: rustc
commit-hash: a330e49593ee890f9197727a3a558b6e6b37f843
commit-date: 2024-06-04
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.6

Anything else?

No response

@Arnavion Arnavion added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 7, 2024
@fmease fmease added D-papercut Diagnostics: An error or lint that needs small tweaks. A-patterns Relating to patterns and pattern matching A-control-flow Area: Control flow and removed A-patterns Relating to patterns and pattern matching labels Jun 7, 2024
@rust-lang rust-lang deleted a comment Jun 7, 2024
@linyihai
Copy link
Contributor

@rustbot claim

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jun 12, 2024
…, r=pnkfelix

No uninitalized report in a pre-returned match arm

This is a attemp to address rust-lang#126133
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jun 12, 2024
Rollup merge of rust-lang#126295 - linyihai:uninitalized-in-match-arm, r=pnkfelix

No uninitalized report in a pre-returned match arm

This is a attemp to address rust-lang#126133
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Jun 13, 2024
No uninitalized report in a pre-returned match arm

This is a attemp to address rust-lang/rust#126133
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-control-flow Area: Control flow A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants