Skip to content

Commit ca4a582

Browse files
committed
Add spawn thread test
1 parent c717c59 commit ca4a582

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0382]: use of moved value: `x`
2+
--> $DIR/spawn-thread.rs:15:42
3+
|
4+
LL | let x = (Arc::new("foo".to_owned()), Arc::new(vec![1, 2, 3]), Arc::new(1));
5+
| - move occurs because `x` has type `(Arc<String>, Arc<Vec<i32>>, Arc<i32>)`, which does not implement the `Copy` trait
6+
LL | for _ in 0..10 {
7+
| -------------- inside of this loop
8+
LL | let handler = std::thread::spawn(use || {
9+
| __________________________________________-^^^^^
10+
LL | |
11+
LL | | drop((x.0, x.1, x.2));
12+
| | --- use occurs due to use in closure
13+
LL | | });
14+
| |_________- value moved here, in previous iteration of loop
15+
|
16+
help: consider moving the expression out of the loop so it is only moved once
17+
|
18+
LL ~ let mut value = std::thread::spawn(use || {
19+
LL +
20+
LL + drop((x.0, x.1, x.2));
21+
LL + });
22+
LL ~ for _ in 0..10 {
23+
LL ~ let handler = value;
24+
|
25+
26+
error: aborting due to 1 previous error
27+
28+
For more information about this error, try `rustc --explain E0382`.

Diff for: tests/ui/ergonomic-clones/closure/spawn-thread.rs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//@ revisions: edition2018 edition2024
2+
//@ [edition2018] edition: 2018
3+
//@ [edition2024] edition: 2024
4+
//@ [edition2024] check-pass
5+
6+
#![feature(ergonomic_clones)]
7+
#![allow(incomplete_features)]
8+
9+
use std::sync::Arc;
10+
11+
fn foo() {
12+
// The type is a tuple and doesn't implement UseCloned
13+
let x = (Arc::new("foo".to_owned()), Arc::new(vec![1, 2, 3]), Arc::new(1));
14+
for _ in 0..10 {
15+
let handler = std::thread::spawn(use || {
16+
//[edition2018]~^ ERROR use of moved value: `x` [E0382]
17+
drop((x.0, x.1, x.2));
18+
});
19+
handler.join().unwrap();
20+
}
21+
}
22+
23+
fn bar() {
24+
let x = Arc::new("foo".to_owned());
25+
let y = Arc::new(vec![1, 2, 3]);
26+
let z = Arc::new(1);
27+
28+
for _ in 0..10 {
29+
let handler = std::thread::spawn(use || {
30+
drop((x, y, z));
31+
});
32+
handler.join().unwrap();
33+
}
34+
}
35+
36+
fn baz() {
37+
use std::sync::Arc;
38+
use std::thread;
39+
40+
let five = Arc::new(5);
41+
42+
for _ in 0..10 {
43+
let handler = thread::spawn(use || {
44+
println!("{five:?}");
45+
});
46+
handler.join().unwrap();
47+
}
48+
}
49+
50+
fn main() {}

0 commit comments

Comments
 (0)