Skip to content

Incorrect differentiation of uninitialized and possibly-uninitialized #157267

@ParisaBM

Description

@ParisaBM

I tried this code:

fn foo(b: bool) {
    let x;
    if b {
        x = 1;
    } else {
        println!("{x}");
    }
}

I expected to see this happen:

Image

since x is uninitialized.

Instead, this happened:

Image

I would consider it incorrect to call x possibly-uninitialized. The differentiation between uninitialized and possibly-uninitialized appears to be here: conflict_errors.rs:827 where it usually suffices that the variable be initialized somewhere to be consider possibly-uninitialized. Indeed even simpler examples exhibit this issue:

let x;
println!("{x}");
x = 1;

However, we have access to the MaybeInitializedPlaces which seems to provide a more accurate criterion to decide whether the variable is possibly or definitely uninitialized. If it's decided that this change is worth making, I would be able to submit a pull request.

Meta

rustc --version --verbose:

rustc 1.98.0-nightly (14210df0e 2026-05-31)
binary: rustc
commit-hash: 14210df0e27ccd7d9e6a05b8085cbd438e4bbc65
commit-date: 2026-05-31
host: x86_64-unknown-linux-gnu
release: 1.98.0-nightly
LLVM version: 22.1.6
Backtrace

As the compiler doesn't crash, nothing extra was shown.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions