Skip to content

Commit c77a6d8

Browse files
committed
Also signal generics that may need to be moved
1 parent 88e61fe commit c77a6d8

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

compiler/rustc_lint/src/non_local_def.rs

+1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
221221
if let Some(of_trait) = &impl_.of_trait {
222222
collector.visit_trait_ref(of_trait);
223223
}
224+
collector.visit_generics(&impl_.generics);
224225

225226
let may_move: Vec<Span> = collector
226227
.paths

tests/ui/lint/non-local-defs/generics.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ help: move this `impl` block outside of the current function `main`
1010
--> $DIR/generics.rs:9:5
1111
|
1212
LL | impl<T: Local> Global for Vec<T> { }
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
| ^^^^^^^^-----^^^^^^^^^^^^^^^^^^^^^^^
14+
| |
15+
| may need to be moved as well
1416
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
1517
= note: `#[warn(non_local_definitions)]` on by default
1618

@@ -26,7 +28,9 @@ help: move this `impl` block outside of the current function `bad`
2628
--> $DIR/generics.rs:20:5
2729
|
2830
LL | impl Uto7 for Test where Local: std::any::Any {}
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^-----^^^^^^^^^^^^^^^^^^
32+
| |
33+
| may need to be moved as well
3034
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
3135

3236
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item

tests/ui/lint/non-local-defs/suggest-moving-inner.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ fn main() {
77
pub struct Type<T>(T);
88
}
99
struct InsideMain;
10+
trait HasFoo {}
1011

11-
impl Trait<InsideMain> for &Vec<below::Type<InsideMain>> {}
12+
impl<T> Trait<InsideMain> for &Vec<below::Type<(InsideMain, T)>>
1213
//~^ WARN non-local `impl` definition
14+
where
15+
T: HasFoo
16+
{}
1317
}

tests/ui/lint/non-local-defs/suggest-moving-inner.stderr

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
2-
--> $DIR/suggest-moving-inner.rs:11:5
2+
--> $DIR/suggest-moving-inner.rs:12:5
33
|
4-
LL | impl Trait<InsideMain> for &Vec<below::Type<InsideMain>> {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
LL | impl<T> Trait<InsideMain> for &Vec<below::Type<(InsideMain, T)>>
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `impl` may be usable in bounds, etc. from outside the expression, which might e.g. make something constructible that previously wasn't, because it's still on a publicly-visible type
88
= note: an `impl` is never scoped, even when it is nested inside an item, as it may impact type checking outside of that item, which can be the case if neither the trait or the self type are at the same nesting level as the `impl`
99
help: move this `impl` block outside of the current function `main`
10-
--> $DIR/suggest-moving-inner.rs:11:5
10+
--> $DIR/suggest-moving-inner.rs:12:5
1111
|
12-
LL | impl Trait<InsideMain> for &Vec<below::Type<InsideMain>> {}
13-
| ^^^^^^^^^^^----------^^^^^^^^^^^-----------^----------^^^^^
14-
| | | |
15-
| | | may need to be moved as well
16-
| | may need to be moved as well
17-
| may need to be moved as well
12+
LL | impl<T> Trait<InsideMain> for &Vec<below::Type<(InsideMain, T)>>
13+
| ^ ---------- ----------- ---------- may need to be moved as well
14+
| | | |
15+
| | | may need to be moved as well
16+
| _____| may need to be moved as well
17+
| |
18+
LL | |
19+
LL | | where
20+
LL | | T: HasFoo
21+
| | ------ may need to be moved as well
22+
LL | | {}
23+
| |______^
1824
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
1925
= note: `#[warn(non_local_definitions)]` on by default
2026

0 commit comments

Comments
 (0)