Skip to content

Commit 6f48a1e

Browse files
committed
non_local_defs: indicate that the macro needs to change
1 parent 5870f1c commit 6f48a1e

File tree

5 files changed

+19
-0
lines changed

5 files changed

+19
-0
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}` needs 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

+8
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,7 @@ pub enum NonLocalDefinitionsDiag {
13551355
has_trait: bool,
13561356
self_ty_str: String,
13571357
of_trait_str: Option<String>,
1358+
macro_to_change: Option<(String, &'static str)>,
13581359
},
13591360
MacroRules {
13601361
depth: u32,
@@ -1380,6 +1381,7 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
13801381
has_trait,
13811382
self_ty_str,
13821383
of_trait_str,
1384+
macro_to_change,
13831385
} => {
13841386
diag.primary_message(fluent::lint_non_local_definitions_impl);
13851387
diag.arg("depth", depth);
@@ -1390,6 +1392,12 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
13901392
diag.arg("of_trait_str", of_trait_str);
13911393
}
13921394

1395+
if let Some((macro_to_change, macro_kind)) = macro_to_change {
1396+
diag.arg("macro_to_change", macro_to_change);
1397+
diag.arg("macro_kind", macro_kind);
1398+
diag.note(fluent::lint_macro_to_change);
1399+
}
1400+
13931401
if has_trait {
13941402
diag.note(fluent::lint_bounds);
13951403
diag.note(fluent::lint_with_trait);

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

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ 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` needs to be changed
1112
= 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
1213
= 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`
1314
= 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`

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` needs 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)