Skip to content

Commit ec7bf5b

Browse files
committed
Use span of the impl def and not the impl block
1 parent 0fde35b commit ec7bf5b

File tree

9 files changed

+79
-180
lines changed

9 files changed

+79
-180
lines changed

compiler/rustc_lint/src/non_local_def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
219219

220220
cx.emit_span_lint(
221221
NON_LOCAL_DEFINITIONS,
222-
item.span,
222+
item.span.shrink_to_lo().to(impl_.self_ty.span),
223223
NonLocalDefinitionsDiag::Impl {
224224
depth: self.body_depth,
225225
body_kind_descr: cx.tcx.def_kind_descr(parent_def_kind, parent),

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | const Z: () = {
55
| - help: use a const-anon item to suppress this lint: `_`
66
...
77
LL | impl Uto for &Test {}
8-
| ^^^^^^^^^^^^^^^^^^^^^
8+
| ^^^^^^^^^^^^^^^^^^
99
|
1010
= help: move this `impl` block and all the necessary types/traits outside the of the current constant `Z`
1111
= 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
@@ -18,7 +18,7 @@ warning: non-local `impl` definition, `impl` blocks should be written at the sam
1818
--> $DIR/consts.rs:24:5
1919
|
2020
LL | impl Uto2 for Test {}
21-
| ^^^^^^^^^^^^^^^^^^^^^
21+
| ^^^^^^^^^^^^^^^^^^
2222
|
2323
= help: move this `impl` block and all the necessary types/traits outside the of the current static `A`
2424
= 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
@@ -30,7 +30,7 @@ warning: non-local `impl` definition, `impl` blocks should be written at the sam
3030
--> $DIR/consts.rs:32:5
3131
|
3232
LL | impl Uto3 for Test {}
33-
| ^^^^^^^^^^^^^^^^^^^^^
33+
| ^^^^^^^^^^^^^^^^^^
3434
|
3535
= help: move this `impl` block and all the necessary types/traits outside the of the current constant `B`
3636
= 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
@@ -41,11 +41,8 @@ LL | impl Uto3 for Test {}
4141
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
4242
--> $DIR/consts.rs:43:5
4343
|
44-
LL | / impl Test {
45-
LL | |
46-
LL | | fn foo() {}
47-
LL | | }
48-
| |_____^
44+
LL | impl Test {
45+
| ^^^^^^^^^
4946
|
5047
= help: move this `impl` block and all the necessary types/traits outside the of the current function `main`
5148
= note: methods and assoc const are still usable outside the current expression, only `impl Local` and `impl dyn Local` are local and only if the `Local` type is at the same nesting as the `impl` block
@@ -54,11 +51,8 @@ LL | | }
5451
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
5552
--> $DIR/consts.rs:50:9
5653
|
57-
LL | / impl Test {
58-
LL | |
59-
LL | | fn hoo() {}
60-
LL | | }
61-
| |_________^
54+
LL | impl Test {
55+
| ^^^^^^^^^
6256
|
6357
= help: move this `impl` block and all the necessary types/traits outside the of the current inline constant `<unnameable>` and up 2 bodies
6458
= note: methods and assoc const are still usable outside the current expression, only `impl Local` and `impl dyn Local` are local and only if the `Local` type is at the same nesting as the `impl` block
@@ -67,11 +61,8 @@ LL | | }
6761
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
6862
--> $DIR/consts.rs:59:9
6963
|
70-
LL | / impl Test {
71-
LL | |
72-
LL | | fn foo2() {}
73-
LL | | }
74-
| |_________^
64+
LL | impl Test {
65+
| ^^^^^^^^^
7566
|
7667
= help: move this `impl` block and all the necessary types/traits outside the of the current constant `_` and up 2 bodies
7768
= note: methods and assoc const are still usable outside the current expression, only `impl Local` and `impl dyn Local` are local and only if the `Local` type is at the same nesting as the `impl` block
@@ -82,7 +73,7 @@ warning: non-local `impl` definition, `impl` blocks should be written at the sam
8273
--> $DIR/consts.rs:72:9
8374
|
8475
LL | impl Uto9 for Test {}
85-
| ^^^^^^^^^^^^^^^^^^^^^
76+
| ^^^^^^^^^^^^^^^^^^
8677
|
8778
= help: move this `impl` block and all the necessary types/traits outside the of the current closure `<unnameable>` and up 2 bodies
8879
= 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
@@ -93,7 +84,7 @@ warning: non-local `impl` definition, `impl` blocks should be written at the sam
9384
--> $DIR/consts.rs:79:9
9485
|
9586
LL | impl Uto10 for Test {}
96-
| ^^^^^^^^^^^^^^^^^^^^^^
87+
| ^^^^^^^^^^^^^^^^^^^
9788
|
9889
= help: move this `impl` block and all the necessary types/traits outside the of the current constant expression `<unnameable>` and up 2 bodies
9990
= 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

tests/ui/lint/non-local-defs/exhaustive-trait.stderr

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
22
--> $DIR/exhaustive-trait.rs:7:5
33
|
4-
LL | / impl PartialEq<()> for Dog {
5-
LL | |
6-
LL | | fn eq(&self, _: &()) -> bool {
7-
LL | | todo!()
8-
LL | | }
9-
LL | | }
10-
| |_____^
4+
LL | impl PartialEq<()> for Dog {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
116
|
127
= help: move this `impl` block and all the necessary types/traits outside the of the current function `main`
138
= 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
@@ -18,13 +13,8 @@ LL | | }
1813
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
1914
--> $DIR/exhaustive-trait.rs:14:5
2015
|
21-
LL | / impl PartialEq<()> for &Dog {
22-
LL | |
23-
LL | | fn eq(&self, _: &()) -> bool {
24-
LL | | todo!()
25-
LL | | }
26-
LL | | }
27-
| |_____^
16+
LL | impl PartialEq<()> for &Dog {
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2818
|
2919
= help: move this `impl` block and all the necessary types/traits outside the of the current function `main`
3020
= 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
@@ -34,13 +24,8 @@ LL | | }
3424
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
3525
--> $DIR/exhaustive-trait.rs:21:5
3626
|
37-
LL | / impl PartialEq<Dog> for () {
38-
LL | |
39-
LL | | fn eq(&self, _: &Dog) -> bool {
40-
LL | | todo!()
41-
LL | | }
42-
LL | | }
43-
| |_____^
27+
LL | impl PartialEq<Dog> for () {
28+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
4429
|
4530
= help: move this `impl` block and all the necessary types/traits outside the of the current function `main`
4631
= 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
@@ -50,13 +35,8 @@ LL | | }
5035
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
5136
--> $DIR/exhaustive-trait.rs:28:5
5237
|
53-
LL | / impl PartialEq<&Dog> for () {
54-
LL | |
55-
LL | | fn eq(&self, _: &&Dog) -> bool {
56-
LL | | todo!()
57-
LL | | }
58-
LL | | }
59-
| |_____^
38+
LL | impl PartialEq<&Dog> for () {
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
6040
|
6141
= help: move this `impl` block and all the necessary types/traits outside the of the current function `main`
6242
= 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
@@ -66,13 +46,8 @@ LL | | }
6646
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
6747
--> $DIR/exhaustive-trait.rs:35:5
6848
|
69-
LL | / impl PartialEq<Dog> for &Dog {
70-
LL | |
71-
LL | | fn eq(&self, _: &Dog) -> bool {
72-
LL | | todo!()
73-
LL | | }
74-
LL | | }
75-
| |_____^
49+
LL | impl PartialEq<Dog> for &Dog {
50+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7651
|
7752
= help: move this `impl` block and all the necessary types/traits outside the of the current function `main`
7853
= 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
@@ -82,13 +57,8 @@ LL | | }
8257
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
8358
--> $DIR/exhaustive-trait.rs:42:5
8459
|
85-
LL | / impl PartialEq<&Dog> for &Dog {
86-
LL | |
87-
LL | | fn eq(&self, _: &&Dog) -> bool {
88-
LL | | todo!()
89-
LL | | }
90-
LL | | }
91-
| |_____^
60+
LL | impl PartialEq<&Dog> for &Dog {
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9262
|
9363
= help: move this `impl` block and all the necessary types/traits outside the of the current function `main`
9464
= 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

0 commit comments

Comments
 (0)