Skip to content

Better diagnostics for multiple borrowed fields with non-unified lifetimes #59299

Open

Description

The following code

struct Foo<'a> { a: &'a usize }

fn get_account_value<'l>(
    flag: bool,
    a: &'l mut Foo,
    b: &'l mut Foo,
) {
    let _ = if flag { a } else { b };
}

Produces the following output:

error[E0623]: lifetime mismatch
 --> src/main.rs:8:34
  |
5 |     a: &'l mut Foo,
  |                ---
6 |     b: &'l mut Foo,
  |                --- these two types are declared with different lifetimes...
7 | ) {
8 |     let _ = if flag { a } else { b };
  |                                  ^ ...but data from `b` flows into `a` here

error[E0623]: lifetime mismatch
 --> src/main.rs:8:34
  |
5 |     a: &'l mut Foo,
  |                --- these two types are declared with different lifetimes...
6 |     b: &'l mut Foo,
  |                ---
7 | ) {
8 |     let _ = if flag { a } else { b };
  |                                  ^ ...but data from `a` flows into `b` here

error: aborting due to 2 previous errors

It seems like the borrow checker could handle this case better. Particularly, the fact that Foo has a lifetime makes the errors more puzzling than they need to be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.NLL-fixed-by-NLLBugs fixed, but only when NLL is enabled.Bugs fixed, but only when NLL is enabled.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant 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