rustc_attr_parsing/attributes/
proc_macro_attrs.rs1use rustc_errors::Diagnostic;
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<S: Stage> NoArgsAttributeParser<S> for ProcMacroParser {
11 const PATH: &[Symbol] = &[sym::proc_macro];
12 const ALLOWED_TARGETS: AllowedTargets = PROC_MACRO_ALLOWED_TARGETS;
13 const CREATE: fn(Span) -> AttributeKind = AttributeKind::ProcMacro;
14}
15
16pub(crate) struct ProcMacroAttributeParser;
17impl<S: Stage> NoArgsAttributeParser<S> for ProcMacroAttributeParser {
18 const PATH: &[Symbol] = &[sym::proc_macro_attribute];
19 const ALLOWED_TARGETS: AllowedTargets = PROC_MACRO_ALLOWED_TARGETS;
20 const CREATE: fn(Span) -> AttributeKind = AttributeKind::ProcMacroAttribute;
21}
22
23pub(crate) struct ProcMacroDeriveParser;
24impl<S: Stage> SingleAttributeParser<S> for ProcMacroDeriveParser {
25 const PATH: &[Symbol] = &[sym::proc_macro_derive];
26 const ALLOWED_TARGETS: AllowedTargets = PROC_MACRO_ALLOWED_TARGETS;
27 const TEMPLATE: AttributeTemplate = ::rustc_feature::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!(
28 List: &["TraitName", "TraitName, attributes(name1, name2, ...)"],
29 "https://doc.rust-lang.org/reference/procedural-macros.html#derive-macros"
30 );
31
32 fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
33 let (trait_name, helper_attrs) = parse_derive_like(cx, args, true)?;
34 Some(AttributeKind::ProcMacroDerive {
35 trait_name: trait_name.expect("Trait name is mandatory, so it is present"),
36 helper_attrs,
37 span: cx.attr_span,
38 })
39 }
40}
41
42pub(crate) struct RustcBuiltinMacroParser;
43impl<S: Stage> SingleAttributeParser<S> for RustcBuiltinMacroParser {
44 const PATH: &[Symbol] = &[sym::rustc_builtin_macro];
45 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::MacroDef)]);
46 const TEMPLATE: AttributeTemplate =
47 ::rustc_feature::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, ...)"]);
48
49 fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
50 let (builtin_name, helper_attrs) = parse_derive_like(cx, args, false)?;
51 Some(AttributeKind::RustcBuiltinMacro { builtin_name, helper_attrs, span: cx.attr_span })
52 }
53}
54
55fn parse_derive_like<S: Stage>(
56 cx: &mut AcceptContext<'_, '_, S>,
57 args: &ArgParser,
58 trait_name_mandatory: bool,
59) -> Option<(Option<Symbol>, ThinVec<Symbol>)> {
60 let Some(list) = args.as_list() else {
61 if args.no_args().is_ok() && !trait_name_mandatory {
63 return Some((None, ThinVec::new()));
64 }
65 let attr_span = cx.attr_span;
66 cx.adcx().expected_list(attr_span, args);
67 return None;
68 };
69 let mut items = list.mixed();
70
71 let Some(trait_attr) = items.next() else {
73 cx.adcx().expected_at_least_one_argument(list.span);
74 return None;
75 };
76 let Some(trait_attr) = trait_attr.meta_item() else {
77 cx.adcx().expected_not_literal(trait_attr.span());
78 return None;
79 };
80 let Some(trait_ident) = trait_attr.path().word() else {
81 cx.adcx().expected_identifier(trait_attr.path().span());
82 return None;
83 };
84 if !trait_ident.name.can_be_raw() {
85 cx.adcx().expected_identifier(trait_ident.span);
86 return None;
87 }
88 if let Err(e) = trait_attr.args().no_args() {
89 cx.adcx().expected_no_args(e);
90 return None;
91 };
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 if let Err(e) = attr.args().no_args() {
113 cx.adcx().expected_no_args(e);
114 return None;
115 };
116 let Some(ident) = attr.path().word() else {
117 cx.adcx().expected_identifier(attr.path().span());
118 return None;
119 };
120 if !ident.name.can_be_raw() {
121 cx.adcx().expected_identifier(ident.span);
122 return None;
123 }
124 if rustc_feature::is_builtin_attr_name(ident.name) {
125 cx.emit_dyn_lint(
126 AMBIGUOUS_DERIVE_HELPERS,
127 |dcx, level| crate::errors::AmbiguousDeriveHelpers.into_diag(dcx, level),
128 ident.span,
129 );
130 }
131 attributes.push(ident.name);
132 }
133 }
134
135 if let Some(next) = items.next() {
137 cx.adcx().expected_no_args(next.span());
138 }
139
140 Some((Some(trait_ident.name), attributes))
141}