rustc_attr_parsing/attributes/
proc_macro_attrs.rs1use rustc_feature::AttributeStability;
2use rustc_session::lint::builtin::AMBIGUOUS_DERIVE_HELPERS;
3
4use super::prelude::*;
5
6const PROC_MACRO_ALLOWED_TARGETS: AllowedTargets =
7 AllowedTargets::AllowList(&[Allow(Target::Fn), Warn(Target::Crate), Warn(Target::MacroCall)]);
8
9pub(crate) struct ProcMacroParser;
10impl NoArgsAttributeParser for ProcMacroParser {
11 const PATH: &[Symbol] = &[sym::proc_macro];
12 const ALLOWED_TARGETS: AllowedTargets = PROC_MACRO_ALLOWED_TARGETS;
13 const STABILITY: AttributeStability = AttributeStability::Stable;
14 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::ProcMacro;
15}
16
17pub(crate) struct ProcMacroAttributeParser;
18impl NoArgsAttributeParser for ProcMacroAttributeParser {
19 const PATH: &[Symbol] = &[sym::proc_macro_attribute];
20 const ALLOWED_TARGETS: AllowedTargets = PROC_MACRO_ALLOWED_TARGETS;
21 const STABILITY: AttributeStability = AttributeStability::Stable;
22 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::ProcMacroAttribute;
23}
24
25pub(crate) struct ProcMacroDeriveParser;
26impl SingleAttributeParser for ProcMacroDeriveParser {
27 const PATH: &[Symbol] = &[sym::proc_macro_derive];
28 const ALLOWED_TARGETS: AllowedTargets = PROC_MACRO_ALLOWED_TARGETS;
29 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: false,
list: Some(&["TraitName", "TraitName, attributes(name1, name2, ...)"]),
one_of: &[],
name_value_str: None,
docs: Some("https://doc.rust-lang.org/reference/procedural-macros.html#derive-macros"),
}template!(
30 List: &["TraitName", "TraitName, attributes(name1, name2, ...)"],
31 "https://doc.rust-lang.org/reference/procedural-macros.html#derive-macros"
32 );
33 const STABILITY: AttributeStability = AttributeStability::Stable;
34
35 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
36 let (trait_name, helper_attrs) = parse_derive_like(cx, args, true)?;
37 Some(AttributeKind::ProcMacroDerive {
38 trait_name: trait_name.expect("Trait name is mandatory, so it is present"),
39 helper_attrs,
40 })
41 }
42}
43
44pub(crate) struct RustcBuiltinMacroParser;
45impl SingleAttributeParser for RustcBuiltinMacroParser {
46 const PATH: &[Symbol] = &[sym::rustc_builtin_macro];
47 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::MacroDef)]);
48 const TEMPLATE: AttributeTemplate =
49 crate::AttributeTemplate {
word: false,
list: Some(&["TraitName", "TraitName, attributes(name1, name2, ...)"]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(List: &["TraitName", "TraitName, attributes(name1, name2, ...)"]);
50 const STABILITY: AttributeStability = AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
gate_check: rustc_feature::Features::rustc_attrs,
notes: &[],
}unstable!(rustc_attrs);
51
52 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
53 let (builtin_name, helper_attrs) = parse_derive_like(cx, args, false)?;
54 Some(AttributeKind::RustcBuiltinMacro { builtin_name, helper_attrs })
55 }
56}
57
58fn parse_derive_like(
59 cx: &mut AcceptContext<'_, '_>,
60 args: &ArgParser,
61 trait_name_mandatory: bool,
62) -> Option<(Option<Symbol>, ThinVec<Symbol>)> {
63 let Some(list) = args.as_list() else {
64 if args.as_no_args().is_ok() && !trait_name_mandatory {
66 return Some((None, ThinVec::new()));
67 }
68 let attr_span = cx.attr_span;
69 cx.adcx().expected_list(attr_span, args);
70 return None;
71 };
72 let mut items = list.mixed();
73
74 let Some(trait_attr) = items.next() else {
76 cx.adcx().expected_at_least_one_argument(list.span);
77 return None;
78 };
79 let Some(trait_attr) = trait_attr.meta_item() else {
80 cx.adcx().expected_not_literal(trait_attr.span());
81 return None;
82 };
83 let Some(trait_ident) = trait_attr.path().word() else {
84 cx.adcx().expected_identifier(trait_attr.path().span());
85 return None;
86 };
87 if !trait_ident.name.can_be_raw() {
88 cx.adcx().expected_identifier(trait_ident.span);
89 return None;
90 }
91 cx.expect_no_args(trait_attr.args())?;
92
93 let mut attributes = ThinVec::new();
95 if let Some(attrs) = items.next() {
96 let Some(attr_list) = attrs.meta_item() else {
97 cx.adcx().expected_not_literal(attrs.span());
98 return None;
99 };
100 if !attr_list.path().word_is(sym::attributes) {
101 cx.adcx().expected_specific_argument(attrs.span(), &[sym::attributes]);
102 return None;
103 }
104 let attr_list = cx.expect_list(attr_list.args(), attrs.span())?;
105
106 for attr in attr_list.mixed() {
108 let Some(attr) = attr.meta_item() else {
109 cx.adcx().expected_identifier(attr.span());
110 return None;
111 };
112 cx.expect_no_args(attr.args())?;
113 let Some(ident) = attr.path().word() else {
114 cx.adcx().expected_identifier(attr.path().span());
115 return None;
116 };
117 if !ident.name.can_be_raw() {
118 cx.adcx().expected_identifier(ident.span);
119 return None;
120 }
121 if rustc_feature::is_builtin_attr_name(ident.name) {
122 cx.emit_lint(
123 AMBIGUOUS_DERIVE_HELPERS,
124 crate::diagnostics::AmbiguousDeriveHelpers,
125 ident.span,
126 );
127 }
128 attributes.push(ident.name);
129 }
130 }
131
132 if let Some(next) = items.next() {
134 cx.adcx().expected_no_args(next.span());
135 }
136
137 Some((Some(trait_ident.name), attributes))
138}