diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index bcd9e092a310f..aff10c4320fe1 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -111,6 +111,7 @@ #![feature(core_intrinsics)] #![feature(deprecated_suggestion)] #![feature(deref_pure_trait)] +#![feature(diagnostic_on_move)] #![feature(dispatch_from_dyn)] #![feature(ergonomic_clones)] #![feature(error_generic_member_access)] diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index b3e49af868b8c..af1eaf2015e9d 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -261,6 +261,11 @@ macro_rules! acquire { #[rustc_diagnostic_item = "Arc"] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_insignificant_dtor] +#[diagnostic::on_move( + message = "the type `{Self}` does not implement `Copy`", + label = "this move could be avoided by cloning the original `{Self}`, which is inexpensive", + note = "consider using `Arc::clone`" +)] pub struct Arc< T: ?Sized, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, diff --git a/tests/ui/moves/arc-consumed-in-looped-closure.rs b/tests/ui/moves/arc-consumed-in-looped-closure.rs index 8700c78508476..6016850838d3c 100644 --- a/tests/ui/moves/arc-consumed-in-looped-closure.rs +++ b/tests/ui/moves/arc-consumed-in-looped-closure.rs @@ -19,7 +19,7 @@ impl ThreadPool { } fn main() { - let results = Arc::new(Mutex::new(Vec::new())); //~ NOTE move occurs because + let results = Arc::new(Mutex::new(Vec::new())); //~ NOTE this move could be avoided by cloning the original `Arc`, which is inexpensive let pool = ThreadPool { workers: vec![], queue: Arc::new(()), @@ -29,6 +29,7 @@ fn main() { // let results = Arc::clone(&results); // Forgot this. pool.execute(move || { //~ ERROR E0382 //~^ NOTE value moved into closure here, in previous iteration of loop + //~| NOTE consider using `Arc::clone` //~| HELP consider cloning the value before moving it into the closure let mut r = results.lock().unwrap(); //~ NOTE use occurs due to use in closure r.push(i); diff --git a/tests/ui/moves/arc-consumed-in-looped-closure.stderr b/tests/ui/moves/arc-consumed-in-looped-closure.stderr index 47d6fd6cbad3b..462eb274ce5af 100644 --- a/tests/ui/moves/arc-consumed-in-looped-closure.stderr +++ b/tests/ui/moves/arc-consumed-in-looped-closure.stderr @@ -1,8 +1,8 @@ -error[E0382]: use of moved value: `results` +error[E0382]: the type `Arc` does not implement `Copy` --> $DIR/arc-consumed-in-looped-closure.rs:30:22 | LL | let results = Arc::new(Mutex::new(Vec::new())); - | ------- move occurs because `results` has type `Arc>>`, which does not implement the `Copy` trait + | ------- this move could be avoided by cloning the original `Arc`, which is inexpensive ... LL | for i in 0..20 { | -------------- inside of this loop @@ -13,12 +13,14 @@ LL | pool.execute(move || { LL | let mut r = results.lock().unwrap(); | ------- use occurs due to use in closure | + = note: consider using `Arc::clone` help: consider cloning the value before moving it into the closure | LL ~ let value = results.clone(); LL ~ pool.execute(move || { LL | LL | +LL | LL ~ let mut r = value.lock().unwrap(); | diff --git a/tests/ui/moves/no-capture-arc.rs b/tests/ui/moves/no-capture-arc.rs index 9c957a4e01b41..eea9858532d4c 100644 --- a/tests/ui/moves/no-capture-arc.rs +++ b/tests/ui/moves/no-capture-arc.rs @@ -9,7 +9,7 @@ fn main() { assert_eq!((*arc_v)[3], 4); }); - assert_eq!((*arc_v)[2], 3); //~ ERROR borrow of moved value: `arc_v` + assert_eq!((*arc_v)[2], 3); //~ ERROR the type `Arc` does not implement `Copy` println!("{:?}", *arc_v); } diff --git a/tests/ui/moves/no-capture-arc.stderr b/tests/ui/moves/no-capture-arc.stderr index 6d4a867fa88d0..e0fce32ba7fc0 100644 --- a/tests/ui/moves/no-capture-arc.stderr +++ b/tests/ui/moves/no-capture-arc.stderr @@ -1,8 +1,8 @@ -error[E0382]: borrow of moved value: `arc_v` +error[E0382]: the type `Arc` does not implement `Copy` --> $DIR/no-capture-arc.rs:12:18 | LL | let arc_v = Arc::new(v); - | ----- move occurs because `arc_v` has type `Arc>`, which does not implement the `Copy` trait + | ----- this move could be avoided by cloning the original `Arc`, which is inexpensive LL | LL | thread::spawn(move|| { | ------ value moved into closure here @@ -12,6 +12,7 @@ LL | assert_eq!((*arc_v)[3], 4); LL | assert_eq!((*arc_v)[2], 3); | ^^^^^ value borrowed here after move | + = note: consider using `Arc::clone` = note: borrow occurs due to deref coercion to `Vec` help: consider cloning the value before moving it into the closure | diff --git a/tests/ui/moves/no-reuse-move-arc.fixed b/tests/ui/moves/no-reuse-move-arc.fixed index a5dac8cc14bf2..c9094cb78a391 100644 --- a/tests/ui/moves/no-reuse-move-arc.fixed +++ b/tests/ui/moves/no-reuse-move-arc.fixed @@ -11,7 +11,7 @@ fn main() { assert_eq!((*value)[3], 4); }); - assert_eq!((*arc_v)[2], 3); //~ ERROR borrow of moved value: `arc_v` + assert_eq!((*arc_v)[2], 3); //~ ERROR the type `Arc` does not implement `Copy` println!("{:?}", *arc_v); } diff --git a/tests/ui/moves/no-reuse-move-arc.rs b/tests/ui/moves/no-reuse-move-arc.rs index 0d67aa56489ce..29452220aebcc 100644 --- a/tests/ui/moves/no-reuse-move-arc.rs +++ b/tests/ui/moves/no-reuse-move-arc.rs @@ -10,7 +10,7 @@ fn main() { assert_eq!((*arc_v)[3], 4); }); - assert_eq!((*arc_v)[2], 3); //~ ERROR borrow of moved value: `arc_v` + assert_eq!((*arc_v)[2], 3); //~ ERROR the type `Arc` does not implement `Copy` println!("{:?}", *arc_v); } diff --git a/tests/ui/moves/no-reuse-move-arc.stderr b/tests/ui/moves/no-reuse-move-arc.stderr index aff979af905e4..8f56b32f96a89 100644 --- a/tests/ui/moves/no-reuse-move-arc.stderr +++ b/tests/ui/moves/no-reuse-move-arc.stderr @@ -1,8 +1,8 @@ -error[E0382]: borrow of moved value: `arc_v` +error[E0382]: the type `Arc` does not implement `Copy` --> $DIR/no-reuse-move-arc.rs:13:18 | LL | let arc_v = Arc::new(v); - | ----- move occurs because `arc_v` has type `Arc>`, which does not implement the `Copy` trait + | ----- this move could be avoided by cloning the original `Arc`, which is inexpensive LL | LL | thread::spawn(move|| { | ------ value moved into closure here @@ -12,6 +12,7 @@ LL | assert_eq!((*arc_v)[3], 4); LL | assert_eq!((*arc_v)[2], 3); | ^^^^^ value borrowed here after move | + = note: consider using `Arc::clone` = note: borrow occurs due to deref coercion to `Vec` help: consider cloning the value before moving it into the closure |