rustc_attr_parsing/interface.rs
1use std::borrow::Cow;
2
3use rustc_ast as ast;
4use rustc_ast::{AttrStyle, NodeId};
5use rustc_errors::DiagCtxtHandle;
6use rustc_feature::{AttributeTemplate, Features};
7use rustc_hir::attrs::AttributeKind;
8use rustc_hir::lints::AttributeLint;
9use rustc_hir::{AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId, Target};
10use rustc_session::Session;
11use rustc_span::{DUMMY_SP, Span, Symbol, sym};
12
13use crate::context::{AcceptContext, FinalizeContext, SharedContext, Stage};
14use crate::parser::{ArgParser, MetaItemParser, PathParser};
15use crate::{Early, Late, OmitDoc, ShouldEmit};
16
17/// Context created once, for example as part of the ast lowering
18/// context, through which all attributes can be lowered.
19pub struct AttributeParser<'sess, S: Stage = Late> {
20 pub(crate) tools: Vec<Symbol>,
21 pub(crate) features: Option<&'sess Features>,
22 pub(crate) sess: &'sess Session,
23 pub(crate) stage: S,
24
25 /// *Only* parse attributes with this symbol.
26 ///
27 /// Used in cases where we want the lowering infrastructure for parse just a single attribute.
28 parse_only: Option<Symbol>,
29}
30
31impl<'sess> AttributeParser<'sess, Early> {
32 /// This method allows you to parse attributes *before* you have access to features or tools.
33 /// One example where this is necessary, is to parse `feature` attributes themselves for
34 /// example.
35 ///
36 /// Try to use this as little as possible. Attributes *should* be lowered during
37 /// `rustc_ast_lowering`. Some attributes require access to features to parse, which would
38 /// crash if you tried to do so through [`parse_limited`](Self::parse_limited).
39 ///
40 /// To make sure use is limited, supply a `Symbol` you'd like to parse. Only attributes with
41 /// that symbol are picked out of the list of instructions and parsed. Those are returned.
42 ///
43 /// No diagnostics will be emitted when parsing limited. Lints are not emitted at all, while
44 /// errors will be emitted as a delayed bugs. in other words, we *expect* attributes parsed
45 /// with `parse_limited` to be reparsed later during ast lowering where we *do* emit the errors
46 pub fn parse_limited(
47 sess: &'sess Session,
48 attrs: &[ast::Attribute],
49 sym: Symbol,
50 target_span: Span,
51 target_node_id: NodeId,
52 features: Option<&'sess Features>,
53 ) -> Option<Attribute> {
54 Self::parse_limited_should_emit(
55 sess,
56 attrs,
57 sym,
58 target_span,
59 target_node_id,
60 features,
61 ShouldEmit::Nothing,
62 )
63 }
64
65 /// This does the same as `parse_limited`, except it has a `should_emit` parameter which allows it to emit errors.
66 /// Usually you want `parse_limited`, which emits no errors.
67 pub fn parse_limited_should_emit(
68 sess: &'sess Session,
69 attrs: &[ast::Attribute],
70 sym: Symbol,
71 target_span: Span,
72 target_node_id: NodeId,
73 features: Option<&'sess Features>,
74 should_emit: ShouldEmit,
75 ) -> Option<Attribute> {
76 let mut parsed = Self::parse_limited_all(
77 sess,
78 attrs,
79 Some(sym),
80 Target::Crate, // Does not matter, we're not going to emit errors anyways
81 target_span,
82 target_node_id,
83 features,
84 should_emit,
85 );
86 assert!(parsed.len() <= 1);
87 parsed.pop()
88 }
89
90 /// This method allows you to parse a list of attributes *before* `rustc_ast_lowering`.
91 /// This can be used for attributes that would be removed before `rustc_ast_lowering`, such as attributes on macro calls.
92 ///
93 /// Try to use this as little as possible. Attributes *should* be lowered during
94 /// `rustc_ast_lowering`. Some attributes require access to features to parse, which would
95 /// crash if you tried to do so through [`parse_limited_all`](Self::parse_limited_all).
96 /// Therefore, if `parse_only` is None, then features *must* be provided.
97 pub fn parse_limited_all(
98 sess: &'sess Session,
99 attrs: &[ast::Attribute],
100 parse_only: Option<Symbol>,
101 target: Target,
102 target_span: Span,
103 target_node_id: NodeId,
104 features: Option<&'sess Features>,
105 emit_errors: ShouldEmit,
106 ) -> Vec<Attribute> {
107 let mut p =
108 Self { features, tools: Vec::new(), parse_only, sess, stage: Early { emit_errors } };
109 p.parse_attribute_list(
110 attrs,
111 target_span,
112 target_node_id,
113 target,
114 OmitDoc::Skip,
115 std::convert::identity,
116 |lint| {
117 crate::lints::emit_attribute_lint(&lint, sess);
118 },
119 )
120 }
121
122 /// This method parses a single attribute, using `parse_fn`.
123 /// This is useful if you already know what exact attribute this is, and want to parse it.
124 pub fn parse_single<T>(
125 sess: &'sess Session,
126 attr: &ast::Attribute,
127 target_span: Span,
128 target_node_id: NodeId,
129 features: Option<&'sess Features>,
130 emit_errors: ShouldEmit,
131 parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &ArgParser<'_>) -> Option<T>,
132 template: &AttributeTemplate,
133 ) -> Option<T> {
134 let ast::AttrKind::Normal(normal_attr) = &attr.kind else {
135 panic!("parse_single called on a doc attr")
136 };
137 let parts =
138 normal_attr.item.path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>();
139 let meta_parser = MetaItemParser::from_attr(normal_attr, &parts, &sess.psess, emit_errors)?;
140 let path = meta_parser.path();
141 let args = meta_parser.args();
142 Self::parse_single_args(
143 sess,
144 attr.span,
145 normal_attr.item.span(),
146 attr.style,
147 path.get_attribute_path(),
148 target_span,
149 target_node_id,
150 features,
151 emit_errors,
152 args,
153 parse_fn,
154 template,
155 )
156 }
157
158 /// This method is equivalent to `parse_single`, but parses arguments using `parse_fn` using manually created `args`.
159 /// This is useful when you want to parse other things than attributes using attribute parsers.
160 pub fn parse_single_args<T, I>(
161 sess: &'sess Session,
162 attr_span: Span,
163 inner_span: Span,
164 attr_style: AttrStyle,
165 attr_path: AttrPath,
166 target_span: Span,
167 target_node_id: NodeId,
168 features: Option<&'sess Features>,
169 emit_errors: ShouldEmit,
170 args: &I,
171 parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &I) -> Option<T>,
172 template: &AttributeTemplate,
173 ) -> Option<T> {
174 let mut parser = Self {
175 features,
176 tools: Vec::new(),
177 parse_only: None,
178 sess,
179 stage: Early { emit_errors },
180 };
181 let mut cx: AcceptContext<'_, 'sess, Early> = AcceptContext {
182 shared: SharedContext {
183 cx: &mut parser,
184 target_span,
185 target_id: target_node_id,
186 emit_lint: &mut |lint| {
187 crate::lints::emit_attribute_lint(&lint, sess);
188 },
189 },
190 attr_span,
191 inner_span,
192 attr_style,
193 template,
194 attr_path,
195 };
196 parse_fn(&mut cx, args)
197 }
198}
199
200impl<'sess, S: Stage> AttributeParser<'sess, S> {
201 pub fn new(
202 sess: &'sess Session,
203 features: &'sess Features,
204 tools: Vec<Symbol>,
205 stage: S,
206 ) -> Self {
207 Self { features: Some(features), tools, parse_only: None, sess, stage }
208 }
209
210 pub(crate) fn sess(&self) -> &'sess Session {
211 &self.sess
212 }
213
214 pub(crate) fn features(&self) -> &'sess Features {
215 self.features.expect("features not available at this point in the compiler")
216 }
217
218 pub(crate) fn features_option(&self) -> Option<&'sess Features> {
219 self.features
220 }
221
222 pub(crate) fn dcx(&self) -> DiagCtxtHandle<'sess> {
223 self.sess().dcx()
224 }
225
226 /// Parse a list of attributes.
227 ///
228 /// `target_span` is the span of the thing this list of attributes is applied to,
229 /// and when `omit_doc` is set, doc attributes are filtered out.
230 pub fn parse_attribute_list(
231 &mut self,
232 attrs: &[ast::Attribute],
233 target_span: Span,
234 target_id: S::Id,
235 target: Target,
236 omit_doc: OmitDoc,
237
238 lower_span: impl Copy + Fn(Span) -> Span,
239 mut emit_lint: impl FnMut(AttributeLint<S::Id>),
240 ) -> Vec<Attribute> {
241 let mut attributes = Vec::new();
242 let mut attr_paths = Vec::new();
243
244 for attr in attrs {
245 // If we're only looking for a single attribute, skip all the ones we don't care about.
246 if let Some(expected) = self.parse_only {
247 if !attr.has_name(expected) {
248 continue;
249 }
250 }
251
252 // Sometimes, for example for `#![doc = include_str!("readme.md")]`,
253 // doc still contains a non-literal. You might say, when we're lowering attributes
254 // that's expanded right? But no, sometimes, when parsing attributes on macros,
255 // we already use the lowering logic and these are still there. So, when `omit_doc`
256 // is set we *also* want to ignore these.
257 if omit_doc == OmitDoc::Skip && attr.has_name(sym::doc) {
258 continue;
259 }
260
261 match &attr.kind {
262 ast::AttrKind::DocComment(comment_kind, symbol) => {
263 if omit_doc == OmitDoc::Skip {
264 continue;
265 }
266
267 attributes.push(Attribute::Parsed(AttributeKind::DocComment {
268 style: attr.style,
269 kind: *comment_kind,
270 span: lower_span(attr.span),
271 comment: *symbol,
272 }))
273 }
274 // // FIXME: make doc attributes go through a proper attribute parser
275 // ast::AttrKind::Normal(n) if n.has_name(sym::doc) => {
276 // let p = GenericMetaItemParser::from_attr(&n, self.dcx());
277 //
278 // attributes.push(Attribute::Parsed(AttributeKind::DocComment {
279 // style: attr.style,
280 // kind: CommentKind::Line,
281 // span: attr.span,
282 // comment: p.args().name_value(),
283 // }))
284 // }
285 ast::AttrKind::Normal(n) => {
286 attr_paths.push(PathParser(Cow::Borrowed(&n.item.path)));
287
288 let parts =
289 n.item.path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>();
290
291 if let Some(accepts) = S::parsers().accepters.get(parts.as_slice()) {
292 let Some(parser) = MetaItemParser::from_attr(
293 n,
294 &parts,
295 &self.sess.psess,
296 self.stage.should_emit(),
297 ) else {
298 continue;
299 };
300 let path = parser.path();
301 let args = parser.args();
302 for accept in accepts {
303 let mut cx: AcceptContext<'_, 'sess, S> = AcceptContext {
304 shared: SharedContext {
305 cx: self,
306 target_span,
307 target_id,
308 emit_lint: &mut emit_lint,
309 },
310 attr_span: lower_span(attr.span),
311 inner_span: lower_span(attr.get_normal_item().span()),
312 attr_style: attr.style,
313 template: &accept.template,
314 attr_path: path.get_attribute_path(),
315 };
316
317 (accept.accept_fn)(&mut cx, args);
318 if !matches!(cx.stage.should_emit(), ShouldEmit::Nothing) {
319 Self::check_target(&accept.allowed_targets, target, &mut cx);
320 }
321 }
322 } else {
323 // If we're here, we must be compiling a tool attribute... Or someone
324 // forgot to parse their fancy new attribute. Let's warn them in any case.
325 // If you are that person, and you really think your attribute should
326 // remain unparsed, carefully read the documentation in this module and if
327 // you still think so you can add an exception to this assertion.
328
329 // FIXME(jdonszelmann): convert other attributes, and check with this that
330 // we caught em all
331 // const FIXME_TEMPORARY_ATTR_ALLOWLIST: &[Symbol] = &[sym::cfg];
332 // assert!(
333 // self.tools.contains(&parts[0]) || true,
334 // // || FIXME_TEMPORARY_ATTR_ALLOWLIST.contains(&parts[0]),
335 // "attribute {path} wasn't parsed and isn't a know tool attribute",
336 // );
337
338 attributes.push(Attribute::Unparsed(Box::new(AttrItem {
339 path: AttrPath::from_ast(&n.item.path),
340 args: self.lower_attr_args(&n.item.args, lower_span),
341 id: HashIgnoredAttrId { attr_id: attr.id },
342 style: attr.style,
343 span: lower_span(attr.span),
344 })));
345 }
346 }
347 }
348 }
349
350 let mut parsed_attributes = Vec::new();
351 for f in &S::parsers().finalizers {
352 if let Some(attr) = f(&mut FinalizeContext {
353 shared: SharedContext {
354 cx: self,
355 target_span,
356 target_id,
357 emit_lint: &mut emit_lint,
358 },
359 all_attrs: &attr_paths,
360 }) {
361 parsed_attributes.push(Attribute::Parsed(attr));
362 }
363 }
364
365 attributes.extend(parsed_attributes);
366
367 attributes
368 }
369
370 /// Returns whether there is a parser for an attribute with this name
371 pub fn is_parsed_attribute(path: &[Symbol]) -> bool {
372 Late::parsers().accepters.contains_key(path)
373 }
374
375 fn lower_attr_args(&self, args: &ast::AttrArgs, lower_span: impl Fn(Span) -> Span) -> AttrArgs {
376 match args {
377 ast::AttrArgs::Empty => AttrArgs::Empty,
378 ast::AttrArgs::Delimited(args) => AttrArgs::Delimited(args.clone()),
379 // This is an inert key-value attribute - it will never be visible to macros
380 // after it gets lowered to HIR. Therefore, we can extract literals to handle
381 // nonterminals in `#[doc]` (e.g. `#[doc = $e]`).
382 ast::AttrArgs::Eq { eq_span, expr } => {
383 // In valid code the value always ends up as a single literal. Otherwise, a dummy
384 // literal suffices because the error is handled elsewhere.
385 let lit = if let ast::ExprKind::Lit(token_lit) = expr.kind
386 && let Ok(lit) =
387 ast::MetaItemLit::from_token_lit(token_lit, lower_span(expr.span))
388 {
389 lit
390 } else {
391 let guar = self.dcx().span_delayed_bug(
392 args.span().unwrap_or(DUMMY_SP),
393 "expr in place where literal is expected (builtin attr parsing)",
394 );
395 ast::MetaItemLit {
396 symbol: sym::dummy,
397 suffix: None,
398 kind: ast::LitKind::Err(guar),
399 span: DUMMY_SP,
400 }
401 };
402 AttrArgs::Eq { eq_span: lower_span(*eq_span), expr: lit }
403 }
404 }
405 }
406}