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

Accessing a field in a closure makes rust want to copy ... something #2833

Closed
gwillen opened this issue Jul 7, 2012 · 4 comments
Closed
Labels
A-type-system Area: Type system
Milestone

Comments

@gwillen
Copy link
Contributor

gwillen commented Jul 7, 2012

class example {
  let mut x: int;
  new() {
    self.x = 1;
  }

  drop {}
}

fn main(_args: [str]) {
  let x: example = example();
  let _z = fn@() {
    let _a: int = x.x;
  };
  x.x = 5;
}
noncopyable-field.rs:13:18: 13:19 error: copying a noncopyable value
noncopyable-field.rs:13     let _a: int = x.x;
                                          ^

Notably, it seems to be trying to copy the example object, not the int (as I first assumed), at least if you trust the error location marker.

So the question is, why does accessing a field of the object try to copy it?

This particular repro goes away if you remove the write to x.x after the closure is created, but I have another, larger one where the write isn't there and it still does it.

@ghost ghost assigned catamorphism Jul 7, 2012
@gwillen
Copy link
Contributor Author

gwillen commented Jul 7, 2012

Oh, in the example where I don't assign to x.x, I instead let z = @x after the closure is created, to trigger the same error.

@nikomatsakis
Copy link
Contributor

This is not a bug. An fn@ closure copies all local variables which are referenced into the closure. If you only want the value of x.x, then make a temporary variable.

@gwillen
Copy link
Contributor Author

gwillen commented Jul 7, 2012

Aha, mystery solved. I will think about what it would take to perhaps make this more intuitive.

In the case where I don't reference x after I close over it, and there's no error: is it detecting that x is dead, and moving it into the closure? Or what's happening there?

@nikomatsakis
Copy link
Contributor

we discussed this on IRC, but for posterity's sake: if the variable is not used after the closure is constructed, it is considered a "last use" and is moved into the closure

RalfJung pushed a commit to RalfJung/rust that referenced this issue Apr 28, 2023
celinval pushed a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
Adds a set of scripts to automate building and installing most
dependencies in Amazon Linux 2 environments.

This will make it easier to provide ARM64 binaries for Linux (see
rust-lang#2805). The installation of dependencies isn't minimal but improves our
situation w.r.t. AL2.

Testing done manually on remote AL2 ARM64 instances, after running this
I'm able to run the regression successfully.

### Callouts
 * Hasn't been tested on AL2 x86.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

3 participants