Skip to main content

rustc_attr_parsing/
interface.rs

1use std::convert::identity;
2
3use rustc_ast as ast;
4use rustc_ast::token::DocFragmentKind;
5use rustc_ast::{AttrItemKind, AttrStyle, CRATE_NODE_ID, NodeId, Safety};
6use rustc_data_structures::sync::{DynSend, DynSync};
7use rustc_errors::{Diag, DiagCtxtHandle, Level, MultiSpan};
8use rustc_feature::{AttributeTemplate, Features};
9use rustc_hir::attrs::AttributeKind;
10use rustc_hir::lints::AttributeLintKind;
11use rustc_hir::{AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId, Target};
12use rustc_session::Session;
13use rustc_session::lint::LintId;
14use rustc_span::{DUMMY_SP, Span, Symbol, sym};
15
16use crate::attributes::AttributeSafety;
17use crate::context::{AcceptContext, FinalizeContext, FinalizeFn, SharedContext, Stage};
18use crate::early_parsed::{EARLY_PARSED_ATTRIBUTES, EarlyParsedState};
19use crate::parser::{AllowExprMetavar, ArgParser, PathParser, RefPathParser};
20use crate::session_diagnostics::ParsedDescription;
21use crate::{Early, Late, OmitDoc, ShouldEmit};
22
23pub enum EmitAttribute {
24    Static(AttributeLintKind),
25    Dynamic(
26        Box<
27            dyn for<'a> Fn(DiagCtxtHandle<'a>, Level) -> Diag<'a, ()> + DynSend + DynSync + 'static,
28        >,
29    ),
30}
31
32/// Context created once, for example as part of the ast lowering
33/// context, through which all attributes can be lowered.
34pub struct AttributeParser<'sess, S: Stage = Late> {
35    pub(crate) tools: Vec<Symbol>,
36    pub(crate) features: Option<&'sess Features>,
37    pub(crate) sess: &'sess Session,
38    pub(crate) stage: S,
39
40    /// *Only* parse attributes with this symbol.
41    ///
42    /// Used in cases where we want the lowering infrastructure for parse just a single attribute.
43    parse_only: Option<&'static [Symbol]>,
44}
45
46impl<'sess> AttributeParser<'sess, Early> {
47    /// This method allows you to parse attributes *before* you have access to features or tools.
48    /// One example where this is necessary, is to parse `feature` attributes themselves for
49    /// example.
50    ///
51    /// Try to use this as little as possible. Attributes *should* be lowered during
52    /// `rustc_ast_lowering`. Some attributes require access to features to parse, which would
53    /// crash if you tried to do so through [`parse_limited`](Self::parse_limited).
54    ///
55    /// To make sure use is limited, supply a `Symbol` you'd like to parse. Only attributes with
56    /// that symbol are picked out of the list of instructions and parsed. Those are returned.
57    ///
58    /// No diagnostics will be emitted when parsing limited. Lints are not emitted at all, while
59    /// errors will be emitted as a delayed bugs. in other words, we *expect* attributes parsed
60    /// with `parse_limited` to be reparsed later during ast lowering where we *do* emit the errors
61    pub fn parse_limited(
62        sess: &'sess Session,
63        attrs: &[ast::Attribute],
64        sym: &'static [Symbol],
65    ) -> Option<Attribute> {
66        Self::parse_limited_should_emit(
67            sess,
68            attrs,
69            sym,
70            // Because we're not emitting warnings/errors, the target should not matter
71            DUMMY_SP,
72            CRATE_NODE_ID,
73            Target::Crate,
74            None,
75            ShouldEmit::Nothing,
76        )
77    }
78
79    /// This does the same as `parse_limited`, except it has a `should_emit` parameter which allows it to emit errors.
80    /// Usually you want `parse_limited`, which emits no errors.
81    pub fn parse_limited_should_emit(
82        sess: &'sess Session,
83        attrs: &[ast::Attribute],
84        sym: &'static [Symbol],
85        target_span: Span,
86        target_node_id: NodeId,
87        target: Target,
88        features: Option<&'sess Features>,
89        should_emit: ShouldEmit,
90    ) -> Option<Attribute> {
91        let mut parsed = Self::parse_limited_all(
92            sess,
93            attrs,
94            Some(sym),
95            target,
96            target_span,
97            target_node_id,
98            features,
99            should_emit,
100        );
101        if !(parsed.len() <= 1) {
    ::core::panicking::panic("assertion failed: parsed.len() <= 1")
};assert!(parsed.len() <= 1);
102        parsed.pop()
103    }
104
105    /// This method allows you to parse a list of attributes *before* `rustc_ast_lowering`.
106    /// This can be used for attributes that would be removed before `rustc_ast_lowering`, such as attributes on macro calls.
107    ///
108    /// Try to use this as little as possible. Attributes *should* be lowered during
109    /// `rustc_ast_lowering`. Some attributes require access to features to parse, which would
110    /// crash if you tried to do so through [`parse_limited_all`](Self::parse_limited_all).
111    /// Therefore, if `parse_only` is None, then features *must* be provided.
112    pub fn parse_limited_all(
113        sess: &'sess Session,
114        attrs: &[ast::Attribute],
115        parse_only: Option<&'static [Symbol]>,
116        target: Target,
117        target_span: Span,
118        target_node_id: NodeId,
119        features: Option<&'sess Features>,
120        emit_errors: ShouldEmit,
121    ) -> Vec<Attribute> {
122        let mut p =
123            Self { features, tools: Vec::new(), parse_only, sess, stage: Early { emit_errors } };
124        p.parse_attribute_list(
125            attrs,
126            target_span,
127            target,
128            OmitDoc::Skip,
129            std::convert::identity,
130            |lint_id, span, kind| match kind {
131                EmitAttribute::Static(kind) => {
132                    sess.psess.buffer_lint(lint_id.lint, span, target_node_id, kind)
133                }
134                EmitAttribute::Dynamic(callback) => {
135                    sess.psess.dyn_buffer_lint(lint_id.lint, span, target_node_id, callback)
136                }
137            },
138        )
139    }
140
141    /// This method parses a single attribute, using `parse_fn`.
142    /// This is useful if you already know what exact attribute this is, and want to parse it.
143    pub fn parse_single<T>(
144        sess: &'sess Session,
145        attr: &ast::Attribute,
146        target_span: Span,
147        target_node_id: NodeId,
148        target: Target,
149        features: Option<&'sess Features>,
150        emit_errors: ShouldEmit,
151        parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &ArgParser) -> Option<T>,
152        template: &AttributeTemplate,
153        allow_expr_metavar: AllowExprMetavar,
154        expected_safety: AttributeSafety,
155    ) -> Option<T> {
156        let ast::AttrKind::Normal(normal_attr) = &attr.kind else {
157            {
    ::core::panicking::panic_fmt(format_args!("parse_single called on a doc attr"));
}panic!("parse_single called on a doc attr")
158        };
159        let parts =
160            normal_attr.item.path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>();
161
162        let path = AttrPath::from_ast(&normal_attr.item.path, identity);
163        let args = ArgParser::from_attr_args(
164            &normal_attr.item.args.unparsed_ref().unwrap(),
165            &parts,
166            &sess.psess,
167            emit_errors,
168            allow_expr_metavar,
169        )?;
170        Self::parse_single_args(
171            sess,
172            attr.span,
173            normal_attr.item.span(),
174            attr.style,
175            path,
176            Some(normal_attr.item.unsafety),
177            expected_safety,
178            ParsedDescription::Attribute,
179            target_span,
180            target_node_id,
181            target,
182            features,
183            emit_errors,
184            &args,
185            parse_fn,
186            template,
187        )
188    }
189
190    /// This method is equivalent to `parse_single`, but parses arguments using `parse_fn` using manually created `args`.
191    /// This is useful when you want to parse other things than attributes using attribute parsers.
192    pub fn parse_single_args<T, I>(
193        sess: &'sess Session,
194        attr_span: Span,
195        inner_span: Span,
196        attr_style: AttrStyle,
197        attr_path: AttrPath,
198        attr_safety: Option<Safety>,
199        expected_safety: AttributeSafety,
200        parsed_description: ParsedDescription,
201        target_span: Span,
202        target_node_id: NodeId,
203        target: Target,
204        features: Option<&'sess Features>,
205        emit_errors: ShouldEmit,
206        args: &I,
207        parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &I) -> T,
208        template: &AttributeTemplate,
209    ) -> T {
210        let mut parser = Self {
211            features,
212            tools: Vec::new(),
213            parse_only: None,
214            sess,
215            stage: Early { emit_errors },
216        };
217        let mut emit_lint = |lint_id: LintId, span: MultiSpan, kind: EmitAttribute| match kind {
218            EmitAttribute::Static(kind) => {
219                sess.psess.buffer_lint(lint_id.lint, span, target_node_id, kind)
220            }
221            EmitAttribute::Dynamic(callback) => {
222                sess.psess.dyn_buffer_lint(lint_id.lint, span, target_node_id, callback)
223            }
224        };
225        if let Some(safety) = attr_safety {
226            parser.check_attribute_safety(
227                &attr_path,
228                inner_span,
229                safety,
230                expected_safety,
231                &mut emit_lint,
232            );
233        }
234        let mut cx: AcceptContext<'_, 'sess, Early> = AcceptContext {
235            shared: SharedContext {
236                cx: &mut parser,
237                target_span,
238                target,
239                emit_lint: &mut emit_lint,
240            },
241            attr_span,
242            inner_span,
243            attr_style,
244            parsed_description,
245            template,
246            attr_path,
247        };
248        parse_fn(&mut cx, args)
249    }
250}
251
252impl<'sess, S: Stage> AttributeParser<'sess, S> {
253    pub fn new(
254        sess: &'sess Session,
255        features: &'sess Features,
256        tools: Vec<Symbol>,
257        stage: S,
258    ) -> Self {
259        Self { features: Some(features), tools, parse_only: None, sess, stage }
260    }
261
262    pub(crate) fn sess(&self) -> &'sess Session {
263        &self.sess
264    }
265
266    pub(crate) fn features(&self) -> &'sess Features {
267        self.features.expect("features not available at this point in the compiler")
268    }
269
270    pub(crate) fn features_option(&self) -> Option<&'sess Features> {
271        self.features
272    }
273
274    pub(crate) fn dcx(&self) -> DiagCtxtHandle<'sess> {
275        self.sess().dcx()
276    }
277
278    /// Parse a list of attributes.
279    ///
280    /// `target_span` is the span of the thing this list of attributes is applied to,
281    /// and when `omit_doc` is set, doc attributes are filtered out.
282    pub fn parse_attribute_list(
283        &mut self,
284        attrs: &[ast::Attribute],
285        target_span: Span,
286        target: Target,
287        omit_doc: OmitDoc,
288        lower_span: impl Copy + Fn(Span) -> Span,
289        mut emit_lint: impl FnMut(LintId, MultiSpan, EmitAttribute),
290    ) -> Vec<Attribute> {
291        let mut attributes = Vec::new();
292        // We store the attributes we intend to discard at the end of this function in order to
293        // check they are applied to the right target and error out if necessary. In practice, we
294        // end up dropping only derive attributes and derive helpers, both being fully processed
295        // at macro expansion.
296        let mut dropped_attributes = Vec::new();
297        let mut attr_paths: Vec<RefPathParser<'_>> = Vec::new();
298        let mut early_parsed_state = EarlyParsedState::default();
299
300        let mut finalizers: Vec<&FinalizeFn<S>> = Vec::with_capacity(attrs.len());
301
302        for attr in attrs {
303            // If we're only looking for a single attribute, skip all the ones we don't care about.
304            if let Some(expected) = self.parse_only {
305                if !attr.path_matches(expected) {
306                    continue;
307                }
308            }
309
310            // Sometimes, for example for `#![doc = include_str!("readme.md")]`,
311            // doc still contains a non-literal. You might say, when we're lowering attributes
312            // that's expanded right? But no, sometimes, when parsing attributes on macros,
313            // we already use the lowering logic and these are still there. So, when `omit_doc`
314            // is set we *also* want to ignore these.
315            let is_doc_attribute = attr.has_name(sym::doc);
316            if omit_doc == OmitDoc::Skip && is_doc_attribute {
317                continue;
318            }
319
320            let attr_span = lower_span(attr.span);
321            match &attr.kind {
322                ast::AttrKind::DocComment(comment_kind, symbol) => {
323                    if omit_doc == OmitDoc::Skip {
324                        continue;
325                    }
326
327                    attributes.push(Attribute::Parsed(AttributeKind::DocComment {
328                        style: attr.style,
329                        kind: DocFragmentKind::Sugared(*comment_kind),
330                        span: attr_span,
331                        comment: *symbol,
332                    }));
333                }
334                ast::AttrKind::Normal(n) => {
335                    attr_paths.push(PathParser(&n.item.path));
336                    let attr_path = AttrPath::from_ast(&n.item.path, lower_span);
337
338                    let args = match &n.item.args {
339                        AttrItemKind::Unparsed(args) => args,
340                        AttrItemKind::Parsed(parsed) => {
341                            early_parsed_state
342                                .accept_early_parsed_attribute(attr_span, lower_span, parsed);
343                            continue;
344                        }
345                    };
346
347                    let parts =
348                        n.item.path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>();
349
350                    if let Some(accept) = S::parsers().accepters.get(parts.as_slice()) {
351                        self.check_attribute_safety(
352                            &attr_path,
353                            lower_span(n.item.span()),
354                            n.item.unsafety,
355                            accept.safety,
356                            &mut emit_lint,
357                        );
358
359                        let Some(args) = ArgParser::from_attr_args(
360                            args,
361                            &parts,
362                            &self.sess.psess,
363                            self.stage.should_emit(),
364                            AllowExprMetavar::No,
365                        ) else {
366                            continue;
367                        };
368
369                        // Special-case handling for `#[doc = "..."]`: if we go through with
370                        // `DocParser`, the order of doc comments will be messed up because `///`
371                        // doc comments are added into `attributes` whereas attributes parsed with
372                        // `DocParser` are added into `parsed_attributes` which are then appended
373                        // to `attributes`. So if you have:
374                        //
375                        // /// bla
376                        // #[doc = "a"]
377                        // /// blob
378                        //
379                        // You would get:
380                        //
381                        // bla
382                        // blob
383                        // a
384                        if is_doc_attribute
385                            && let ArgParser::NameValue(nv) = &args
386                            // If not a string key/value, it should emit an error, but to make
387                            // things simpler, it's handled in `DocParser` because it's simpler to
388                            // emit an error with `AcceptContext`.
389                            && let Some(comment) = nv.value_as_str()
390                        {
391                            attributes.push(Attribute::Parsed(AttributeKind::DocComment {
392                                style: attr.style,
393                                kind: DocFragmentKind::Raw(nv.value_span),
394                                span: attr_span,
395                                comment,
396                            }));
397                            continue;
398                        }
399
400                        let mut cx: AcceptContext<'_, 'sess, S> = AcceptContext {
401                            shared: SharedContext {
402                                cx: self,
403                                target_span,
404                                target,
405                                emit_lint: &mut emit_lint,
406                            },
407                            attr_span,
408                            inner_span: lower_span(n.item.span()),
409                            attr_style: attr.style,
410                            parsed_description: ParsedDescription::Attribute,
411                            template: &accept.template,
412                            attr_path: attr_path.clone(),
413                        };
414
415                        (accept.accept_fn)(&mut cx, &args);
416                        finalizers.push(&accept.finalizer);
417
418                        if !#[allow(non_exhaustive_omitted_patterns)] match cx.stage.should_emit() {
    ShouldEmit::Nothing => true,
    _ => false,
}matches!(cx.stage.should_emit(), ShouldEmit::Nothing) {
419                            Self::check_target(&accept.allowed_targets, target, &mut cx);
420                        }
421                    } else {
422                        let attr = AttrItem {
423                            path: attr_path.clone(),
424                            args: self
425                                .lower_attr_args(n.item.args.unparsed_ref().unwrap(), lower_span),
426                            id: HashIgnoredAttrId { attr_id: attr.id },
427                            style: attr.style,
428                            span: attr_span,
429                        };
430
431                        self.check_attribute_safety(
432                            &attr_path,
433                            lower_span(n.item.span()),
434                            n.item.unsafety,
435                            AttributeSafety::Normal,
436                            &mut emit_lint,
437                        );
438
439                        if !#[allow(non_exhaustive_omitted_patterns)] match self.stage.should_emit() {
    ShouldEmit::Nothing => true,
    _ => false,
}matches!(self.stage.should_emit(), ShouldEmit::Nothing)
440                            && target == Target::Crate
441                        {
442                            self.check_invalid_crate_level_attr_item(&attr, n.item.span());
443                        }
444
445                        let attr = Attribute::Unparsed(Box::new(attr));
446
447                        if self.tools.contains(&parts[0])
448                            // FIXME: this can be removed once #152369 has been merged.
449                            // https://github.com/rust-lang/rust/pull/152369
450                            || [sym::allow, sym::deny, sym::expect, sym::forbid, sym::warn]
451                                .contains(&parts[0])
452                        {
453                            attributes.push(attr);
454                        } else {
455                            dropped_attributes.push(attr);
456                        }
457                    }
458                }
459            }
460        }
461
462        early_parsed_state.finalize_early_parsed_attributes(&mut attributes);
463        for f in &finalizers {
464            if let Some(attr) = f(&mut FinalizeContext {
465                shared: SharedContext { cx: self, target_span, target, emit_lint: &mut emit_lint },
466                all_attrs: &attr_paths,
467            }) {
468                attributes.push(Attribute::Parsed(attr));
469            }
470        }
471
472        if !#[allow(non_exhaustive_omitted_patterns)] match self.stage.should_emit() {
    ShouldEmit::Nothing => true,
    _ => false,
}matches!(self.stage.should_emit(), ShouldEmit::Nothing)
473            && target == Target::WherePredicate
474        {
475            self.check_invalid_where_predicate_attrs(attributes.iter().chain(&dropped_attributes));
476        }
477
478        attributes
479    }
480
481    /// Returns whether there is a parser for an attribute with this name
482    pub fn is_parsed_attribute(path: &[Symbol]) -> bool {
483        /// The list of attributes that are parsed attributes,
484        /// even though they don't have a parser in `Late::parsers()`
485        const SPECIAL_ATTRIBUTES: &[&[Symbol]] = &[
486            // Cfg attrs are removed after being early-parsed, so don't need to be in the parser list
487            &[sym::cfg],
488            &[sym::cfg_attr],
489        ];
490
491        Late::parsers().accepters.contains_key(path)
492            || EARLY_PARSED_ATTRIBUTES.contains(&path)
493            || SPECIAL_ATTRIBUTES.contains(&path)
494    }
495
496    fn lower_attr_args(&self, args: &ast::AttrArgs, lower_span: impl Fn(Span) -> Span) -> AttrArgs {
497        match args {
498            ast::AttrArgs::Empty => AttrArgs::Empty,
499            ast::AttrArgs::Delimited(args) => AttrArgs::Delimited(args.clone()),
500            // This is an inert key-value attribute - it will never be visible to macros
501            // after it gets lowered to HIR. Therefore, we can extract literals to handle
502            // nonterminals in `#[doc]` (e.g. `#[doc = $e]`).
503            ast::AttrArgs::Eq { eq_span, expr } => {
504                // In valid code the value always ends up as a single literal. Otherwise, a dummy
505                // literal suffices because the error is handled elsewhere.
506                let lit = if let ast::ExprKind::Lit(token_lit) = expr.kind
507                    && let Ok(lit) =
508                        ast::MetaItemLit::from_token_lit(token_lit, lower_span(expr.span))
509                {
510                    lit
511                } else {
512                    let guar = self.dcx().span_delayed_bug(
513                        args.span().unwrap_or(DUMMY_SP),
514                        "expr in place where literal is expected (builtin attr parsing)",
515                    );
516                    ast::MetaItemLit {
517                        symbol: sym::dummy,
518                        suffix: None,
519                        kind: ast::LitKind::Err(guar),
520                        span: DUMMY_SP,
521                    }
522                };
523                AttrArgs::Eq { eq_span: lower_span(*eq_span), expr: lit }
524            }
525        }
526    }
527}