Skip to content
12 changes: 12 additions & 0 deletions compiler/rustc_attr_data_structures/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,24 @@ pub enum AttributeKind {
/// Represents `#[pointee]`
Pointee(Span),

/// Represents `#[proc_macro]`
ProcMacro(Span),

/// Represents `#[proc_macro_attribute]`
ProcMacroAttribute(Span),

/// Represents `#[proc_macro_derive]`
ProcMacroDerive { trait_name: Symbol, helper_attrs: ThinVec<Symbol>, span: Span },

/// Represents `#[rustc_pub_transparent]` (used by the `repr_transparent_external_private_fields` lint).
PubTransparent(Span),

/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
Repr { reprs: ThinVec<(ReprAttr, Span)>, first_span: Span },

/// Represents `#[rustc_builtin_macro]`.
RustcBuiltinMacro { builtin_name: Option<Symbol>, helper_attrs: ThinVec<Symbol>, span: Span },

/// Represents `#[rustc_layout_scalar_valid_range_end]`.
RustcLayoutScalarValidRangeEnd(Box<u128>, Span),

Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_attr_data_structures/src/encode_cross_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ impl AttributeKind {
PassByValue(..) => Yes,
Path(..) => No,
Pointee(..) => No,
ProcMacro(..) => No,
ProcMacroAttribute(..) => No,
ProcMacroDerive { .. } => No,
PubTransparent(..) => Yes,
Repr { .. } => No,
RustcBuiltinMacro { .. } => Yes,
RustcLayoutScalarValidRangeEnd(..) => Yes,
RustcLayoutScalarValidRangeStart(..) => Yes,
RustcObjectLifetimeDefault => No,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_attr_parsing/src/attributes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub(crate) mod must_use;
pub(crate) mod no_implicit_prelude;
pub(crate) mod non_exhaustive;
pub(crate) mod path;
pub(crate) mod proc_macro_attrs;
pub(crate) mod repr;
pub(crate) mod rustc_internal;
pub(crate) mod semantics;
Expand Down
139 changes: 139 additions & 0 deletions compiler/rustc_attr_parsing/src/attributes/proc_macro_attrs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use rustc_attr_data_structures::AttributeKind;
use rustc_feature::{AttributeTemplate, template};
use rustc_span::{Span, Symbol, sym};
use thin_vec::ThinVec;

use crate::attributes::{
AttributeOrder, NoArgsAttributeParser, OnDuplicate, SingleAttributeParser,
};
use crate::context::{AcceptContext, Stage};
use crate::parser::ArgParser;

pub(crate) struct ProcMacroParser;
impl<S: Stage> NoArgsAttributeParser<S> for ProcMacroParser {
const PATH: &[Symbol] = &[sym::proc_macro];
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
const CREATE: fn(Span) -> AttributeKind = AttributeKind::ProcMacro;
}

pub(crate) struct ProcMacroAttributeParser;
impl<S: Stage> NoArgsAttributeParser<S> for ProcMacroAttributeParser {
const PATH: &[Symbol] = &[sym::proc_macro_attribute];
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
const CREATE: fn(Span) -> AttributeKind = AttributeKind::ProcMacroAttribute;
}

pub(crate) struct ProcMacroDeriveParser;
impl<S: Stage> SingleAttributeParser<S> for ProcMacroDeriveParser {
const PATH: &[Symbol] = &[sym::proc_macro_derive];
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
const TEMPLATE: AttributeTemplate =
template!(List: "TraitName, /*opt*/ attributes(name1, name2, ...)");

fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
let (trait_name, helper_attrs) = parse_derive_like(cx, args, true)?;
Some(AttributeKind::ProcMacroDerive {
trait_name: trait_name.expect("Trait name is mandatory, so it is present"),
helper_attrs,
span: cx.attr_span,
})
}
}

pub(crate) struct RustcBuiltinMacroParser;
impl<S: Stage> SingleAttributeParser<S> for RustcBuiltinMacroParser {
const PATH: &[Symbol] = &[sym::rustc_builtin_macro];
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
const TEMPLATE: AttributeTemplate =
template!(List: "TraitName, /*opt*/ attributes(name1, name2, ...)");

fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
let (builtin_name, helper_attrs) = parse_derive_like(cx, args, false)?;
Some(AttributeKind::RustcBuiltinMacro { builtin_name, helper_attrs, span: cx.attr_span })
}
}

fn parse_derive_like<S: Stage>(
cx: &mut AcceptContext<'_, '_, S>,
args: &ArgParser<'_>,
trait_name_mandatory: bool,
) -> Option<(Option<Symbol>, ThinVec<Symbol>)> {
let Some(list) = args.list() else {
// For #[rustc_builtin_macro], it is permitted to leave out the trait name
if args.no_args().is_ok() && !trait_name_mandatory {
return Some((None, ThinVec::new()));
}
cx.expected_list(cx.attr_span);
return None;
};
let mut items = list.mixed();

// Parse the name of the trait that is derived.
let Some(trait_attr) = items.next() else {
cx.expected_at_least_one_argument(list.span);
return None;
};
let Some(trait_attr) = trait_attr.meta_item() else {
cx.unexpected_literal(trait_attr.span());
return None;
};
let Some(trait_ident) = trait_attr.path().word() else {
cx.expected_identifier(trait_attr.path().span());
return None;
};
if !trait_ident.name.can_be_raw() {
cx.expected_identifier(trait_ident.span);
return None;
}
if let Err(e) = trait_attr.args().no_args() {
cx.expected_no_args(e);
return None;
};

// Parse optional attributes
let mut attributes = ThinVec::new();
if let Some(attrs) = items.next() {
let Some(attr_list) = attrs.meta_item() else {
cx.expected_list(attrs.span());
return None;
};
if !attr_list.path().word_is(sym::attributes) {
cx.expected_specific_argument(attrs.span(), vec!["attributes"]);
return None;
}
let Some(attr_list) = attr_list.args().list() else {
cx.expected_list(attrs.span());
return None;
};

// Parse item in `attributes(...)` argument
for attr in attr_list.mixed() {
let Some(attr) = attr.meta_item() else {
cx.expected_identifier(attr.span());
return None;
};
if let Err(e) = attr.args().no_args() {
cx.expected_no_args(e);
return None;
};
let Some(ident) = attr.path().word() else {
cx.expected_identifier(attr.path().span());
return None;
};
if !ident.name.can_be_raw() {
cx.expected_identifier(ident.span);
return None;
}
attributes.push(ident.name);
}
}

// If anything else is specified, we should reject it
if let Some(next) = items.next() {
cx.expected_no_args(next.span());
}

Some((Some(trait_ident.name), attributes))
}
7 changes: 7 additions & 0 deletions compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ use crate::attributes::must_use::MustUseParser;
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
use crate::attributes::non_exhaustive::NonExhaustiveParser;
use crate::attributes::path::PathParser as PathAttributeParser;
use crate::attributes::proc_macro_attrs::{
ProcMacroAttributeParser, ProcMacroDeriveParser, ProcMacroParser, RustcBuiltinMacroParser,
};
use crate::attributes::repr::{AlignParser, ReprParser};
use crate::attributes::rustc_internal::{
RustcLayoutScalarValidRangeEnd, RustcLayoutScalarValidRangeStart,
Expand Down Expand Up @@ -154,6 +157,8 @@ attribute_parsers!(
Single<MustUseParser>,
Single<OptimizeParser>,
Single<PathAttributeParser>,
Single<ProcMacroDeriveParser>,
Single<RustcBuiltinMacroParser>,
Single<RustcForceInlineParser>,
Single<RustcLayoutScalarValidRangeEnd>,
Single<RustcLayoutScalarValidRangeStart>,
Expand Down Expand Up @@ -186,6 +191,8 @@ attribute_parsers!(
Single<WithoutArgs<ParenSugarParser>>,
Single<WithoutArgs<PassByValueParser>>,
Single<WithoutArgs<PointeeParser>>,
Single<WithoutArgs<ProcMacroAttributeParser>>,
Single<WithoutArgs<ProcMacroParser>>,
Single<WithoutArgs<PubTransparentParser>>,
Single<WithoutArgs<SpecializationTraitParser>>,
Single<WithoutArgs<StdInternalSymbolParser>>,
Expand Down
28 changes: 21 additions & 7 deletions compiler/rustc_builtin_macros/src/proc_macro_harness.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::mem;
use std::{mem, slice};

use rustc_ast::ptr::P;
use rustc_ast::visit::{self, Visitor};
use rustc_ast::{self as ast, NodeId, attr};
use rustc_ast::{self as ast, HasNodeId, NodeId, attr};
use rustc_ast_pretty::pprust;
use rustc_attr_data_structures::AttributeKind;
use rustc_attr_parsing::AttributeParser;
use rustc_errors::DiagCtxtHandle;
use rustc_expand::base::{ExtCtxt, ResolverExpand, parse_macro_name_and_helper_attrs};
use rustc_expand::base::{ExtCtxt, ResolverExpand};
use rustc_expand::expand::{AstFragment, ExpansionConfig};
use rustc_feature::Features;
use rustc_session::Session;
Expand All @@ -22,7 +24,7 @@ struct ProcMacroDerive {
trait_name: Symbol,
function_ident: Ident,
span: Span,
attrs: Vec<Symbol>,
attrs: ThinVec<Symbol>,
}

struct ProcMacroDef {
Expand All @@ -41,6 +43,7 @@ struct CollectProcMacros<'a> {
macros: Vec<ProcMacro>,
in_root: bool,
dcx: DiagCtxtHandle<'a>,
session: &'a Session,
source_map: &'a SourceMap,
is_proc_macro_crate: bool,
is_test_crate: bool,
Expand All @@ -63,6 +66,7 @@ pub fn inject(
macros: Vec::new(),
in_root: true,
dcx,
session: sess,
source_map: sess.source_map(),
is_proc_macro_crate,
is_test_crate,
Expand Down Expand Up @@ -98,8 +102,18 @@ impl<'a> CollectProcMacros<'a> {
function_ident: Ident,
attr: &'a ast::Attribute,
) {
let Some((trait_name, proc_attrs)) =
parse_macro_name_and_helper_attrs(self.dcx, attr, "derive")
let Some(rustc_hir::Attribute::Parsed(AttributeKind::ProcMacroDerive {
trait_name,
helper_attrs,
..
})) = AttributeParser::parse_limited(
self.session,
slice::from_ref(attr),
sym::proc_macro_derive,
item.span,
item.node_id(),
None,
)
else {
return;
};
Expand All @@ -110,7 +124,7 @@ impl<'a> CollectProcMacros<'a> {
span: item.span,
trait_name,
function_ident,
attrs: proc_attrs,
attrs: helper_attrs,
}));
} else {
let msg = if !self.in_root {
Expand Down
27 changes: 0 additions & 27 deletions compiler/rustc_expand/messages.ftl
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
expand_arg_not_attributes =
second argument must be `attributes`

expand_attr_no_arguments =
attribute must have either one or two arguments

expand_attribute_meta_item =
attribute must be a meta item, not a literal

expand_attribute_single_word =
attribute must only be a single word

expand_attributes_on_expressions_experimental =
attributes on expressions are experimental
.help_outer_doc = `///` is used for outer documentation comments; for a plain comment, use `//`
.help_inner_doc = `//!` is used for inner documentation comments; for a plain comment, use `//` by removing the `!` or inserting a space in between them: `// !`

expand_attributes_wrong_form =
attribute must be of form: `attributes(foo, bar)`

expand_cannot_be_name_of_macro =
`{$trait_ident}` cannot be a name of {$macro_type} macro

expand_collapse_debuginfo_illegal =
illegal value for attribute #[collapse_debuginfo(no|external|yes)]

Expand Down Expand Up @@ -71,9 +53,6 @@ expand_glob_delegation_outside_impls =
expand_glob_delegation_traitless_qpath =
qualified path without a trait in glob delegation

expand_helper_attribute_name_invalid =
`{$name}` cannot be a name of derive helper attribute

expand_incomplete_parse =
macro expansion ignores {$descr} and any tokens following
.label = caused by the macro expansion here
Expand Down Expand Up @@ -165,12 +144,6 @@ expand_mve_unrecognized_var =
expand_non_inline_modules_in_proc_macro_input_are_unstable =
non-inline modules in proc macro input are unstable

expand_not_a_meta_item =
not a meta item

expand_only_one_word =
must only be one word

expand_proc_macro_back_compat = using an old version of `{$crate_name}`
.note = older versions of the `{$crate_name}` crate no longer compile; please update to `{$crate_name}` v{$fixed_version}, or switch to one of the `{$crate_name}` alternatives

Expand Down
Loading
Loading