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