• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::errors::{InvalidMetaItem, SuffixedLiteralInAttribute};
2 use crate::fluent_generated as fluent;
3 
4 use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle};
5 use rustc_ast as ast;
6 use rustc_ast::attr;
7 use rustc_ast::token::{self, Delimiter, Nonterminal};
8 use rustc_errors::{error_code, Diagnostic, IntoDiagnostic, PResult};
9 use rustc_span::{sym, BytePos, Span};
10 use std::convert::TryInto;
11 use thin_vec::ThinVec;
12 use tracing::debug;
13 
14 // Public for rustfmt usage
15 #[derive(Debug)]
16 pub enum InnerAttrPolicy {
17     Permitted,
18     Forbidden(Option<InnerAttrForbiddenReason>),
19 }
20 
21 #[derive(Clone, Copy, Debug)]
22 pub enum InnerAttrForbiddenReason {
23     InCodeBlock,
24     AfterOuterDocComment { prev_doc_comment_span: Span },
25     AfterOuterAttribute { prev_outer_attr_sp: Span },
26 }
27 
28 enum OuterAttributeType {
29     DocComment,
30     DocBlockComment,
31     Attribute,
32 }
33 
34 impl<'a> Parser<'a> {
35     /// Parses attributes that appear before an item.
parse_outer_attributes(&mut self) -> PResult<'a, AttrWrapper>36     pub(super) fn parse_outer_attributes(&mut self) -> PResult<'a, AttrWrapper> {
37         let mut outer_attrs = ast::AttrVec::new();
38         let mut just_parsed_doc_comment = false;
39         let start_pos = self.token_cursor.num_next_calls;
40         loop {
41             let attr = if self.check(&token::Pound) {
42                 let prev_outer_attr_sp = outer_attrs.last().map(|attr| attr.span);
43 
44                 let inner_error_reason = if just_parsed_doc_comment {
45                     Some(InnerAttrForbiddenReason::AfterOuterDocComment {
46                         prev_doc_comment_span: prev_outer_attr_sp.unwrap(),
47                     })
48                 } else {
49                     prev_outer_attr_sp.map(|prev_outer_attr_sp| {
50                         InnerAttrForbiddenReason::AfterOuterAttribute { prev_outer_attr_sp }
51                     })
52                 };
53                 let inner_parse_policy = InnerAttrPolicy::Forbidden(inner_error_reason);
54                 just_parsed_doc_comment = false;
55                 Some(self.parse_attribute(inner_parse_policy)?)
56             } else if let token::DocComment(comment_kind, attr_style, data) = self.token.kind {
57                 if attr_style != ast::AttrStyle::Outer {
58                     let span = self.token.span;
59                     let mut err = self.sess.span_diagnostic.struct_span_err_with_code(
60                         span,
61                         fluent::parse_inner_doc_comment_not_permitted,
62                         error_code!(E0753),
63                     );
64                     if let Some(replacement_span) = self.annotate_following_item_if_applicable(
65                         &mut err,
66                         span,
67                         match comment_kind {
68                             token::CommentKind::Line => OuterAttributeType::DocComment,
69                             token::CommentKind::Block => OuterAttributeType::DocBlockComment,
70                         },
71                     ) {
72                         err.note(fluent::parse_note);
73                         err.span_suggestion_verbose(
74                             replacement_span,
75                             fluent::parse_suggestion,
76                             "",
77                             rustc_errors::Applicability::MachineApplicable,
78                         );
79                     }
80                     err.emit();
81                 }
82                 self.bump();
83                 just_parsed_doc_comment = true;
84                 // Always make an outer attribute - this allows us to recover from a misplaced
85                 // inner attribute.
86                 Some(attr::mk_doc_comment(
87                     &self.sess.attr_id_generator,
88                     comment_kind,
89                     ast::AttrStyle::Outer,
90                     data,
91                     self.prev_token.span,
92                 ))
93             } else {
94                 None
95             };
96 
97             if let Some(attr) = attr {
98                 if attr.style == ast::AttrStyle::Outer {
99                     outer_attrs.push(attr);
100                 }
101             } else {
102                 break;
103             }
104         }
105         Ok(AttrWrapper::new(outer_attrs, start_pos))
106     }
107 
108     /// Matches `attribute = # ! [ meta_item ]`.
109     /// `inner_parse_policy` prescribes how to handle inner attributes.
110     // Public for rustfmt usage.
parse_attribute( &mut self, inner_parse_policy: InnerAttrPolicy, ) -> PResult<'a, ast::Attribute>111     pub fn parse_attribute(
112         &mut self,
113         inner_parse_policy: InnerAttrPolicy,
114     ) -> PResult<'a, ast::Attribute> {
115         debug!(
116             "parse_attribute: inner_parse_policy={:?} self.token={:?}",
117             inner_parse_policy, self.token
118         );
119         let lo = self.token.span;
120         // Attributes can't have attributes of their own [Editor's note: not with that attitude]
121         self.collect_tokens_no_attrs(|this| {
122             assert!(this.eat(&token::Pound), "parse_attribute called in non-attribute position");
123 
124             let style =
125                 if this.eat(&token::Not) { ast::AttrStyle::Inner } else { ast::AttrStyle::Outer };
126 
127             this.expect(&token::OpenDelim(Delimiter::Bracket))?;
128             let item = this.parse_attr_item(false)?;
129             this.expect(&token::CloseDelim(Delimiter::Bracket))?;
130             let attr_sp = lo.to(this.prev_token.span);
131 
132             // Emit error if inner attribute is encountered and forbidden.
133             if style == ast::AttrStyle::Inner {
134                 this.error_on_forbidden_inner_attr(attr_sp, inner_parse_policy);
135             }
136 
137             Ok(attr::mk_attr_from_item(&self.sess.attr_id_generator, item, None, style, attr_sp))
138         })
139     }
140 
annotate_following_item_if_applicable( &self, err: &mut Diagnostic, span: Span, attr_type: OuterAttributeType, ) -> Option<Span>141     fn annotate_following_item_if_applicable(
142         &self,
143         err: &mut Diagnostic,
144         span: Span,
145         attr_type: OuterAttributeType,
146     ) -> Option<Span> {
147         let mut snapshot = self.create_snapshot_for_diagnostic();
148         let lo = span.lo()
149             + BytePos(match attr_type {
150                 OuterAttributeType::Attribute => 1,
151                 _ => 2,
152             });
153         let hi = lo + BytePos(1);
154         let replacement_span = span.with_lo(lo).with_hi(hi);
155         if let OuterAttributeType::DocBlockComment | OuterAttributeType::DocComment = attr_type {
156             snapshot.bump();
157         }
158         loop {
159             // skip any other attributes, we want the item
160             if snapshot.token.kind == token::Pound {
161                 if let Err(err) = snapshot.parse_attribute(InnerAttrPolicy::Permitted) {
162                     err.cancel();
163                     return Some(replacement_span);
164                 }
165             } else {
166                 break;
167             }
168         }
169         match snapshot.parse_item_common(
170             AttrWrapper::empty(),
171             true,
172             false,
173             FnParseMode { req_name: |_| true, req_body: true },
174             ForceCollect::No,
175         ) {
176             Ok(Some(item)) => {
177                 // FIXME(#100717)
178                 err.set_arg("item", item.kind.descr());
179                 err.span_label(item.span, fluent::parse_label_does_not_annotate_this);
180                 err.span_suggestion_verbose(
181                     replacement_span,
182                     fluent::parse_sugg_change_inner_to_outer,
183                     match attr_type {
184                         OuterAttributeType::Attribute => "",
185                         OuterAttributeType::DocBlockComment => "*",
186                         OuterAttributeType::DocComment => "/",
187                     },
188                     rustc_errors::Applicability::MachineApplicable,
189                 );
190                 return None;
191             }
192             Err(item_err) => {
193                 item_err.cancel();
194             }
195             Ok(None) => {}
196         }
197         Some(replacement_span)
198     }
199 
error_on_forbidden_inner_attr(&self, attr_sp: Span, policy: InnerAttrPolicy)200     pub(super) fn error_on_forbidden_inner_attr(&self, attr_sp: Span, policy: InnerAttrPolicy) {
201         if let InnerAttrPolicy::Forbidden(reason) = policy {
202             let mut diag = match reason.as_ref().copied() {
203                 Some(InnerAttrForbiddenReason::AfterOuterDocComment { prev_doc_comment_span }) => {
204                     let mut diag = self.struct_span_err(
205                         attr_sp,
206                         fluent::parse_inner_attr_not_permitted_after_outer_doc_comment,
207                     );
208                     diag.span_label(attr_sp, fluent::parse_label_attr)
209                         .span_label(prev_doc_comment_span, fluent::parse_label_prev_doc_comment);
210                     diag
211                 }
212                 Some(InnerAttrForbiddenReason::AfterOuterAttribute { prev_outer_attr_sp }) => {
213                     let mut diag = self.struct_span_err(
214                         attr_sp,
215                         fluent::parse_inner_attr_not_permitted_after_outer_attr,
216                     );
217                     diag.span_label(attr_sp, fluent::parse_label_attr)
218                         .span_label(prev_outer_attr_sp, fluent::parse_label_prev_attr);
219                     diag
220                 }
221                 Some(InnerAttrForbiddenReason::InCodeBlock) | None => {
222                     self.struct_span_err(attr_sp, fluent::parse_inner_attr_not_permitted)
223                 }
224             };
225 
226             diag.note(fluent::parse_inner_attr_explanation);
227             if self
228                 .annotate_following_item_if_applicable(
229                     &mut diag,
230                     attr_sp,
231                     OuterAttributeType::Attribute,
232                 )
233                 .is_some()
234             {
235                 diag.note(fluent::parse_outer_attr_explanation);
236             };
237             diag.emit();
238         }
239     }
240 
241     /// Parses an inner part of an attribute (the path and following tokens).
242     /// The tokens must be either a delimited token stream, or empty token stream,
243     /// or the "legacy" key-value form.
244     ///     PATH `(` TOKEN_STREAM `)`
245     ///     PATH `[` TOKEN_STREAM `]`
246     ///     PATH `{` TOKEN_STREAM `}`
247     ///     PATH
248     ///     PATH `=` UNSUFFIXED_LIT
249     /// The delimiters or `=` are still put into the resulting token stream.
parse_attr_item(&mut self, capture_tokens: bool) -> PResult<'a, ast::AttrItem>250     pub fn parse_attr_item(&mut self, capture_tokens: bool) -> PResult<'a, ast::AttrItem> {
251         let item = match &self.token.kind {
252             token::Interpolated(nt) => match &**nt {
253                 Nonterminal::NtMeta(item) => Some(item.clone().into_inner()),
254                 _ => None,
255             },
256             _ => None,
257         };
258         Ok(if let Some(item) = item {
259             self.bump();
260             item
261         } else {
262             let do_parse = |this: &mut Self| {
263                 let path = this.parse_path(PathStyle::Mod)?;
264                 let args = this.parse_attr_args()?;
265                 Ok(ast::AttrItem { path, args, tokens: None })
266             };
267             // Attr items don't have attributes
268             if capture_tokens { self.collect_tokens_no_attrs(do_parse) } else { do_parse(self) }?
269         })
270     }
271 
272     /// Parses attributes that appear after the opening of an item. These should
273     /// be preceded by an exclamation mark, but we accept and warn about one
274     /// terminated by a semicolon.
275     ///
276     /// Matches `inner_attrs*`.
parse_inner_attributes(&mut self) -> PResult<'a, ast::AttrVec>277     pub(crate) fn parse_inner_attributes(&mut self) -> PResult<'a, ast::AttrVec> {
278         let mut attrs = ast::AttrVec::new();
279         loop {
280             let start_pos: u32 = self.token_cursor.num_next_calls.try_into().unwrap();
281             // Only try to parse if it is an inner attribute (has `!`).
282             let attr = if self.check(&token::Pound) && self.look_ahead(1, |t| t == &token::Not) {
283                 Some(self.parse_attribute(InnerAttrPolicy::Permitted)?)
284             } else if let token::DocComment(comment_kind, attr_style, data) = self.token.kind {
285                 if attr_style == ast::AttrStyle::Inner {
286                     self.bump();
287                     Some(attr::mk_doc_comment(
288                         &self.sess.attr_id_generator,
289                         comment_kind,
290                         attr_style,
291                         data,
292                         self.prev_token.span,
293                     ))
294                 } else {
295                     None
296                 }
297             } else {
298                 None
299             };
300             if let Some(attr) = attr {
301                 let end_pos: u32 = self.token_cursor.num_next_calls.try_into().unwrap();
302                 // If we are currently capturing tokens, mark the location of this inner attribute.
303                 // If capturing ends up creating a `LazyAttrTokenStream`, we will include
304                 // this replace range with it, removing the inner attribute from the final
305                 // `AttrTokenStream`. Inner attributes are stored in the parsed AST note.
306                 // During macro expansion, they are selectively inserted back into the
307                 // token stream (the first inner attribute is removed each time we invoke the
308                 // corresponding macro).
309                 let range = start_pos..end_pos;
310                 if let Capturing::Yes = self.capture_state.capturing {
311                     self.capture_state.inner_attr_ranges.insert(attr.id, (range, vec![]));
312                 }
313                 attrs.push(attr);
314             } else {
315                 break;
316             }
317         }
318         Ok(attrs)
319     }
320 
321     // Note: must be unsuffixed.
parse_unsuffixed_meta_item_lit(&mut self) -> PResult<'a, ast::MetaItemLit>322     pub(crate) fn parse_unsuffixed_meta_item_lit(&mut self) -> PResult<'a, ast::MetaItemLit> {
323         let lit = self.parse_meta_item_lit()?;
324         debug!("checking if {:?} is unsuffixed", lit);
325 
326         if !lit.kind.is_unsuffixed() {
327             self.sess.emit_err(SuffixedLiteralInAttribute { span: lit.span });
328         }
329 
330         Ok(lit)
331     }
332 
333     /// Parses `cfg_attr(pred, attr_item_list)` where `attr_item_list` is comma-delimited.
parse_cfg_attr(&mut self) -> PResult<'a, (ast::MetaItem, Vec<(ast::AttrItem, Span)>)>334     pub fn parse_cfg_attr(&mut self) -> PResult<'a, (ast::MetaItem, Vec<(ast::AttrItem, Span)>)> {
335         let cfg_predicate = self.parse_meta_item()?;
336         self.expect(&token::Comma)?;
337 
338         // Presumably, the majority of the time there will only be one attr.
339         let mut expanded_attrs = Vec::with_capacity(1);
340         while self.token.kind != token::Eof {
341             let lo = self.token.span;
342             let item = self.parse_attr_item(true)?;
343             expanded_attrs.push((item, lo.to(self.prev_token.span)));
344             if !self.eat(&token::Comma) {
345                 break;
346             }
347         }
348 
349         Ok((cfg_predicate, expanded_attrs))
350     }
351 
352     /// Matches `COMMASEP(meta_item_inner)`.
parse_meta_seq_top(&mut self) -> PResult<'a, ThinVec<ast::NestedMetaItem>>353     pub(crate) fn parse_meta_seq_top(&mut self) -> PResult<'a, ThinVec<ast::NestedMetaItem>> {
354         // Presumably, the majority of the time there will only be one attr.
355         let mut nmis = ThinVec::with_capacity(1);
356         while self.token.kind != token::Eof {
357             nmis.push(self.parse_meta_item_inner()?);
358             if !self.eat(&token::Comma) {
359                 break;
360             }
361         }
362         Ok(nmis)
363     }
364 
365     /// Matches the following grammar (per RFC 1559).
366     /// ```ebnf
367     /// meta_item : PATH ( '=' UNSUFFIXED_LIT | '(' meta_item_inner? ')' )? ;
368     /// meta_item_inner : (meta_item | UNSUFFIXED_LIT) (',' meta_item_inner)? ;
369     /// ```
parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem>370     pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> {
371         let nt_meta = match &self.token.kind {
372             token::Interpolated(nt) => match &**nt {
373                 token::NtMeta(e) => Some(e.clone()),
374                 _ => None,
375             },
376             _ => None,
377         };
378 
379         if let Some(item) = nt_meta {
380             return match item.meta(item.path.span) {
381                 Some(meta) => {
382                     self.bump();
383                     Ok(meta)
384                 }
385                 None => self.unexpected(),
386             };
387         }
388 
389         let lo = self.token.span;
390         let path = self.parse_path(PathStyle::Mod)?;
391         let kind = self.parse_meta_item_kind()?;
392         let span = lo.to(self.prev_token.span);
393         Ok(ast::MetaItem { path, kind, span })
394     }
395 
parse_meta_item_kind(&mut self) -> PResult<'a, ast::MetaItemKind>396     pub(crate) fn parse_meta_item_kind(&mut self) -> PResult<'a, ast::MetaItemKind> {
397         Ok(if self.eat(&token::Eq) {
398             ast::MetaItemKind::NameValue(self.parse_unsuffixed_meta_item_lit()?)
399         } else if self.check(&token::OpenDelim(Delimiter::Parenthesis)) {
400             // Matches `meta_seq = ( COMMASEP(meta_item_inner) )`.
401             let (list, _) = self.parse_paren_comma_seq(|p| p.parse_meta_item_inner())?;
402             ast::MetaItemKind::List(list)
403         } else {
404             ast::MetaItemKind::Word
405         })
406     }
407 
408     /// Matches `meta_item_inner : (meta_item | UNSUFFIXED_LIT) ;`.
parse_meta_item_inner(&mut self) -> PResult<'a, ast::NestedMetaItem>409     fn parse_meta_item_inner(&mut self) -> PResult<'a, ast::NestedMetaItem> {
410         match self.parse_unsuffixed_meta_item_lit() {
411             Ok(lit) => return Ok(ast::NestedMetaItem::Lit(lit)),
412             Err(err) => err.cancel(),
413         }
414 
415         match self.parse_meta_item() {
416             Ok(mi) => return Ok(ast::NestedMetaItem::MetaItem(mi)),
417             Err(err) => err.cancel(),
418         }
419 
420         Err(InvalidMetaItem { span: self.token.span, token: self.token.clone() }
421             .into_diagnostic(&self.sess.span_diagnostic))
422     }
423 }
424 
425 /// The attributes are complete if all attributes are either a doc comment or a builtin attribute other than `cfg_attr`
is_complete(attrs: &[ast::Attribute]) -> bool426 pub fn is_complete(attrs: &[ast::Attribute]) -> bool {
427     attrs.iter().all(|attr| {
428         attr.is_doc_comment()
429             || attr.ident().is_some_and(|ident| {
430                 ident.name != sym::cfg_attr && rustc_feature::is_builtin_attr_name(ident.name)
431             })
432     })
433 }
434