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