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

Ungreat diagnostic error borrowed data escapes outside of function #62953

Closed
spastorino opened this issue Jul 24, 2019 · 5 comments
Closed

Ungreat diagnostic error borrowed data escapes outside of function #62953

spastorino opened this issue Jul 24, 2019 · 5 comments
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@spastorino
Copy link
Member

spastorino commented Jul 24, 2019

On an change I was implementing in the compiler I got the following error ...

error[E0521]: borrowed data escapes outside of function
   --> src/librustc_codegen_ssa/mir/analyze.rs:228:9
    |
218 |     fn visit_place(&mut self,
    |                    --------- `self` is declared here, outside of the function body
219 |                    place: &mir::Place<'tcx>,
    |                    ----- `place` is a reference that is only valid in the function body
...
228 |         self.process_place(place_ref, context, location);
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `place` escapes the function body here

error: aborting due to previous error

I don't remember how the code looked like exactly but we can probably rebuild an example that shows this behavior.

More info https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/borrowed.20data.20escapes.20outside.20of.20function

@jonas-schievink jonas-schievink added A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 24, 2019
@ssomers
Copy link
Contributor

ssomers commented Aug 8, 2020

This is effectively google's landing page for an explanation of this mysterious Rust error E0521, given that the official error spokeswomen and rustc --explain deny its existence. Most of the links here badmouth the error, but I couldn't figure out what the actual problem in the code is about. #67007 goes into a lot of detail, but if I understood all that, I bet I wouldn't have landed here.

Luckily, just a few links further, google also lists a link to a stackoverflow question (even though it does not mention E0521 nor the same error message). It doesn't actually offer a complete example, but it allowed me to strip mine down and spot the solution.

fn main() {
    let mut stash: Option<&i32> = None;
    let mut thief = |r: &i32| { stash = Some(r) };
    thief(&42);
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0521]: borrowed data escapes outside of closure
 --> src/main.rs:3:33
  |
2 |     let mut stash: Option<&i32> = None;
  |         --------- `stash` declared here, outside of the closure body
3 |     let mut thief = |r: &i32| { stash = Some(r) };
  |                      -          ^^^^^^^^^^^^^^^ `r` escapes the closure body here
  |                      |
  |                      `r` is a reference that is only valid in the closure body

The problem here is that we need to remove the type from the closure:

    let mut thief = |r| { stash = Some(r) };

@jesusprubio
Copy link
Contributor

Long explanation added. Thanks for the explanation @ssomers, it helped me a lot :).

#81634

Do you consider it enough to close the issue?

@ssomers
Copy link
Contributor

ssomers commented Feb 4, 2021

For my example, that is definitely very helpful and more than enough.

@jesusprubio
Copy link
Contributor

@GuillaumeGomez , could you close this issue please? :)

@GuillaumeGomez
Copy link
Member

@ssomers seems satisfied so let's close it. Thanks @jesusprubio !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants