Skip to content

MIR borrowck: error message confuses locals and temporaries #46471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
arielb1 opened this issue Dec 3, 2017 · 3 comments · Fixed by #46598
Closed

MIR borrowck: error message confuses locals and temporaries #46471

arielb1 opened this issue Dec 3, 2017 · 3 comments · Fixed by #46598
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug.
Milestone

Comments

@arielb1
Copy link
Contributor

arielb1 commented Dec 3, 2017

e.g.:

fn foo() -> &u32 {
    let x = 0;
    &x
}

Gives the following error:

error[E0597]: `x` does not live long enough (Ast)
 --> foo.rs:4:6
  |
4 |     &x
  |      ^ does not live long enough
5 | }
  | - borrowed value only lives until here
  |
  = note: borrowed value must be valid for the static lifetime...

error[E0597]: borrowed value does not live long enough (Mir)
 --> foo.rs:5:2
  |
3 |     let x = 0;
  |         - temporary value created here
4 |     &x
5 | }
  | -^ temporary value dropped here while still borrowed
  | |
  | temporary value needs to live until here
  |
  = note: consider using a `let` binding to increase its lifetime

error: aborting due to 2 previous errors

The MIR error message should be more like the AST one

@arielb1 arielb1 added A-diagnostics Area: Messages for errors, warnings, and lints WG-compiler-nll labels Dec 3, 2017
@arielb1 arielb1 added this to the NLL prototype milestone Dec 3, 2017
@arielb1
Copy link
Contributor Author

arielb1 commented Dec 3, 2017

Note that the MIR message has 2 problems

  1. It calls locals "temporaries"
  2. It throws away the span of the borrow, which can be confusing if a local is only borrowed in a few places in a big function.

@davidtwco
Copy link
Member

I'll take a stab at this one.

@TimNN TimNN added the C-bug Category: This is a bug. label Dec 5, 2017
@nikomatsakis
Copy link
Contributor

The example as given does not compile. You can try these two:

fn foo() -> &'static u32 {
    let x = 0;
    &x
}

fn main() { }

and

fn foo(_: &u32) -> &u32 {
    let x = 0;
    &x
}

fn main() { }

bors added a commit that referenced this issue Dec 12, 2017
MIR borrowck: error message confuses locals and temporaries

Fixes #46471 and fixes #46472 (see [this Gitter comment](https://gitter.im/rust-impl-period/WG-compiler-nll?at=5a2d5cb53ae2aa6b3facf0c2)).

r? @arielb1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants