rustc_attr_parsing/attributes/
macro_attrs.rs1use rustc_attr_ir::{MacroUseArgs, find_attr};
2use rustc_feature::AttributeStability;
3use rustc_lint_defs::builtin::INVALID_MACRO_EXPORT_ARGUMENTS;
4use rustc_structures::CollapseMacroDebuginfo;
5
6use super::prelude::*;
7use crate::diagnostics::MacroOnlyAttribute;
8
9pub(crate) struct MacroEscapeParser;
10impl NoArgsAttributeParser for MacroEscapeParser {
11 const PATH: &[Symbol] = &[sym::macro_escape];
12 const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
13 const ALLOWED_TARGETS: AllowedTargets<'_> = MACRO_USE_ALLOWED_TARGETS;
14 const STABILITY: AttributeStability = AttributeStability::Stable;
15 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::MacroEscape;
16}
17
18#[derive(#[automatically_derived]
impl ::core::default::Default for MacroUseParser {
#[inline]
fn default() -> Self {
Self {
state: ::core::default::Default::default(),
uses_attr_spans: ::core::default::Default::default(),
first_span: ::core::default::Default::default(),
}
}
}Default)]
23pub(crate) struct MacroUseParser {
24 state: MacroUseArgs,
25
26 uses_attr_spans: ThinVec<Span>,
28 first_span: Option<Span>,
31}
32
33const MACRO_USE_TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: true,
list: Some(&["name1, name2, ..."]),
one_of: &[],
name_value_str: None,
docs: Some("https://doc.rust-lang.org/reference/macros-by-example.html#the-macro_use-attribute"),
}template!(
34 Word, List: &["name1, name2, ..."],
35 "https://doc.rust-lang.org/reference/macros-by-example.html#the-macro_use-attribute"
36);
37const MACRO_USE_ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowListWarnRest(&[
38 Allow(Target::Mod),
39 Allow(Target::ExternCrate),
40 Error(Target::WherePredicate),
41]);
42
43impl AttributeParser for MacroUseParser {
44 const ATTRIBUTES: AcceptMapping<Self> = &[(
45 &[sym::macro_use],
46 MACRO_USE_TEMPLATE,
47 AttributeStability::Stable,
48 |group: &mut Self, cx: &mut AcceptContext<'_, '_>, args| {
49 let span = cx.attr_span;
50 group.first_span.get_or_insert(span);
51 match args {
52 ArgParser::NoArgs => {
53 match group.state {
54 MacroUseArgs::UseAll => {
55 let first_span = group.first_span.expect(
56 "State is UseAll is some so this is not the first attribute",
57 );
58 cx.warn_unused_duplicate(first_span, span);
60 }
61 MacroUseArgs::UseSpecific(_) => {
62 group.state = MacroUseArgs::UseAll;
63 group.first_span = Some(span);
64 for specific_use in group.uses_attr_spans.drain(..) {
66 cx.warn_unused_duplicate(span, specific_use);
67 }
68 }
69 }
70 }
71 ArgParser::List(list) => {
72 if list.is_empty() {
73 cx.adcx().warn_empty_attribute(list.span);
74 return;
75 }
76
77 match &mut group.state {
78 MacroUseArgs::UseAll => {
79 let first_span = group.first_span.expect(
80 "State is UseAll is some so this is not the first attribute",
81 );
82 cx.warn_unused_duplicate(first_span, span);
83 }
84 MacroUseArgs::UseSpecific(arguments) => {
85 group.uses_attr_spans.push(cx.attr_span);
87
88 for item in list.mixed() {
89 let Some(item) = item.meta_item() else {
90 cx.adcx().expected_identifier(item.span());
91 continue;
92 };
93 let Some(()) = cx.expect_no_args(item.args()) else {
94 continue;
95 };
96 let Some(item) = item.path().word() else {
97 cx.adcx().expected_identifier(item.span());
98 continue;
99 };
100 arguments.push(item);
101 }
102 }
103 }
104 }
105 ArgParser::NameValue(nv) => {
106 cx.adcx().expected_list_or_no_args(nv.args_span());
107 }
108 }
109 },
110 )];
111 const ALLOWED_TARGETS: AllowedTargets<'_> = MACRO_USE_ALLOWED_TARGETS;
112
113 fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
114 Some(AttributeKind::MacroUse { span: self.first_span?, arguments: self.state })
115 }
116}
117
118pub(crate) fn check_macro_only(cx: &FinalizeCheckContext<'_, '_>, attr_span: Span) {
122 if cx.target == Target::Fn
123 && !{
{
'done:
{
for i in cx.parsed_attrs {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(ProcMacro |
ProcMacroAttribute | ProcMacroDerive { .. }) => {
break 'done Some(());
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}.is_some()
}find_attr!(cx.parsed_attrs, ProcMacro | ProcMacroAttribute | ProcMacroDerive { .. })
124 {
125 cx.emit_err(MacroOnlyAttribute { attr_span, span: cx.target_span });
126 }
127}
128
129pub(crate) struct AllowInternalUnsafeParser;
130
131impl NoArgsAttributeParser for AllowInternalUnsafeParser {
132 const PATH: &[Symbol] = &[sym::allow_internal_unsafe];
133 const ON_DUPLICATE: OnDuplicate = OnDuplicate::Ignore;
134 const ALLOWED_TARGETS: AllowedTargets<'_> =
135 AllowedTargets::AllowList(&[Allow(Target::Fn), Allow(Target::MacroDef)]);
136 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::allow_internal_unsafe;
AttributeStability::Unstable {
gate_name: rustc_span::sym::allow_internal_unsafe,
notes: &[],
}
}unstable!(allow_internal_unsafe);
137 const CREATE: fn(Span) -> AttributeKind = |span| AttributeKind::AllowInternalUnsafe(span);
138
139 fn finalize_check(cx: &FinalizeCheckContext<'_, '_>, attr_span: Span) {
140 check_macro_only(cx, attr_span);
141 }
142}
143
144pub(crate) struct MacroExportParser;
145
146impl SingleAttributeParser for MacroExportParser {
147 const PATH: &[Symbol] = &[sym::macro_export];
148 const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
149 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: true,
list: Some(&["local_inner_macros"]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(Word, List: &["local_inner_macros"]);
150 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowListWarnRest(&[
151 Allow(Target::MacroDef),
152 Error(Target::WherePredicate),
153 Error(Target::Crate),
154 ]);
155 const STABILITY: AttributeStability = AttributeStability::Stable;
156
157 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
158 let local_inner_macros = match args {
159 ArgParser::NoArgs => false,
160 ArgParser::List(list) => {
161 let Some(l) = list.as_single() else {
162 cx.adcx().warn_ill_formed_attribute_input(INVALID_MACRO_EXPORT_ARGUMENTS);
163 return None;
164 };
165 if l.meta_item_no_args().is_some_and(|m| m.path().word_is(sym::local_inner_macros))
166 {
167 true
168 } else {
169 cx.adcx().warn_ill_formed_attribute_input(INVALID_MACRO_EXPORT_ARGUMENTS);
170 return None;
171 }
172 }
173 ArgParser::NameValue(nv) => {
174 cx.adcx().expected_list_or_no_args(nv.args_span());
175 return None;
176 }
177 };
178 Some(AttributeKind::MacroExport { span: cx.attr_span, local_inner_macros })
179 }
180}
181
182pub(crate) struct CollapseDebugInfoParser;
183
184impl SingleAttributeParser for CollapseDebugInfoParser {
185 const PATH: &[Symbol] = &[sym::collapse_debuginfo];
186 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: false,
list: Some(&["no", "external", "yes"]),
one_of: &[],
name_value_str: None,
docs: Some("https://doc.rust-lang.org/reference/attributes/debugger.html#the-collapse_debuginfo-attribute"),
}template!(
187 List: &["no", "external", "yes"],
188 "https://doc.rust-lang.org/reference/attributes/debugger.html#the-collapse_debuginfo-attribute"
189 );
190 const ALLOWED_TARGETS: AllowedTargets<'_> =
191 AllowedTargets::AllowList(&[Allow(Target::MacroDef)]);
192 const STABILITY: AttributeStability = AttributeStability::Stable;
193
194 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
195 let single = cx.expect_single_element_list(args, cx.attr_span)?;
196 let Some(mi) = single.meta_item() else {
197 cx.adcx().expected_not_literal(single.span());
198 return None;
199 };
200 let _ = cx.expect_no_args(mi.args());
201 let path = mi.path().word_sym();
202 let info = match path {
203 Some(sym::yes) => CollapseMacroDebuginfo::Yes,
204 Some(sym::no) => CollapseMacroDebuginfo::No,
205 Some(sym::external) => CollapseMacroDebuginfo::External,
206 _ => {
207 cx.adcx()
208 .expected_specific_argument(mi.span(), &[sym::yes, sym::no, sym::external]);
209 return None;
210 }
211 };
212
213 Some(AttributeKind::CollapseDebugInfo(info))
214 }
215}
216
217pub(crate) struct RustcProcMacroDeclsParser;
218
219impl NoArgsAttributeParser for RustcProcMacroDeclsParser {
220 const PATH: &[Symbol] = &[sym::rustc_proc_macro_decls];
221 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Static)]);
222 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
223 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcProcMacroDecls;
224}