Skip to content
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

MIR borrowck: error does not know about free/named lifetimes #46472

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

MIR borrowck: error does not know about free/named lifetimes #46472

arielb1 opened this issue Dec 3, 2017 · 3 comments · Fixed by #46598
Labels
C-bug Category: This is a bug.
Milestone

Comments

@arielb1
Copy link
Contributor

arielb1 commented Dec 3, 2017

e.g.

fn bar<'a>() -> &'a mut u32 {
    &mut 4
}

Gives the following errors for AST and MIR borrowck - the MIR error should be more like the AST error

error[E0597]: borrowed value does not live long enough (Ast)
 --> bar.rs:2:10
  |
2 |     &mut 4
  |          ^ does not live long enough
3 | }
  | - temporary value only lives until here
  |
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 1:1...
 --> bar.rs:1:1
  |
1 | / fn bar<'a>() -> &'a mut u32 {
2 | |     &mut 4
3 | | }
  | |_^

error[E0597]: borrowed value does not live long enough (Mir)
 --> bar.rs:3:2
  |
2 |     &mut 4
  |          - temporary value created here
3 | }
  | -^ 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
@arielb1 arielb1 added this to the NLL prototype milestone Dec 3, 2017
@TimNN TimNN added the C-bug Category: This is a bug. label Dec 5, 2017
@nikomatsakis
Copy link
Contributor

Some mentoring notes:

The error is issued here:

let mut err = self.tcx
.path_does_not_live_long_enough(span, "borrowed value", Origin::Mir);
err.span_label(proper_span, "temporary value created here");
err.span_label(span, "temporary value dropped here while still borrowed");
err.note("consider using a `let` binding to increase its lifetime");
if let Some(end) = end_span {
err.span_label(end, "temporary value needs to live until here");
}

Note in particular the call to span_label at the end:

err.span_label(end, "temporary value needs to live until here");

I think the first thing to add a new parameter to report_borrowed_value_does_not_live_long_enough:

fn report_borrowed_value_does_not_live_long_enough(
&mut self,
_: Context,
(place, span): (&Place<'tcx>, Span),
end_span: Option<Span>,
) {

It's going to need access to the borrow that does not live long enough. The callers ought to have a variable borrow: &BorrowData in scope, so we can pass that in.

In that case, we can match on borrow.region. If we have a RegionKind::Free or RegionKind::EarlyBound, that corresponds to a named lifetime on the fn, so we don't probably want to emit that call to span_label I mentioned earlier. Instead, we want to invoke err.note. It might be worth ripgrep'ing through librustc_borrowck to find the code that it uses to emit its message.

@Yoric
Copy link
Contributor

Yoric commented Dec 7, 2017

I'm interested in handling this.

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
@Yoric
Copy link
Contributor

Yoric commented Dec 15, 2017

Oh, closed? Ok, moving to something else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants