Skip to content

Commit 42cdc7d

Browse files
authored
Rollup merge of #114413 - CohenArthur:warn-macro-export-decl-macros, r=cjgillot
Warn when #[macro_export] is applied on decl macros The existing code checks if `#[macro_export]` is being applied to an item other than a macro, and warns in that case, but fails to take into account macros 2.0/decl macros, despite the attribute having no effect on these macros. This PR adds a special case for decl macros with the aforementioned attribute, so that the warning is a bit more precise. Instead of just saying "this attribute has no effect", hint towards the fact that decl macros get exported and resolved like regular items. It also removes a `#[macro_export]` attribute which was applied on one of `core`'s decl macros. - core: Remove #[macro_export] from `debug_assert_matches` - check_attrs: Warn when #[macro_export] is used on macros 2.0
2 parents 328e978 + bdf4e3d commit 42cdc7d

File tree

6 files changed

+47
-1
lines changed

6 files changed

+47
-1
lines changed

Diff for: compiler/rustc_passes/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@ passes_link_section =
428428
passes_macro_export =
429429
`#[macro_export]` only has an effect on macro definitions
430430
431+
passes_macro_export_on_decl_macro =
432+
`#[macro_export]` has no effect on declarative macro definitions
433+
.note = declarative macros follow the same exporting rules as regular items
434+
431435
passes_macro_use =
432436
`#[{$name}]` only has an effect on `extern crate` and modules
433437

Diff for: compiler/rustc_passes/src/check_attr.rs

+14
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,20 @@ impl CheckAttrVisitor<'_> {
21332133
);
21342134
}
21352135
}
2136+
} else {
2137+
// special case when `#[macro_export]` is applied to a macro 2.0
2138+
let (macro_definition, _) =
2139+
self.tcx.hir().find(hir_id).unwrap().expect_item().expect_macro();
2140+
let is_decl_macro = !macro_definition.macro_rules;
2141+
2142+
if is_decl_macro {
2143+
self.tcx.emit_spanned_lint(
2144+
UNUSED_ATTRIBUTES,
2145+
hir_id,
2146+
attr.span,
2147+
errors::MacroExport::OnDeclMacro,
2148+
);
2149+
}
21362150
}
21372151
}
21382152

Diff for: compiler/rustc_passes/src/errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,10 @@ pub enum MacroExport {
690690
#[diag(passes_macro_export)]
691691
Normal,
692692

693+
#[diag(passes_macro_export_on_decl_macro)]
694+
#[note]
695+
OnDeclMacro,
696+
693697
#[diag(passes_invalid_macro_export_arguments)]
694698
UnknownItem { name: Symbol },
695699

Diff for: library/core/src/macros/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ macro_rules! debug_assert_ne {
312312
/// let c = Ok("abc".to_string());
313313
/// debug_assert_matches!(c, Ok(x) | Err(x) if x.len() < 100);
314314
/// ```
315-
#[macro_export]
316315
#[unstable(feature = "assert_matches", issue = "82775")]
317316
#[allow_internal_unstable(assert_matches)]
318317
#[rustc_macro_transparency = "semitransparent"]

Diff for: tests/ui/attributes/macro_export_on_decl_macro.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Using #[macro_export] on a decl macro has no effect and should warn
2+
3+
#![feature(decl_macro)]
4+
#![deny(unused)]
5+
6+
#[macro_export] //~ ERROR `#[macro_export]` has no effect on declarative macro definitions
7+
pub macro foo() {}
8+
9+
fn main() {}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: `#[macro_export]` has no effect on declarative macro definitions
2+
--> $DIR/macro_export_on_decl_macro.rs:6:1
3+
|
4+
LL | #[macro_export]
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: declarative macros follow the same exporting rules as regular items
8+
note: the lint level is defined here
9+
--> $DIR/macro_export_on_decl_macro.rs:4:9
10+
|
11+
LL | #![deny(unused)]
12+
| ^^^^^^
13+
= note: `#[deny(unused_attributes)]` implied by `#[deny(unused)]`
14+
15+
error: aborting due to previous error
16+

0 commit comments

Comments
 (0)