Skip to content

Commit abceb21

Browse files
Rollup merge of #166127 - JonathanBrouwer:param2, r=Kivooeo
Add tests from failed attempt at implementing `hir::Param` move During the implementation of #163758, we found a lot of bugs this PR introduced during review. I no longer believe we should merge this PR, but lets add regression tests for the bugs! r? @Kivooeo (or someone else) cc @ada4a for the clippy changes but I think those are trivial so 🤷 Part of #164357
2 parents b47e4ab + 07c0c12 commit abceb21

15 files changed

Lines changed: 315 additions & 2 deletions

‎src/tools/clippy/tests/ui/large_types_passed_by_value.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,14 @@ fn foo(x: LargeAndCopy) {
7171
todo!();
7272
}
7373

74+
#[derive(Clone, Copy)]
75+
struct Big([u64; 1024]);
76+
77+
trait Trait {
78+
fn named(x: Big);
79+
//~^ large_types_passed_by_value
80+
fn unnamed(_: Big);
81+
//~^ large_types_passed_by_value
82+
}
83+
7484
fn main() {}

‎src/tools/clippy/tests/ui/large_types_passed_by_value.stderr‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,17 @@ error: this argument (N byte) is passed by value, but might be more efficient if
4949
LL | fn foo(x: LargeAndCopy) {
5050
| ^^^^^^^^^^^^ help: consider passing by reference instead: `&LargeAndCopy`
5151

52-
error: aborting due to 8 previous errors
52+
error: this argument (N byte) is passed by value, but might be more efficient if passed by reference (limit: N byte)
53+
--> tests/ui/large_types_passed_by_value.rs:78:17
54+
|
55+
LL | fn named(x: Big);
56+
| ^^^ help: consider passing by reference instead: `&Big`
57+
58+
error: this argument (N byte) is passed by value, but might be more efficient if passed by reference (limit: N byte)
59+
--> tests/ui/large_types_passed_by_value.rs:80:19
60+
|
61+
LL | fn unnamed(_: Big);
62+
| ^^^ help: consider passing by reference instead: `&Big`
63+
64+
error: aborting due to 10 previous errors
5365

‎src/tools/clippy/tests/ui/shadow.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,19 @@ fn issue14377() {
177177
S { c, d } = S { c: 1, d: 2 };
178178
}
179179

180+
fn plain_same(x: u8) { let x = x; }
181+
//~^ shadow_same
182+
fn tuple_same((x,): (u8,)) { let x = x; }
183+
//~^ shadow_same
184+
185+
fn plain_reuse(x: u8) { let x = x + 1; }
186+
//~^ shadow_reuse
187+
fn tuple_reuse((x,): (u8,)) { let x = x + 1; }
188+
//~^ shadow_reuse
189+
190+
fn plain_unrelated(x: u8) { let x = 1u8; }
191+
//~^ shadow_unrelated
192+
fn tuple_unrelated((x,): (u8,)) { let x = 1u8; }
193+
//~^ shadow_unrelated
194+
180195
fn main() {}

‎src/tools/clippy/tests/ui/shadow.stderr‎

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,5 +316,77 @@ note: previous binding is here
316316
LL | fn issue13795(value: Issue13795) {
317317
| ^^^^^
318318

319-
error: aborting due to 26 previous errors
319+
error: `x` is shadowed by itself in `x`
320+
--> tests/ui/shadow.rs:180:28
321+
|
322+
LL | fn plain_same(x: u8) { let x = x; }
323+
| ^
324+
|
325+
note: previous binding is here
326+
--> tests/ui/shadow.rs:180:15
327+
|
328+
LL | fn plain_same(x: u8) { let x = x; }
329+
| ^
330+
331+
error: `x` is shadowed by itself in `x`
332+
--> tests/ui/shadow.rs:182:34
333+
|
334+
LL | fn tuple_same((x,): (u8,)) { let x = x; }
335+
| ^
336+
|
337+
note: previous binding is here
338+
--> tests/ui/shadow.rs:182:16
339+
|
340+
LL | fn tuple_same((x,): (u8,)) { let x = x; }
341+
| ^
342+
343+
error: `x` is shadowed
344+
--> tests/ui/shadow.rs:185:29
345+
|
346+
LL | fn plain_reuse(x: u8) { let x = x + 1; }
347+
| ^
348+
|
349+
note: previous binding is here
350+
--> tests/ui/shadow.rs:185:16
351+
|
352+
LL | fn plain_reuse(x: u8) { let x = x + 1; }
353+
| ^
354+
355+
error: `x` is shadowed
356+
--> tests/ui/shadow.rs:187:35
357+
|
358+
LL | fn tuple_reuse((x,): (u8,)) { let x = x + 1; }
359+
| ^
360+
|
361+
note: previous binding is here
362+
--> tests/ui/shadow.rs:187:17
363+
|
364+
LL | fn tuple_reuse((x,): (u8,)) { let x = x + 1; }
365+
| ^
366+
367+
error: `x` shadows a previous, unrelated binding
368+
--> tests/ui/shadow.rs:190:33
369+
|
370+
LL | fn plain_unrelated(x: u8) { let x = 1u8; }
371+
| ^
372+
|
373+
note: previous binding is here
374+
--> tests/ui/shadow.rs:190:20
375+
|
376+
LL | fn plain_unrelated(x: u8) { let x = 1u8; }
377+
| ^
378+
379+
error: `x` shadows a previous, unrelated binding
380+
--> tests/ui/shadow.rs:192:39
381+
|
382+
LL | fn tuple_unrelated((x,): (u8,)) { let x = 1u8; }
383+
| ^
384+
|
385+
note: previous binding is here
386+
--> tests/ui/shadow.rs:192:21
387+
|
388+
LL | fn tuple_unrelated((x,): (u8,)) { let x = 1u8; }
389+
| ^
390+
391+
error: aborting due to 32 previous errors
320392

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Checks whether borrow checking visits constants correctly
2+
3+
struct NonCopy(u8);
4+
5+
const C: () = {
6+
let &moved = &NonCopy(0);
7+
//~^ ERROR cannot move out of a shared reference
8+
};
9+
10+
fn main() { }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0507]: cannot move out of a shared reference
2+
--> $DIR/const-cannot-move-shared-ref.rs:6:18
3+
|
4+
LL | let &moved = &NonCopy(0);
5+
| ----- ^^^^^^^^^^^
6+
| |
7+
| data moved here because `moved` has type `NonCopy`, which does not implement the `Copy` trait
8+
|
9+
help: consider removing the borrow
10+
|
11+
LL - let &moved = &NonCopy(0);
12+
LL + let moved = &NonCopy(0);
13+
|
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0507`.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This test checks that the span of c-variadics is handled correctly when creating obligations
2+
3+
struct NeedsCopy<T: Copy>(T);
4+
5+
extern "C" {
6+
fn f1(...) -> NeedsCopy<String>;
7+
//~^ ERROR the trait bound `String: Copy` is not satisfied
8+
fn f2(x: i32, ...) -> NeedsCopy<String>;
9+
//~^ ERROR the trait bound `String: Copy` is not satisfied
10+
fn f3(..., x: i32) -> NeedsCopy<String>;
11+
//~^ ERROR `...` must be the last argument of a C-variadic function
12+
//~| ERROR the trait bound `String: Copy` is not satisfied
13+
}
14+
15+
fn main() { }
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error: `...` must be the last argument of a C-variadic function
2+
--> $DIR/obligation-span.rs:10:11
3+
|
4+
LL | fn f3(..., x: i32) -> NeedsCopy<String>;
5+
| ^^^
6+
7+
error[E0277]: the trait bound `String: Copy` is not satisfied
8+
--> $DIR/obligation-span.rs:6:19
9+
|
10+
LL | fn f1(...) -> NeedsCopy<String>;
11+
| ^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
12+
|
13+
note: required by a bound in `NeedsCopy`
14+
--> $DIR/obligation-span.rs:3:21
15+
|
16+
LL | struct NeedsCopy<T: Copy>(T);
17+
| ^^^^ required by this bound in `NeedsCopy`
18+
19+
error[E0277]: the trait bound `String: Copy` is not satisfied
20+
--> $DIR/obligation-span.rs:8:27
21+
|
22+
LL | fn f2(x: i32, ...) -> NeedsCopy<String>;
23+
| ^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
24+
|
25+
note: required by a bound in `NeedsCopy`
26+
--> $DIR/obligation-span.rs:3:21
27+
|
28+
LL | struct NeedsCopy<T: Copy>(T);
29+
| ^^^^ required by this bound in `NeedsCopy`
30+
31+
error[E0277]: the trait bound `String: Copy` is not satisfied
32+
--> $DIR/obligation-span.rs:10:27
33+
|
34+
LL | fn f3(..., x: i32) -> NeedsCopy<String>;
35+
| ^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
36+
|
37+
note: required by a bound in `NeedsCopy`
38+
--> $DIR/obligation-span.rs:3:21
39+
|
40+
LL | struct NeedsCopy<T: Copy>(T);
41+
| ^^^^ required by this bound in `NeedsCopy`
42+
43+
error: aborting due to 4 previous errors
44+
45+
For more information about this error, try `rustc --explain E0277`.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Check whether the borrow checker runs in guard patterns
2+
3+
#![feature(guard_patterns)]
4+
5+
struct NonCopy(u8);
6+
7+
pub fn probe(
8+
(_ if const {
9+
let x = NonCopy(0);
10+
let y = x;
11+
let z = x;
12+
//~^ ERROR use of moved value: `x` [E0382]
13+
true
14+
}): u8,
15+
) {}
16+
17+
fn main() {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0382]: use of moved value: `x`
2+
--> $DIR/borrow-check.rs:11:13
3+
|
4+
LL | let x = NonCopy(0);
5+
| - move occurs because `x` has type `NonCopy`, which does not implement the `Copy` trait
6+
LL | let y = x;
7+
| - value moved here
8+
LL | let z = x;
9+
| ^ value used here after move
10+
|
11+
note: if `NonCopy` implemented `Clone`, you could clone the value
12+
--> $DIR/borrow-check.rs:5:1
13+
|
14+
LL | struct NonCopy(u8);
15+
| ^^^^^^^^^^^^^^ consider implementing `Clone` for this type
16+
...
17+
LL | let y = x;
18+
| - you could clone this value
19+
20+
error: aborting due to 1 previous error
21+
22+
For more information about this error, try `rustc --explain E0382`.

0 commit comments

Comments
 (0)