Skip to content

Commit bdf4e3d

Browse files
committed
check_attrs: Warn when #[macro_export] is used on macros 2.0
The compiler should emit a more specific error when the `#[macro_export]` attribute is present on a decl macro, instead of silently ignoring it. This commit adds the required error message in rustc_passes/messages.ftl, as well as a note. A new variant is added to the `errors::MacroExport` enum, specifically for the case where the attribute is added to a macro 2.0.
1 parent f177625 commit bdf4e3d

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

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

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

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

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() {}
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)