-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Suggest cloning Arc
moved into closure
#124595
base: master
Are you sure you want to change the base?
Conversation
rustbot has assigned @compiler-errors. Use |
r? compiler too busy to review this |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
``` error[E0382]: borrow of moved value: `x` --> $DIR/moves-based-on-type-capture-clause-bad.rs:9:20 | LL | let x = "Hello world!".to_string(); | - move occurs because `x` has type `String`, which does not implement the `Copy` trait LL | thread::spawn(move || { | ------- value moved into closure here LL | println!("{}", x); | - variable moved due to use in closure LL | }); LL | println!("{}", x); | ^ value borrowed here after move | = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) help: clone the value before moving it into the closure | LL ~ let value = x.clone(); LL ~ thread::spawn(move || { LL ~ println!("{}", value); | ``` Fix rust-lang#104232.
@estebank , isn't this suggesting cloning a lot more stuff than just The description for issue #104232 explicitly said:
which I took to mean that we wouldn't jump to suggesting cloning for things like (for others following along, the reason for pushing back here is the concern that clones are not always cheap, and therefore the compiler should take care before suggesting them. The underling assumption of #104232 is that cloning an |
@rfcbot author |
@rustbot author |
The thing is that given the case being checked for here, either people For example, in this case: use std::thread;
fn main() {
let x = "Hello world!".to_string();
thread::spawn(move || {
println!("{}", x);
});
println!("{}", x); //~ ERROR borrow of moved value
} we suggest to |
☔ The latest upstream changes (presumably #125379) made this pull request unmergeable. Please resolve the merge conflicts. |
I don't have further review commentary. I remain personally skeptical about increasing the scope of the suggestion here, but we can land this and see what kinds of behaviors result. I.e. I am willing to be more liberal with diagnostics like this, since we can go back and reduce their scope later after we get more data about their impact. @rustbot author |
Fix #104232.