Skip to content

Need a lifetime hint for E0409 "variable is bound inconsistently" #104069

Open
@cuviper

Description

@cuviper

From https://users.rust-lang.org/t/how-to-match-t-and-t-in-the-same-arm/83835

Given the following code: playground

enum Foo<'a, T> {
    A,
    B(T),
    C(&'a T),
}

fn main() {
    let foo = Foo::<'static, ()>::A;
    
    match foo {
        Foo::A => {},
        Foo::B(ref t) | Foo::C(t) => todo!(),
    }
}

The current output is:

error[E0409]: variable `t` is bound inconsistently across alternatives separated by `|`
  --> src/main.rs:12:32
   |
12 |         Foo::B(ref t) | Foo::C(t) => todo!(),
   |                    -           ^ bound in different ways
   |                    |
   |                    first binding

The cause of the error is a lifetime difference, where B(ref t) binds &'_ T (for some local lifetime) and C(t) binds &'static T. It would be nice to have a hint about this, possibly suggesting a fix into C(&ref t) to reborrow that reference.

(Even better if the language/compiler could make that happen automatically!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-patternsRelating to patterns and pattern matchingD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-papercutDiagnostics: An error or lint that needs small tweaks.F-or_patterns`#![feature(or_patterns)]`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