Skip to content

Commit 163c9b3

Browse files
committed
Specialize diagnostic for impl without Trait
1 parent c9e8d0b commit 163c9b3

File tree

6 files changed

+16
-13
lines changed

6 files changed

+16
-13
lines changed

compiler/rustc_lint/messages.ftl

+2-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,8 @@ lint_non_local_definitions_impl = non-local `impl` definition, `impl` blocks sho
446446
[one] `{$body_name}`
447447
*[other] `{$body_name}` and up {$depth} bodies
448448
}
449-
.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`
449+
.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
450+
.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`
450451
.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
451452
.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
452453
.const_anon = use a const-anon item to suppress this lint

compiler/rustc_lint/src/lints.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ pub enum NonLocalDefinitionsDiag {
13401340
body_name: String,
13411341
cargo_update: Option<NonLocalDefinitionsCargoUpdateNote>,
13421342
const_anon: Option<Option<Span>>,
1343-
bounds: Option<()>,
1343+
has_trait: bool,
13441344
},
13451345
MacroRules {
13461346
depth: u32,
@@ -1361,17 +1361,19 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
13611361
body_name,
13621362
cargo_update,
13631363
const_anon,
1364-
bounds,
1364+
has_trait,
13651365
} => {
13661366
diag.arg("depth", depth);
13671367
diag.arg("body_kind_descr", body_kind_descr);
13681368
diag.arg("body_name", body_name);
13691369

13701370
diag.help(fluent::lint_help);
1371-
if let Some(()) = bounds {
1371+
if has_trait {
13721372
diag.note(fluent::lint_bounds);
1373+
diag.note(fluent::lint_with_trait);
1374+
} else {
1375+
diag.note(fluent::lint_without_trait);
13731376
}
1374-
diag.note(fluent::lint_non_local);
13751377

13761378
if let Some(cargo_update) = cargo_update {
13771379
diag.subdiagnostic(&diag.dcx, cargo_update);

compiler/rustc_lint/src/non_local_def.rs

+1-1
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

+3-3
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

+3-3
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

+1-1
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)