Skip to content

Commit 4bfd268

Browse files
committed
Restrict const-anon exception diag to relevant places
1 parent 2540ce8 commit 4bfd268

12 files changed

+24
-70
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ lint_non_local_definitions_impl = non-local `impl` definition, `impl` blocks sho
447447
*[other] `{$body_name}` and up {$depth} bodies
448448
}
449449
.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`
450-
.exception = one exception to the rule are anon-const (`const _: () = {"{"} ... {"}"}`) at top-level module and anon-const at the same nesting as the trait or type
450+
.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
451451
.const_anon = use a const-anon item to suppress this lint
452452
453453
lint_non_local_definitions_macro_rules = non-local `macro_rules!` definition, `#[macro_export]` macro should be written at top level module

compiler/rustc_lint/src/lints.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,7 @@ pub enum NonLocalDefinitionsDiag {
13391339
body_kind_descr: &'static str,
13401340
body_name: String,
13411341
cargo_update: Option<NonLocalDefinitionsCargoUpdateNote>,
1342-
const_anon: Option<Span>,
1342+
const_anon: Option<Option<Span>>,
13431343
},
13441344
MacroRules {
13451345
depth: u32,
@@ -1367,20 +1367,23 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
13671367

13681368
diag.help(fluent::lint_help);
13691369
diag.note(fluent::lint_non_local);
1370-
diag.note(fluent::lint_exception);
1371-
diag.note(fluent::lint_non_local_definitions_deprecation);
13721370

13731371
if let Some(cargo_update) = cargo_update {
13741372
diag.subdiagnostic(&diag.dcx, cargo_update);
13751373
}
13761374
if let Some(const_anon) = const_anon {
1377-
diag.span_suggestion(
1378-
const_anon,
1379-
fluent::lint_const_anon,
1380-
"_",
1381-
Applicability::MachineApplicable,
1382-
);
1375+
diag.note(fluent::lint_exception);
1376+
if let Some(const_anon) = const_anon {
1377+
diag.span_suggestion(
1378+
const_anon,
1379+
fluent::lint_const_anon,
1380+
"_",
1381+
Applicability::MachineApplicable,
1382+
);
1383+
}
13831384
}
1385+
1386+
diag.note(fluent::lint_non_local_definitions_deprecation);
13841387
}
13851388
NonLocalDefinitionsDiag::MacroRules {
13861389
depth,

compiler/rustc_lint/src/non_local_def.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
202202
// Get the span of the parent const item ident (if it's a not a const anon).
203203
//
204204
// Used to suggest changing the const item to a const anon.
205-
let span_for_const_anon_suggestion = if self.body_depth == 1
206-
&& parent_def_kind == DefKind::Const
205+
let span_for_const_anon_suggestion = if parent_def_kind == DefKind::Const
207206
&& parent_opt_item_name != Some(kw::Underscore)
208207
&& let Some(parent) = parent.as_local()
209208
&& let Node::Item(item) = cx.tcx.hir_node_by_def_id(parent)
@@ -215,6 +214,9 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
215214
None
216215
};
217216

217+
let const_anon = matches!(parent_def_kind, DefKind::Const | DefKind::Static { .. })
218+
.then_some(span_for_const_anon_suggestion);
219+
218220
cx.emit_span_lint(
219221
NON_LOCAL_DEFINITIONS,
220222
item.span,
@@ -225,7 +227,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
225227
.map(|s| s.to_ident_string())
226228
.unwrap_or_else(|| "<unnameable>".to_string()),
227229
cargo_update: cargo_update(),
228-
const_anon: span_for_const_anon_suggestion,
230+
const_anon,
229231
},
230232
)
231233
}

tests/ui/lint/non-local-defs/cargo-update.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ LL | non_local_macro::non_local_impl!(LocalStruct);
66
|
77
= help: move this `impl` block outside the of the current constant `_IMPL_DEBUG`
88
= 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`
9-
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
10-
= 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>
119
= note: the macro `non_local_macro::non_local_impl` may come from an old version of the `non_local_macro` crate, try updating your dependency with `cargo update -p non_local_macro`
10+
= 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
11+
= 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>
1212
= note: `#[warn(non_local_definitions)]` on by default
1313
= note: this warning originates in the macro `non_local_macro::non_local_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
1414

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | impl Uto for &Test {}
99
|
1010
= help: move this `impl` block outside the of the current constant `Z`
1111
= 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`
12-
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
12+
= 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
1313
= 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>
1414
= note: `#[warn(non_local_definitions)]` on by default
1515

@@ -21,7 +21,7 @@ LL | impl Uto2 for Test {}
2121
|
2222
= help: move this `impl` block outside the of the current static `A`
2323
= 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`
24-
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
24+
= 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
2525
= 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>
2626

2727
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
@@ -32,7 +32,7 @@ LL | impl Uto3 for Test {}
3232
|
3333
= help: move this `impl` block outside the of the current constant `B`
3434
= 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`
35-
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
35+
= 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
3636
= 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>
3737

3838
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
@@ -46,7 +46,6 @@ LL | | }
4646
|
4747
= help: move this `impl` block outside the of the current function `main`
4848
= 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`
49-
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
5049
= 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>
5150

5251
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
@@ -60,7 +59,6 @@ LL | | }
6059
|
6160
= help: move this `impl` block outside the of the current inline constant `<unnameable>` and up 2 bodies
6261
= 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`
63-
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
6462
= 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>
6563

6664
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
@@ -74,7 +72,7 @@ LL | | }
7472
|
7573
= help: move this `impl` block outside the of the current constant `_` and up 2 bodies
7674
= 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: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
75+
= 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
7876
= 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>
7977

8078
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
@@ -85,7 +83,6 @@ LL | impl Uto9 for Test {}
8583
|
8684
= help: move this `impl` block outside the of the current closure `<unnameable>` and up 2 bodies
8785
= 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`
88-
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
8986
= 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>
9087

9188
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
@@ -96,7 +93,6 @@ LL | impl Uto10 for Test {}
9693
|
9794
= help: move this `impl` block outside the of the current constant expression `<unnameable>` and up 2 bodies
9895
= 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`
99-
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
10096
= 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>
10197

10298
warning: 8 warnings emitted

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ LL | | }
1111
|
1212
= help: move this `impl` block outside the of the current function `main`
1313
= 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`
14-
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
1514
= 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>
1615
= note: `#[warn(non_local_definitions)]` on by default
1716

@@ -28,7 +27,6 @@ LL | | }
2827
|
2928
= help: move this `impl` block outside the of the current function `main`
3029
= 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`
31-
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
3230
= 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>
3331

3432
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
@@ -44,7 +42,6 @@ LL | | }
4442
|
4543
= help: move this `impl` block outside the of the current function `main`
4644
= 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`
47-
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
4845
= 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>
4946

5047
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
@@ -60,7 +57,6 @@ LL | | }
6057
|
6158
= help: move this `impl` block outside the of the current function `main`
6259
= 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`
63-
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
6460
= 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>
6561

6662
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
@@ -76,7 +72,6 @@ LL | | }
7672
|
7773
= help: move this `impl` block outside the of the current function `main`
7874
= 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`
79-
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
8075
= 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>
8176

8277
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
@@ -92,7 +87,6 @@ LL | | }
9287
|
9388
= help: move this `impl` block outside the of the current function `main`
9489
= 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`
95-
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
9690
= 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>
9791

9892
warning: 6 warnings emitted

0 commit comments

Comments
 (0)