未验证 提交 42cdc7df 编写于 作者: M Matthias Krüger 提交者: GitHub

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
......@@ -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
......
......@@ -2133,6 +2133,20 @@ fn check_macro_export(&self, hir_id: HirId, attr: &Attribute, target: Target) {
);
}
}
} 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,
);
}
}
}
......
......@@ -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 },
......
......@@ -312,7 +312,6 @@
/// 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"]
......
// 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() {}
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册