Skip to content

Variables bound with different modes in patterns #23610

Open
@Stebalien

Description

@Stebalien

The following doesn't compile:

enum Test<'a> {
    A(&'a u64),
    B(u64),
}
fn foo(test: Test) {
    match test {
        Test::A(r) | Test::B(ref r) => println!("{}", r)
    }
}
fn main() {
    foo(Test::A(&0));
    foo(Test::B(1));
}

failing with the following error:

test.rs:7:30: 7:35 error: variable `r` is bound with different mode in pattern #2 than in pattern #1
test.rs:7         Test::A(r) | Test::B(ref r) => println!("{}", r)
                                       ^~~~~
error: aborting due to previous error

However, the following does work:

enum Test<'a> {
    A(&'a u64),
    B(u64),
}
fn foo(test: Test) {
    match test {
        Test::A(&ref r) | Test::B(ref r) => println!("{}", r)
    }
}
fn main() {
    foo(Test::A(&0));
    foo(Test::B(1));
}

Is there any reason rust can't just perform this reborrow automatically? &ref is a very cryptic pattern.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerC-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