Skip to content

Commit 2fea7b3

Browse files
committed
Add spawn thread test
1 parent 908b118 commit 2fea7b3

File tree

2 files changed

+73
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)