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

Crash on the use of moved value in the closure #6653

Closed
lifthrasiir opened this issue May 21, 2013 · 2 comments
Closed

Crash on the use of moved value in the closure #6653

lifthrasiir opened this issue May 21, 2013 · 2 comments
Labels
I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.

Comments

@lifthrasiir
Copy link
Contributor

The following code segfaults in both 0.6 and the current incoming:

struct A { t: bool }
fn main() {
    let mut a = ~A { t: false };
    let f = || { a.t = true; };
    let mut x = a;
    f();
    x.t = false; // not strictly required but ignores an warning
}

The cause is that a was moved to x and f (created before the movement) was unable to deduce that a has been moved. Ideally such code should not compile.

@Aatch
Copy link
Contributor

Aatch commented May 24, 2013

Hmm, it seems that because f is a stack closure, it is allowed to capture upvars pretty much without limit, because (it thinks) it can guarantee that the captured variables are valid for it's lifetime. Ideally, it should act as if the closure either has a mutable borrow, or that a has been moved. I'm tempted to lean towards the first option in this case, since it means that the end of f releases the borrow, thus allowing a to be used again.

This means that this code shouldn't compile, giving the same error message as:

fn main() {
  let mut a = ~1;
  let b = &mut *a;
  a = ~2;
}
// error: cannot assign to `a` because it is borrowed

I'm nominating this for "well defined".

@nikomatsakis
Copy link
Contributor

dup of #2202

flip1995 pushed a commit to flip1995/rust that referenced this issue Jan 30, 2021
Rustup

changelog: Deprecate `unknown_clippy_lints` (integrated in `unknown_lints`)

r? `@ghost`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.
Projects
None yet
Development

No branches or pull requests

3 participants