rustc_attr_parsing/
interface.rs

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