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