Skip to content

Commit 3507f35

Browse files
authored
Rollup merge of #138628 - spastorino:add-more-ergonomic-clone-tests, r=nikomatsakis
Add more ergonomic clone tests I've added some extra tests. r? `@nikomatsakis`
2 parents 2b28e6b + ca4a582 commit 3507f35

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#![feature(ergonomic_clones)]
2+
#![allow(incomplete_features)]
3+
4+
use std::clone::UseCloned;
5+
6+
fn takes_val<T>(_: T) {}
7+
fn takes_ref<'a, T>(_: &'a T) {}
8+
9+
#[derive(Clone)]
10+
struct Inner<'a, T>(&'a T);
11+
12+
impl<'a, T> UseCloned for Inner<'a, T> where T: Clone {}
13+
14+
fn main() {
15+
let v = String::new();
16+
let inner = Inner(&v);
17+
18+
let _ = use || {
19+
takes_ref(inner.0);
20+
takes_val(inner.0)
21+
};
22+
let _ = use || {
23+
takes_ref(inner.0);
24+
takes_val(inner.0);
25+
takes_val(inner.0);
26+
takes_val(inner)
27+
};
28+
let _ = use || {
29+
takes_ref(inner.0);
30+
takes_val(inner.0);
31+
takes_val(inner);
32+
takes_val(inner)
33+
//~^ ERROR: use of moved value: `inner` [E0382]
34+
};
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0382]: use of moved value: `inner`
2+
--> $DIR/multiple-use-variants.rs:32:19
3+
|
4+
LL | takes_val(inner);
5+
| ----- value moved here
6+
LL | takes_val(inner)
7+
| ^^^^^ value used here after move
8+
|
9+
= note: move occurs because `inner` has type `Inner<'_, String>`, which does not implement the `Copy` trait
10+
11+
error: aborting due to 1 previous error
12+
13+
For more information about this error, try `rustc --explain E0382`.
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() {}

Diff for: tests/ui/ergonomic-clones/dotuse/block.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ check-pass
2+
3+
#![feature(ergonomic_clones)]
4+
#![allow(incomplete_features)]
5+
6+
fn use_block_test(x: i32) -> i32 {
7+
let x = { let x = x + 1; x }.use;
8+
x
9+
}
10+
11+
fn main() {}

0 commit comments

Comments
 (0)