Skip to content

Commit 2aad418

Browse files
committed
Adjust #[macro_export]/doctest help suggestion for non_local_defs lint
1 parent e27af29 commit 2aad418

File tree

6 files changed

+49
-4
lines changed

6 files changed

+49
-4
lines changed

compiler/rustc_lint/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@ lint_non_local_definitions_macro_rules = non-local `macro_rules!` definition, th
456456
[one] `{$body_name}`
457457
*[other] `{$body_name}` and up {$depth} bodies
458458
}
459+
.help_doctest =
460+
remove the `#[macro_export]` or make this doc-test a standalone test with it's own `fn main() {"{"} ... {"}"}`
459461
.non_local = a `macro_rules!` definition is non-local if it is nested inside an item and has a `#[macro_export]` attribute
460462
.exception = one exception to the rule are anon-const (`const _: () = {"{"} ... {"}"}`) at top-level module
461463

compiler/rustc_lint/src/lints.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1350,14 +1350,18 @@ pub enum NonLocalDefinitionsDiag {
13501350
const_anon: Option<Span>,
13511351
},
13521352
#[diag(lint_non_local_definitions_macro_rules)]
1353-
#[help]
1354-
#[note(lint_non_local)]
1355-
#[note(lint_exception)]
1356-
#[note(lint_non_local_definitions_deprecation)]
13571353
MacroRules {
13581354
depth: u32,
13591355
body_kind_descr: &'static str,
13601356
body_name: String,
1357+
#[help]
1358+
help: Option<()>,
1359+
#[help(lint_help_doctest)]
1360+
doctest_help: Option<()>,
1361+
#[note(lint_non_local)]
1362+
#[note(lint_exception)]
1363+
#[note(lint_non_local_definitions_deprecation)]
1364+
notes: (),
13611365
#[subdiagnostic]
13621366
cargo_update: Option<NonLocalDefinitionsCargoUpdateNote>,
13631367
},

compiler/rustc_lint/src/non_local_def.rs

+9
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
232232
ItemKind::Macro(_macro, MacroKind::Bang)
233233
if cx.tcx.has_attr(item.owner_id.def_id, sym::macro_export) =>
234234
{
235+
// this condition is very much a hack but it's the best we can do for now
236+
let is_at_toplevel_doctest = self.body_depth == 2
237+
&& parent_opt_item_name
238+
.map(|s| s.as_str().starts_with("_doctest"))
239+
.unwrap_or_default();
240+
235241
cx.emit_span_lint(
236242
NON_LOCAL_DEFINITIONS,
237243
item.span,
@@ -242,6 +248,9 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
242248
.map(|s| s.to_ident_string())
243249
.unwrap_or_else(|| "<unnameable>".to_string()),
244250
cargo_update: cargo_update(),
251+
help: (!is_at_toplevel_doctest).then_some(()),
252+
doctest_help: is_at_toplevel_doctest.then_some(()),
253+
notes: (),
245254
},
246255
)
247256
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ check-pass
2+
//@ compile-flags:--test --test-args --test-threads=1 --nocapture -Zunstable-options
3+
//@ normalize-stdout-test: "tests/rustdoc-ui/doctest" -> "$$DIR"
4+
//@ normalize-stderr-test: "tests/rustdoc-ui/doctest" -> "$$DIR"
5+
//@ normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
6+
7+
//! ```
8+
//! #[macro_export]
9+
//! macro_rules! a_macro { () => {} }
10+
//! ```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: non-local `macro_rules!` definition, they should be avoided as they go against expectation
2+
--> $DIR/non_local_defs.rs:9:1
3+
|
4+
LL | macro_rules! a_macro { () => {} }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: remove the `#[macro_export]` or make this doc-test a standalone test with it's own `fn main() { ... }`
8+
= note: a `macro_rules!` definition is non-local if it is nested inside an item and has a `#[macro_export]` attribute
9+
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module
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>
11+
= note: `#[warn(non_local_definitions)]` on by default
12+
13+
warning: 1 warning emitted
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
running 1 test
3+
test $DIR/non_local_defs.rs - (line 7) ... ok
4+
5+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
6+

0 commit comments

Comments
 (0)