Skip to content

Add more ergonomic clone tests #138628

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

Merged
merged 3 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions tests/ui/ergonomic-clones/closure/multiple-use-variants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#![feature(ergonomic_clones)]
#![allow(incomplete_features)]

use std::clone::UseCloned;

fn takes_val<T>(_: T) {}
fn takes_ref<'a, T>(_: &'a T) {}

#[derive(Clone)]
struct Inner<'a, T>(&'a T);

impl<'a, T> UseCloned for Inner<'a, T> where T: Clone {}

fn main() {
let v = String::new();
let inner = Inner(&v);

let _ = use || {
takes_ref(inner.0);
takes_val(inner.0)
};
let _ = use || {
takes_ref(inner.0);
takes_val(inner.0);
takes_val(inner.0);
takes_val(inner)
};
let _ = use || {
takes_ref(inner.0);
takes_val(inner.0);
takes_val(inner);
takes_val(inner)
//~^ ERROR: use of moved value: `inner` [E0382]
};
}
13 changes: 13 additions & 0 deletions tests/ui/ergonomic-clones/closure/multiple-use-variants.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0382]: use of moved value: `inner`
--> $DIR/multiple-use-variants.rs:32:19
|
LL | takes_val(inner);
| ----- value moved here
LL | takes_val(inner)
| ^^^^^ value used here after move
|
= note: move occurs because `inner` has type `Inner<'_, String>`, which does not implement the `Copy` trait

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0382`.
28 changes: 28 additions & 0 deletions tests/ui/ergonomic-clones/closure/spawn-thread.edition2018.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
error[E0382]: use of moved value: `x`
--> $DIR/spawn-thread.rs:15:42
|
LL | let x = (Arc::new("foo".to_owned()), Arc::new(vec![1, 2, 3]), Arc::new(1));
| - move occurs because `x` has type `(Arc<String>, Arc<Vec<i32>>, Arc<i32>)`, which does not implement the `Copy` trait
LL | for _ in 0..10 {
| -------------- inside of this loop
LL | let handler = std::thread::spawn(use || {
| __________________________________________-^^^^^
LL | |
LL | | drop((x.0, x.1, x.2));
| | --- use occurs due to use in closure
LL | | });
| |_________- value moved here, in previous iteration of loop
|
help: consider moving the expression out of the loop so it is only moved once
|
LL ~ let mut value = std::thread::spawn(use || {
LL +
LL + drop((x.0, x.1, x.2));
LL + });
LL ~ for _ in 0..10 {
LL ~ let handler = value;
|

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0382`.
50 changes: 50 additions & 0 deletions tests/ui/ergonomic-clones/closure/spawn-thread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//@ revisions: edition2018 edition2024
//@ [edition2018] edition: 2018
//@ [edition2024] edition: 2024
//@ [edition2024] check-pass

#![feature(ergonomic_clones)]
#![allow(incomplete_features)]

use std::sync::Arc;

fn foo() {
// The type is a tuple and doesn't implement UseCloned
let x = (Arc::new("foo".to_owned()), Arc::new(vec![1, 2, 3]), Arc::new(1));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is interesting

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I noted on Zulip, the problem here is that you this test defaults to Rust 2015. On newer editions, it works fine.

for _ in 0..10 {
let handler = std::thread::spawn(use || {
//[edition2018]~^ ERROR use of moved value: `x` [E0382]
drop((x.0, x.1, x.2));
});
handler.join().unwrap();
}
}

fn bar() {
let x = Arc::new("foo".to_owned());
let y = Arc::new(vec![1, 2, 3]);
let z = Arc::new(1);

for _ in 0..10 {
let handler = std::thread::spawn(use || {
drop((x, y, z));
});
handler.join().unwrap();
}
}

fn baz() {
use std::sync::Arc;
use std::thread;

let five = Arc::new(5);

for _ in 0..10 {
let handler = thread::spawn(use || {
println!("{five:?}");
});
handler.join().unwrap();
}
}

fn main() {}
11 changes: 11 additions & 0 deletions tests/ui/ergonomic-clones/dotuse/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@ check-pass

#![feature(ergonomic_clones)]
#![allow(incomplete_features)]

fn use_block_test(x: i32) -> i32 {
let x = { let x = x + 1; x }.use;
x
}

fn main() {}
Loading