Skip to content

Commit 3e8c6e5

Browse files
authored
Rollup merge of rust-lang#125722 - Urgau:non_local_defs-macro-to-change, r=estebank
Indicate in `non_local_defs` lint that the macro needs to change This PR adds a note to indicate that the macro needs to change in the `non_local_definitions` lint output. Address rust-lang#125089 (comment) Fixes rust-lang#125681 r? ``@estebank``
2 parents a61039f + b5d4867 commit 3e8c6e5

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

compiler/rustc_lint/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ lint_non_local_definitions_impl = non-local `impl` definition, `impl` blocks sho
550550
.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
551551
.exception = items in an anonymous const item (`const _: () = {"{"} ... {"}"}`) are treated as in the same scope as the anonymous const's declaration
552552
.const_anon = use a const-anon item to suppress this lint
553+
.macro_to_change = the {$macro_kind} `{$macro_to_change}` defines the non-local `impl`, and may need to be changed
553554
554555
lint_non_local_definitions_impl_move_help =
555556
move the `impl` block outside of this {$body_kind_descr} {$depth ->

compiler/rustc_lint/src/lints.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,7 @@ pub enum NonLocalDefinitionsDiag {
13621362
has_trait: bool,
13631363
self_ty_str: String,
13641364
of_trait_str: Option<String>,
1365+
macro_to_change: Option<(String, &'static str)>,
13651366
},
13661367
MacroRules {
13671368
depth: u32,
@@ -1387,6 +1388,7 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
13871388
has_trait,
13881389
self_ty_str,
13891390
of_trait_str,
1391+
macro_to_change,
13901392
} => {
13911393
diag.primary_message(fluent::lint_non_local_definitions_impl);
13921394
diag.arg("depth", depth);
@@ -1397,6 +1399,15 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
13971399
diag.arg("of_trait_str", of_trait_str);
13981400
}
13991401

1402+
if let Some((macro_to_change, macro_kind)) = macro_to_change {
1403+
diag.arg("macro_to_change", macro_to_change);
1404+
diag.arg("macro_kind", macro_kind);
1405+
diag.note(fluent::lint_macro_to_change);
1406+
}
1407+
if let Some(cargo_update) = cargo_update {
1408+
diag.subdiagnostic(&diag.dcx, cargo_update);
1409+
}
1410+
14001411
if has_trait {
14011412
diag.note(fluent::lint_bounds);
14021413
diag.note(fluent::lint_with_trait);
@@ -1422,9 +1433,6 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
14221433
);
14231434
}
14241435

1425-
if let Some(cargo_update) = cargo_update {
1426-
diag.subdiagnostic(&diag.dcx, cargo_update);
1427-
}
14281436
if let Some(const_anon) = const_anon {
14291437
diag.note(fluent::lint_exception);
14301438
if let Some(const_anon) = const_anon {

compiler/rustc_lint/src/non_local_def.rs

+8
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
258258
Some((cx.tcx.def_span(parent), may_move))
259259
};
260260

261+
let macro_to_change =
262+
if let ExpnKind::Macro(kind, name) = item.span.ctxt().outer_expn_data().kind {
263+
Some((name.to_string(), kind.descr()))
264+
} else {
265+
None
266+
};
267+
261268
cx.emit_span_lint(
262269
NON_LOCAL_DEFINITIONS,
263270
ms,
@@ -274,6 +281,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
274281
move_to,
275282
may_remove,
276283
has_trait: impl_.of_trait.is_some(),
284+
macro_to_change,
277285
},
278286
)
279287
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ LL | non_local_macro::non_local_impl!(LocalStruct);
88
| `Debug` is not local
99
| move the `impl` block outside of this constant `_IMPL_DEBUG`
1010
|
11+
= note: the macro `non_local_macro::non_local_impl` defines the non-local `impl`, and may need to be changed
12+
= 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`
1113
= 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
1214
= note: an `impl` is never scoped, even when it is nested inside an item, as it may impact type checking outside of that item, which can be the case if neither the trait or the self type are at the same nesting level as the `impl`
13-
= 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`
1415
= note: items in an anonymous const item (`const _: () = { ... }`) are treated as in the same scope as the anonymous const's declaration
1516
= 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>
1617
= note: `#[warn(non_local_definitions)]` on by default

tests/ui/lint/non-local-defs/inside-macro_rules.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ LL | impl MacroTrait for OutsideStruct {}
1212
LL | m!();
1313
| ---- in this macro invocation
1414
|
15+
= note: the macro `m` defines the non-local `impl`, and may need to be changed
1516
= 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
1617
= note: an `impl` is never scoped, even when it is nested inside an item, as it may impact type checking outside of that item, which can be the case if neither the trait or the self type are at the same nesting level as the `impl`
1718
= 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>

0 commit comments

Comments
 (0)