Skip to content

Commit 879efa8

Browse files
committed
Add a test for Adt copy suggestions
1 parent 400d343 commit 879efa8

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
fn duplicate_t<T>(t: T) -> (T, T) {
2+
(t, t) //~ use of moved value: `t`
3+
}
4+
5+
fn duplicate_opt<T>(t: Option<T>) -> (Option<T>, Option<T>) {
6+
(t, t) //~ use of moved value: `t`
7+
}
8+
9+
fn duplicate_tup1<T>(t: (T,)) -> ((T,), (T,)) {
10+
(t, t) //~ use of moved value: `t`
11+
}
12+
13+
fn duplicate_tup2<A, B>(t: (A, B)) -> ((A, B), (A, B)) {
14+
(t, t) //~ use of moved value: `t`
15+
}
16+
17+
fn duplicate_custom<T>(t: S<T>) -> (S<T>, S<T>) {
18+
(t, t) //~ use of moved value: `t`
19+
}
20+
21+
struct S<T>(T);
22+
trait Trait {}
23+
impl<T: Trait + Clone> Clone for S<T> {
24+
fn clone(&self) -> Self {
25+
Self(self.0.clone())
26+
}
27+
}
28+
impl<T: Trait + Copy> Copy for S<T> {}
29+
30+
trait A {}
31+
trait B {}
32+
33+
// Test where bounds are added with different bound placements
34+
fn duplicate_custom_1<T>(t: S<T>) -> (S<T>, S<T>) where {
35+
(t, t) //~ use of moved value: `t`
36+
}
37+
38+
fn duplicate_custom_2<T>(t: S<T>) -> (S<T>, S<T>)
39+
where
40+
T: A,
41+
{
42+
(t, t) //~ use of moved value: `t`
43+
}
44+
45+
fn duplicate_custom_3<T>(t: S<T>) -> (S<T>, S<T>)
46+
where
47+
T: A,
48+
T: B,
49+
{
50+
(t, t) //~ use of moved value: `t`
51+
}
52+
53+
fn duplicate_custom_4<T: A>(t: S<T>) -> (S<T>, S<T>)
54+
where
55+
T: B,
56+
{
57+
(t, t) //~ use of moved value: `t`
58+
}
59+
60+
// `Rc` is not ever `Copy`, we should not suggest adding `T: Copy` constraint
61+
fn duplicate_rc<T>(t: std::rc::Rc<T>) -> (std::rc::Rc<T>, std::rc::Rc<T>) {
62+
(t, t) //~ use of moved value: `t`
63+
}
64+
65+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
error[E0382]: use of moved value: `t`
2+
--> $DIR/use_of_moved_value_copy_suggestions.rs:2:9
3+
|
4+
LL | fn duplicate_t<T>(t: T) -> (T, T) {
5+
| - move occurs because `t` has type `T`, which does not implement the `Copy` trait
6+
LL | (t, t)
7+
| - ^ value used here after move
8+
| |
9+
| value moved here
10+
|
11+
help: consider restricting type parameter `T`
12+
|
13+
LL | fn duplicate_t<T: Copy>(t: T) -> (T, T) {
14+
| ++++++
15+
16+
error[E0382]: use of moved value: `t`
17+
--> $DIR/use_of_moved_value_copy_suggestions.rs:6:9
18+
|
19+
LL | fn duplicate_opt<T>(t: Option<T>) -> (Option<T>, Option<T>) {
20+
| - move occurs because `t` has type `Option<T>`, which does not implement the `Copy` trait
21+
LL | (t, t)
22+
| - ^ value used here after move
23+
| |
24+
| value moved here
25+
|
26+
help: consider restricting type parameter `T`
27+
|
28+
LL | fn duplicate_opt<T: Copy>(t: Option<T>) -> (Option<T>, Option<T>) {
29+
| ++++++
30+
31+
error[E0382]: use of moved value: `t`
32+
--> $DIR/use_of_moved_value_copy_suggestions.rs:10:9
33+
|
34+
LL | fn duplicate_tup1<T>(t: (T,)) -> ((T,), (T,)) {
35+
| - move occurs because `t` has type `(T,)`, which does not implement the `Copy` trait
36+
LL | (t, t)
37+
| - ^ value used here after move
38+
| |
39+
| value moved here
40+
|
41+
help: consider restricting type parameter `T`
42+
|
43+
LL | fn duplicate_tup1<T: Copy>(t: (T,)) -> ((T,), (T,)) {
44+
| ++++++
45+
46+
error[E0382]: use of moved value: `t`
47+
--> $DIR/use_of_moved_value_copy_suggestions.rs:14:9
48+
|
49+
LL | fn duplicate_tup2<A, B>(t: (A, B)) -> ((A, B), (A, B)) {
50+
| - move occurs because `t` has type `(A, B)`, which does not implement the `Copy` trait
51+
LL | (t, t)
52+
| - ^ value used here after move
53+
| |
54+
| value moved here
55+
|
56+
help: consider restricting type parameters
57+
|
58+
LL | fn duplicate_tup2<A: Copy, B: Copy>(t: (A, B)) -> ((A, B), (A, B)) {
59+
| ++++++ ++++++
60+
61+
error[E0382]: use of moved value: `t`
62+
--> $DIR/use_of_moved_value_copy_suggestions.rs:18:9
63+
|
64+
LL | fn duplicate_custom<T>(t: S<T>) -> (S<T>, S<T>) {
65+
| - move occurs because `t` has type `S<T>`, which does not implement the `Copy` trait
66+
LL | (t, t)
67+
| - ^ value used here after move
68+
| |
69+
| value moved here
70+
|
71+
help: consider restricting type parameter `T`
72+
|
73+
LL | fn duplicate_custom<T: Trait + Copy>(t: S<T>) -> (S<T>, S<T>) {
74+
| ++++++++++++++
75+
76+
error[E0382]: use of moved value: `t`
77+
--> $DIR/use_of_moved_value_copy_suggestions.rs:35:9
78+
|
79+
LL | fn duplicate_custom_1<T>(t: S<T>) -> (S<T>, S<T>) where {
80+
| - move occurs because `t` has type `S<T>`, which does not implement the `Copy` trait
81+
LL | (t, t)
82+
| - ^ value used here after move
83+
| |
84+
| value moved here
85+
|
86+
help: consider restricting type parameter `T`
87+
|
88+
LL | fn duplicate_custom_1<T: Trait + Copy>(t: S<T>) -> (S<T>, S<T>) where {
89+
| ++++++++++++++
90+
91+
error[E0382]: use of moved value: `t`
92+
--> $DIR/use_of_moved_value_copy_suggestions.rs:42:9
93+
|
94+
LL | fn duplicate_custom_2<T>(t: S<T>) -> (S<T>, S<T>)
95+
| - move occurs because `t` has type `S<T>`, which does not implement the `Copy` trait
96+
...
97+
LL | (t, t)
98+
| - ^ value used here after move
99+
| |
100+
| value moved here
101+
|
102+
help: consider further restricting this bound
103+
|
104+
LL | T: A + Trait + Copy,
105+
| ++++++++++++++
106+
107+
error[E0382]: use of moved value: `t`
108+
--> $DIR/use_of_moved_value_copy_suggestions.rs:50:9
109+
|
110+
LL | fn duplicate_custom_3<T>(t: S<T>) -> (S<T>, S<T>)
111+
| - move occurs because `t` has type `S<T>`, which does not implement the `Copy` trait
112+
...
113+
LL | (t, t)
114+
| - ^ value used here after move
115+
| |
116+
| value moved here
117+
|
118+
help: consider further restricting type parameter `T`
119+
|
120+
LL | T: B, T: Trait, T: Copy
121+
| ~~~~~~~~~~~~~~~~~~~
122+
123+
error[E0382]: use of moved value: `t`
124+
--> $DIR/use_of_moved_value_copy_suggestions.rs:57:9
125+
|
126+
LL | fn duplicate_custom_4<T: A>(t: S<T>) -> (S<T>, S<T>)
127+
| - move occurs because `t` has type `S<T>`, which does not implement the `Copy` trait
128+
...
129+
LL | (t, t)
130+
| - ^ value used here after move
131+
| |
132+
| value moved here
133+
|
134+
help: consider further restricting this bound
135+
|
136+
LL | T: B + Trait + Copy,
137+
| ++++++++++++++
138+
139+
error[E0382]: use of moved value: `t`
140+
--> $DIR/use_of_moved_value_copy_suggestions.rs:62:9
141+
|
142+
LL | fn duplicate_rc<T>(t: std::rc::Rc<T>) -> (std::rc::Rc<T>, std::rc::Rc<T>) {
143+
| - move occurs because `t` has type `Rc<T>`, which does not implement the `Copy` trait
144+
LL | (t, t)
145+
| - ^ value used here after move
146+
| |
147+
| value moved here
148+
149+
error: aborting due to 10 previous errors
150+
151+
For more information about this error, try `rustc --explain E0382`.

0 commit comments

Comments
 (0)