-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
bors
merged 3 commits into
rust-lang:master
from
spastorino:add-more-ergonomic-clone-tests
Apr 10, 2025
Merged
Add more ergonomic clone tests #138628
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
tests/ui/ergonomic-clones/closure/multiple-use-variants.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
tests/ui/ergonomic-clones/closure/multiple-use-variants.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
tests/ui/ergonomic-clones/closure/spawn-thread.edition2018.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
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() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This case is interesting
There was a problem hiding this comment.
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.