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<'_> =
48 AllowedTargets::AllowList(&[Allow(Target::MacroDef)]);
49 const TEMPLATE: AttributeTemplate =
50 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, ...)"]);
51 const STABILITY: AttributeStability = AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
gate_check: rustc_feature::Features::rustc_attrs,
notes: &[],
}unstable!(rustc_attrs);
52
53 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
54 let (builtin_name, helper_attrs) = parse_derive_like(cx, args, false)?;
55 Some(AttributeKind::RustcBuiltinMacro { builtin_name, helper_attrs })
56 }
57}
58
59fn parse_derive_like(
60 cx: &mut AcceptContext<'_, '_>,
61 args: &ArgParser,
62 trait_name_mandatory: bool,
63) -> Option<(Option<Symbol>, ThinVec<Symbol>)> {
64 let Some(list) = args.as_list() else {
65 if args.as_no_args().is_ok() && !trait_name_mandatory {
67 return Some((None, ThinVec::new()));
68 }
69 let attr_span = cx.attr_span;
70 cx.adcx().expected_list(attr_span, args);
71 return None;
72 };
73 let mut items = list.mixed();
74
75 let Some(trait_attr) = items.next() else {
77 cx.adcx().expected_at_least_one_argument(list.span);
78 return None;
79 };
80 let Some(trait_attr) = trait_attr.meta_item() else {
81 cx.adcx().expected_not_literal(trait_attr.span());
82 return None;
83 };
84 let Some(trait_ident) = trait_attr.path().word() else {
85 cx.adcx().expected_identifier(trait_attr.path().span());
86 return None;
87 };
88 if !trait_ident.name.can_be_raw() {
89 cx.adcx().expected_identifier(trait_ident.span);
90 return None;
91 }
92 cx.expect_no_args(trait_attr.args())?;
93
94 let mut attributes = ThinVec::new();
96 if let Some(attrs) = items.next() {
97 let Some(attr_list) = attrs.meta_item() else {
98 cx.adcx().expected_not_literal(attrs.span());
99 return None;
100 };
101 if !attr_list.path().word_is(sym::attributes) {
102 cx.adcx().expected_specific_argument(attrs.span(), &[sym::attributes]);
103 return None;
104 }
105 let attr_list = cx.expect_list(attr_list.args(), attrs.span())?;
106
107 for attr in attr_list.mixed() {
109 let Some(attr) = attr.meta_item() else {
110 cx.adcx().expected_identifier(attr.span());
111 return None;
112 };
113 cx.expect_no_args(attr.args())?;
114 let Some(ident) = attr.path().word() else {
115 cx.adcx().expected_identifier(attr.path().span());
116 return None;
117 };
118 if !ident.name.can_be_raw() {
119 cx.adcx().expected_identifier(ident.span);
120 return None;
121 }
122 if rustc_feature::is_builtin_attr_name(ident.name) {
123 cx.emit_lint(
124 AMBIGUOUS_DERIVE_HELPERS,
125 crate::diagnostics::AmbiguousDeriveHelpers,
126 ident.span,
127 );
128 }
129 attributes.push(ident.name);
130 }
131 }
132
133 if let Some(next) = items.next() {
135 cx.adcx().expected_no_args(next.span());
136 }
137
138 Some((Some(trait_ident.name), attributes))
139}