Skip to content

Commit ac1f122

Browse files
committed
Port #[macro_export] to the new attribute parsing infrastructure
1 parent d2baa49 commit ac1f122

File tree

22 files changed

+206
-158
lines changed

22 files changed

+206
-158
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ pub enum AttributeKind {
300300
/// Represents `#[loop_match]`.
301301
LoopMatch(Span),
302302

303+
/// Represents [`#[macro_export}`](https://doc.rust-lang.org/reference/macros-by-example.html#r-macro.decl.scope.path).
304+
MacroExport { span: Span, local_inner_macros: bool },
305+
303306
/// Represents `#[rustc_macro_transparency]`.
304307
MacroTransparency(Transparency),
305308

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ impl AttributeKind {
4242
LinkName { .. } => Yes,
4343
LinkSection { .. } => No,
4444
LoopMatch(..) => No,
45+
MacroExport { .. } => Yes,
4546
MacroTransparency(..) => Yes,
4647
Marker(..) => No,
4748
MayDangle(..) => No,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use rustc_attr_data_structures::AttributeKind;
2+
use rustc_attr_data_structures::AttributeKind::MacroExport;
3+
use rustc_attr_data_structures::lints::AttributeLintKind;
4+
use rustc_feature::{AttributeTemplate, template};
5+
use rustc_span::{Symbol, sym};
6+
7+
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
8+
use crate::context::{AcceptContext, Stage};
9+
use crate::parser::ArgParser;
10+
11+
pub(crate) struct MacroExportParser;
12+
13+
impl<S: Stage> SingleAttributeParser<S> for crate::attributes::macro_attrs::MacroExportParser {
14+
const PATH: &[Symbol] = &[sym::macro_export];
15+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
16+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
17+
const TEMPLATE: AttributeTemplate = template!(Word, List: "local_inner_macros");
18+
19+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
20+
let local_inner_macros = match args {
21+
ArgParser::NoArgs => false,
22+
ArgParser::List(list) => {
23+
let Some(l) = list.single() else {
24+
cx.expected_single_argument(list.span);
25+
return None;
26+
};
27+
match l.meta_item().and_then(|i| i.path().word_sym()) {
28+
Some(sym::local_inner_macros) => true,
29+
_ => {
30+
cx.expected_specific_argument(l.span(), vec!["local_inner_macros"]);
31+
return None;
32+
}
33+
}
34+
}
35+
ArgParser::NameValue(_) => {
36+
let suggestions =
37+
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "macro_export");
38+
let span = cx.attr_span;
39+
cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
40+
return None;
41+
}
42+
};
43+
Some(MacroExport { span: cx.attr_span, local_inner_macros })
44+
}
45+
}

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub(crate) mod inline;
3535
pub(crate) mod link_attrs;
3636
pub(crate) mod lint_helpers;
3737
pub(crate) mod loop_match;
38+
pub(crate) mod macro_attrs;
3839
pub(crate) mod must_use;
3940
pub(crate) mod no_implicit_prelude;
4041
pub(crate) mod non_exhaustive;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use crate::attributes::link_attrs::{
2929
};
3030
use crate::attributes::lint_helpers::{AsPtrParser, PassByValueParser, PubTransparentParser};
3131
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
32+
use crate::attributes::macro_attrs::MacroExportParser;
3233
use crate::attributes::must_use::MustUseParser;
3334
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
3435
use crate::attributes::non_exhaustive::NonExhaustiveParser;
@@ -142,6 +143,7 @@ attribute_parsers!(
142143
Single<InlineParser>,
143144
Single<LinkNameParser>,
144145
Single<LinkSectionParser>,
146+
Single<MacroExportParser>,
145147
Single<MustUseParser>,
146148
Single<OptimizeParser>,
147149
Single<PathAttributeParser>,

compiler/rustc_expand/src/base.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -892,9 +892,9 @@ impl SyntaxExtension {
892892
let allow_internal_unsafe =
893893
ast::attr::find_by_name(attrs, sym::allow_internal_unsafe).is_some();
894894

895-
let local_inner_macros = ast::attr::find_by_name(attrs, sym::macro_export)
896-
.and_then(|macro_export| macro_export.meta_item_list())
897-
.is_some_and(|l| ast::attr::list_contains_name(&l, sym::local_inner_macros));
895+
let local_inner_macros =
896+
*find_attr!(attrs, AttributeKind::MacroExport {local_inner_macros: l, ..} => l)
897+
.unwrap_or(&false);
898898
let collapse_debuginfo = Self::get_collapse_debuginfo(sess, attrs, !is_local);
899899
tracing::debug!(?name, ?local_inner_macros, ?collapse_debuginfo, ?allow_internal_unsafe);
900900

compiler/rustc_lint/src/non_local_def.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
use rustc_attr_data_structures::{AttributeKind, find_attr};
12
use rustc_errors::MultiSpan;
23
use rustc_hir::def::{DefKind, Res};
34
use rustc_hir::intravisit::{self, Visitor, VisitorExt};
45
use rustc_hir::{Body, HirId, Item, ItemKind, Node, Path, TyKind};
56
use rustc_middle::ty::TyCtxt;
67
use rustc_session::{declare_lint, impl_lint_pass};
78
use rustc_span::def_id::{DefId, LOCAL_CRATE};
8-
use rustc_span::{ExpnKind, MacroKind, Span, kw, sym};
9+
use rustc_span::{ExpnKind, MacroKind, Span, kw};
910

1011
use crate::lints::{NonLocalDefinitionsCargoUpdateNote, NonLocalDefinitionsDiag};
1112
use crate::{LateContext, LateLintPass, LintContext, fluent_generated as fluent};
@@ -241,7 +242,10 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
241242
)
242243
}
243244
ItemKind::Macro(_, _macro, MacroKind::Bang)
244-
if cx.tcx.has_attr(item.owner_id.def_id, sym::macro_export) =>
245+
if find_attr!(
246+
cx.tcx.get_all_attrs(item.owner_id.def_id),
247+
AttributeKind::MacroExport { .. }
248+
) =>
245249
{
246250
cx.emit_span_lint(
247251
NON_LOCAL_DEFINITIONS,

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ declare_lint_pass! {
5252
INEFFECTIVE_UNSTABLE_TRAIT_IMPL,
5353
INLINE_NO_SANITIZE,
5454
INVALID_DOC_ATTRIBUTES,
55-
INVALID_MACRO_EXPORT_ARGUMENTS,
5655
INVALID_TYPE_PARAM_DEFAULT,
5756
IRREFUTABLE_LET_PATTERNS,
5857
LARGE_ASSIGNMENTS,
@@ -4161,36 +4160,6 @@ declare_lint! {
41614160
report_in_external_macro
41624161
}
41634162

4164-
declare_lint! {
4165-
/// The `invalid_macro_export_arguments` lint detects cases where `#[macro_export]` is being used with invalid arguments.
4166-
///
4167-
/// ### Example
4168-
///
4169-
/// ```rust,compile_fail
4170-
/// #![deny(invalid_macro_export_arguments)]
4171-
///
4172-
/// #[macro_export(invalid_parameter)]
4173-
/// macro_rules! myMacro {
4174-
/// () => {
4175-
/// // [...]
4176-
/// }
4177-
/// }
4178-
///
4179-
/// #[macro_export(too, many, items)]
4180-
/// ```
4181-
///
4182-
/// {{produces}}
4183-
///
4184-
/// ### Explanation
4185-
///
4186-
/// The only valid argument is `#[macro_export(local_inner_macros)]` or no argument (`#[macro_export]`).
4187-
/// You can't have multiple arguments in a `#[macro_export(..)]`, or mention arguments other than `local_inner_macros`.
4188-
///
4189-
pub INVALID_MACRO_EXPORT_ARGUMENTS,
4190-
Warn,
4191-
"\"invalid_parameter\" isn't a valid argument for `#[macro_export]`",
4192-
}
4193-
41944163
declare_lint! {
41954164
/// The `private_interfaces` lint detects types in a primary interface of an item,
41964165
/// that are more private than the item itself. Primary interface of an item is all

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ pub fn check_builtin_meta_item(
302302
| sym::cold
303303
| sym::target_feature
304304
| sym::rustc_allow_const_fn_unstable
305+
| sym::macro_export
305306
| sym::naked
306307
| sym::no_mangle
307308
| sym::non_exhaustive

compiler/rustc_passes/messages.ftl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,6 @@ passes_invalid_attr_at_crate_level =
395395
passes_invalid_attr_at_crate_level_item =
396396
the inner attribute doesn't annotate this {$kind}
397397
398-
passes_invalid_macro_export_arguments = invalid `#[macro_export]` argument
399-
400-
passes_invalid_macro_export_arguments_too_many_items = `#[macro_export]` can only take 1 or 0 arguments
401-
402398
passes_lang_item_fn = {$name ->
403399
[panic_impl] `#[panic_handler]`
404400
*[other] `{$name}` lang item

0 commit comments

Comments
 (0)