Skip to content

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

Closed
@gwillen

Description

@gwillen
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions