Skip to content

Compiler incorrectly understand initialisation of vars with patterns. #110452

Open
@lovely-error

Description

@lovely-error

Hey, this code does not work in 1.68.2, but it should.

struct W(u8);

fn sooqa () -> Option<W> {
    Some(W(0))
}

fn main() {
    let p@W(v);
    loop {
        if let Some(p_) = sooqa() { p = p_;break /*A*/ };
    }
    println!("{}", v);
}

This is the error message

error[[E0381]](https://doc.rust-lang.org/stable/error_codes/E0381.html): used binding `v` isn't initialized
  --> src/main.rs:13:20
   |
9  |     let p@W(v);
   |             - binding declared here but left uninitialized
...
13 |     println!("{}", v);
   |                    ^ `v` used here but it isn't initialized
   |
   = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
   |
9  |     let p@W(v = 0);
   |               +++

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-control-flowArea: Control flowA-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions