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