Skip to content

Commit a48d5a2

Browse files
committed
Restrict const-anon exception diag to relevant places
1 parent 9f41b55 commit a48d5a2

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
@@ -549,7 +549,7 @@ lint_non_local_definitions_impl = non-local `impl` definition, `impl` blocks sho
549549
*[other] `{$body_name}` and up {$depth} bodies
550550
}
551551
.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`
552-
.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
552+
.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
553553
.const_anon = use a const-anon item to suppress this lint
554554
555555
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
@@ -1346,7 +1346,7 @@ pub enum NonLocalDefinitionsDiag {
13461346
body_kind_descr: &'static str,
13471347
body_name: String,
13481348
cargo_update: Option<NonLocalDefinitionsCargoUpdateNote>,
1349-
const_anon: Option<Span>,
1349+
const_anon: Option<Option<Span>>,
13501350
},
13511351
MacroRules {
13521352
depth: u32,
@@ -1374,20 +1374,23 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
13741374

13751375
diag.help(fluent::lint_help);
13761376
diag.note(fluent::lint_non_local);
1377-
diag.note(fluent::lint_exception);
1378-
diag.note(fluent::lint_non_local_definitions_deprecation);
13791377

13801378
if let Some(cargo_update) = cargo_update {
13811379
diag.subdiagnostic(&diag.dcx, cargo_update);
13821380
}
13831381
if let Some(const_anon) = const_anon {
1384-
diag.span_suggestion(
1385-
const_anon,
1386-
fluent::lint_const_anon,
1387-
"_",
1388-
Applicability::MachineApplicable,
1389-
);
1382+
diag.note(fluent::lint_exception);
1383+
if let Some(const_anon) = const_anon {
1384+
diag.span_suggestion(
1385+
const_anon,
1386+
fluent::lint_const_anon,
1387+
"_",
1388+
Applicability::MachineApplicable,
1389+
);
1390+
}
13901391
}
1392+
1393+
diag.note(fluent::lint_non_local_definitions_deprecation);
13911394
}
13921395
NonLocalDefinitionsDiag::MacroRules {
13931396
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)