Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn when #[macro_export] is applied on decl macros #114413

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@ passes_link_section =
passes_macro_export =
`#[macro_export]` only has an effect on macro definitions

passes_macro_export_on_decl_macro =
`#[macro_export]` has no effect on declarative macro definitions
.note = declarative macros follow the same exporting rules as regular items

passes_macro_use =
`#[{$name}]` only has an effect on `extern crate` and modules

Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2133,6 +2133,20 @@ impl CheckAttrVisitor<'_> {
);
}
}
} else {
// special case when `#[macro_export]` is applied to a macro 2.0
let (macro_definition, _) =
self.tcx.hir().find(hir_id).unwrap().expect_item().expect_macro();
let is_decl_macro = !macro_definition.macro_rules;

if is_decl_macro {
self.tcx.emit_spanned_lint(
UNUSED_ATTRIBUTES,
hir_id,
attr.span,
errors::MacroExport::OnDeclMacro,
);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,10 @@ pub enum MacroExport {
#[diag(passes_macro_export)]
Normal,

#[diag(passes_macro_export_on_decl_macro)]
#[note]
OnDeclMacro,

#[diag(passes_invalid_macro_export_arguments)]
UnknownItem { name: Symbol },

Expand Down
1 change: 0 additions & 1 deletion library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ macro_rules! debug_assert_ne {
/// let c = Ok("abc".to_string());
/// debug_assert_matches!(c, Ok(x) | Err(x) if x.len() < 100);
/// ```
#[macro_export]
#[unstable(feature = "assert_matches", issue = "82775")]
#[allow_internal_unstable(assert_matches)]
#[rustc_macro_transparency = "semitransparent"]
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/attributes/macro_export_on_decl_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Using #[macro_export] on a decl macro has no effect and should warn

#![feature(decl_macro)]
#![deny(unused)]

#[macro_export] //~ ERROR `#[macro_export]` has no effect on declarative macro definitions
pub macro foo() {}

fn main() {}
16 changes: 16 additions & 0 deletions tests/ui/attributes/macro_export_on_decl_macro.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error: `#[macro_export]` has no effect on declarative macro definitions
--> $DIR/macro_export_on_decl_macro.rs:6:1
|
LL | #[macro_export]
| ^^^^^^^^^^^^^^^
|
= note: declarative macros follow the same exporting rules as regular items
note: the lint level is defined here
--> $DIR/macro_export_on_decl_macro.rs:4:9
|
LL | #![deny(unused)]
| ^^^^^^
= note: `#[deny(unused_attributes)]` implied by `#[deny(unused)]`

error: aborting due to previous error

Loading