Skip to content

Commit f8201f0

Browse files
committed
Specialize diagnostic for impl without Trait
1 parent 094fd26 commit f8201f0

File tree

6 files changed

+16
-13
lines changed

6 files changed

+16
-13
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,8 @@ lint_non_local_definitions_impl = non-local `impl` definition, `impl` blocks sho
548548
[one] `{$body_name}`
549549
*[other] `{$body_name}` and up {$depth} bodies
550550
}
551-
.non_local = an `impl` definition is non-local if it is nested inside an item and may impact type checking outside of that item. This can be the case if neither the trait or the self type are at the same nesting level as the `impl`
551+
.without_trait = 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
552+
.with_trait = an `impl` definition is non-local if it is nested inside an item and may impact type checking outside of that item. This can be the case if neither the trait or the self type are at the same nesting level as the `impl`
552553
.bounds = `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
553554
.exception = anon-const (`const _: () = {"{"} ... {"}"}`) at top-level module and anon-const at the same nesting as the trait or type are consider to be transparent regarding the nesting level
554555
.const_anon = use a const-anon item to suppress this lint

compiler/rustc_lint/src/lints.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,7 @@ pub enum NonLocalDefinitionsDiag {
13471347
body_name: String,
13481348
cargo_update: Option<NonLocalDefinitionsCargoUpdateNote>,
13491349
const_anon: Option<Option<Span>>,
1350-
bounds: Option<()>,
1350+
has_trait: bool,
13511351
},
13521352
MacroRules {
13531353
depth: u32,
@@ -1368,17 +1368,19 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
13681368
body_name,
13691369
cargo_update,
13701370
const_anon,
1371-
bounds,
1371+
has_trait,
13721372
} => {
13731373
diag.arg("depth", depth);
13741374
diag.arg("body_kind_descr", body_kind_descr);
13751375
diag.arg("body_name", body_name);
13761376

13771377
diag.help(fluent::lint_help);
1378-
if let Some(()) = bounds {
1378+
if has_trait {
13791379
diag.note(fluent::lint_bounds);
1380+
diag.note(fluent::lint_with_trait);
1381+
} else {
1382+
diag.note(fluent::lint_without_trait);
13801383
}
1381-
diag.note(fluent::lint_non_local);
13821384

13831385
if let Some(cargo_update) = cargo_update {
13841386
diag.subdiagnostic(&diag.dcx, cargo_update);

compiler/rustc_lint/src/non_local_def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
228228
.unwrap_or_else(|| "<unnameable>".to_string()),
229229
cargo_update: cargo_update(),
230230
const_anon,
231-
bounds: impl_.of_trait.map(|_| ()),
231+
has_trait: impl_.of_trait.is_some(),
232232
},
233233
)
234234
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ LL | | }
4848
| |_____^
4949
|
5050
= help: move this `impl` block outside the of the current function `main`
51-
= note: an `impl` definition is non-local if it is nested inside an item and may impact type checking outside of that item. This can be the case if neither the trait or the self type are at the same nesting level as the `impl`
51+
= 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
5252
= 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>
5353

5454
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
@@ -61,7 +61,7 @@ LL | | }
6161
| |_________^
6262
|
6363
= help: move this `impl` block outside the of the current inline constant `<unnameable>` and up 2 bodies
64-
= note: an `impl` definition is non-local if it is nested inside an item and may impact type checking outside of that item. This can be the case if neither the trait or the self type are at the same nesting level as the `impl`
64+
= 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
6565
= 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>
6666

6767
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
@@ -74,7 +74,7 @@ LL | | }
7474
| |_________^
7575
|
7676
= help: move this `impl` block outside the of the current constant `_` and up 2 bodies
77-
= note: an `impl` definition is non-local if it is nested inside an item and may impact type checking outside of that item. This can be the case if neither the trait or the self type are at the same nesting level as the `impl`
77+
= 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
7878
= note: anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type are consider to be transparent regarding the nesting level
7979
= 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>
8080

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | | }
88
| |_____^
99
|
1010
= help: move this `impl` block outside the of the current function `main`
11-
= note: an `impl` definition is non-local if it is nested inside an item and may impact type checking outside of that item. This can be the case if neither the trait or the self type are at the same nesting level as the `impl`
11+
= 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
1212
= 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>
1313
= note: `#[warn(non_local_definitions)]` on by default
1414

@@ -35,7 +35,7 @@ LL | impl dyn Trait {}
3535
| ^^^^^^^^^^^^^^^^^
3636
|
3737
= help: move this `impl` block outside the of the current function `main`
38-
= note: an `impl` definition is non-local if it is nested inside an item and may impact type checking outside of that item. This can be the case if neither the trait or the self type are at the same nesting level as the `impl`
38+
= 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
3939
= 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>
4040

4141
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
@@ -229,7 +229,7 @@ LL | | }
229229
| |_________^
230230
|
231231
= help: move this `impl` block outside the of the current function `inside_inside` and up 2 bodies
232-
= note: an `impl` definition is non-local if it is nested inside an item and may impact type checking outside of that item. This can be the case if neither the trait or the self type are at the same nesting level as the `impl`
232+
= 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
233233
= 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>
234234

235235
warning: 20 warnings emitted

tests/ui/lint/non-local-defs/weird-exprs.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ LL | | }
3131
| |_________^
3232
|
3333
= help: move this `impl` block outside the of the current constant expression `<unnameable>` and up 2 bodies
34-
= note: an `impl` definition is non-local if it is nested inside an item and may impact type checking outside of that item. This can be the case if neither the trait or the self type are at the same nesting level as the `impl`
34+
= 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
3535
= 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>
3636

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

0 commit comments

Comments
 (0)