]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/parse/attr.rs
Auto merge of #63779 - Centril:rollup-sx96dli, r=Centril
[rust.git] / src / libsyntax / parse / attr.rs
1 use crate::attr;
2 use crate::ast;
3 use crate::parse::{SeqSep, PResult};
4 use crate::parse::token::{self, Nonterminal, DelimToken};
5 use crate::parse::parser::{Parser, TokenType, PathStyle};
6 use crate::tokenstream::{TokenStream, TokenTree};
7 use crate::source_map::Span;
8
9 use log::debug;
10 use smallvec::smallvec;
11
12 #[derive(Debug)]
13 enum InnerAttributeParsePolicy<'a> {
14     Permitted,
15     NotPermitted { reason: &'a str, saw_doc_comment: bool, prev_attr_sp: Option<Span> },
16 }
17
18 const DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG: &str = "an inner attribute is not \
19                                                      permitted in this context";
20
21 impl<'a> Parser<'a> {
22     crate fn parse_arg_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
23         let attrs = self.parse_outer_attributes()?;
24         self.sess.gated_spans.param_attrs.borrow_mut()
25             .extend(attrs.iter().map(|a| a.span));
26         Ok(attrs)
27     }
28
29     /// Parse attributes that appear before an item
30     crate fn parse_outer_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
31         let mut attrs: Vec<ast::Attribute> = Vec::new();
32         let mut just_parsed_doc_comment = false;
33         loop {
34             debug!("parse_outer_attributes: self.token={:?}", self.token);
35             match self.token.kind {
36                 token::Pound => {
37                     let inner_error_reason = if just_parsed_doc_comment {
38                         "an inner attribute is not permitted following an outer doc comment"
39                     } else if !attrs.is_empty() {
40                         "an inner attribute is not permitted following an outer attribute"
41                     } else {
42                         DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG
43                     };
44                     let inner_parse_policy =
45                         InnerAttributeParsePolicy::NotPermitted {
46                             reason: inner_error_reason,
47                             saw_doc_comment: just_parsed_doc_comment,
48                             prev_attr_sp: attrs.last().and_then(|a| Some(a.span))
49                         };
50                     let attr = self.parse_attribute_with_inner_parse_policy(inner_parse_policy)?;
51                     attrs.push(attr);
52                     just_parsed_doc_comment = false;
53                 }
54                 token::DocComment(s) => {
55                     let attr = attr::mk_sugared_doc_attr(s, self.token.span);
56                     if attr.style != ast::AttrStyle::Outer {
57                         let mut err = self.fatal("expected outer doc comment");
58                         err.note("inner doc comments like this (starting with \
59                                   `//!` or `/*!`) can only appear before items");
60                         return Err(err);
61                     }
62                     attrs.push(attr);
63                     self.bump();
64                     just_parsed_doc_comment = true;
65                 }
66                 _ => break,
67             }
68         }
69         Ok(attrs)
70     }
71
72     /// Matches `attribute = # ! [ meta_item ]`
73     ///
74     /// If permit_inner is true, then a leading `!` indicates an inner
75     /// attribute
76     pub fn parse_attribute(&mut self, permit_inner: bool) -> PResult<'a, ast::Attribute> {
77         debug!("parse_attribute: permit_inner={:?} self.token={:?}",
78                permit_inner,
79                self.token);
80         let inner_parse_policy = if permit_inner {
81             InnerAttributeParsePolicy::Permitted
82         } else {
83             InnerAttributeParsePolicy::NotPermitted {
84                 reason: DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG,
85                 saw_doc_comment: false,
86                 prev_attr_sp: None
87             }
88         };
89         self.parse_attribute_with_inner_parse_policy(inner_parse_policy)
90     }
91
92     /// The same as `parse_attribute`, except it takes in an `InnerAttributeParsePolicy`
93     /// that prescribes how to handle inner attributes.
94     fn parse_attribute_with_inner_parse_policy(&mut self,
95                                                inner_parse_policy: InnerAttributeParsePolicy<'_>)
96                                                -> PResult<'a, ast::Attribute> {
97         debug!("parse_attribute_with_inner_parse_policy: inner_parse_policy={:?} self.token={:?}",
98                inner_parse_policy,
99                self.token);
100         let (span, path, tokens, style) = match self.token.kind {
101             token::Pound => {
102                 let lo = self.token.span;
103                 self.bump();
104
105                 if let InnerAttributeParsePolicy::Permitted = inner_parse_policy {
106                     self.expected_tokens.push(TokenType::Token(token::Not));
107                 }
108
109                 let style = if self.token == token::Not {
110                     self.bump();
111                     ast::AttrStyle::Inner
112                 } else {
113                     ast::AttrStyle::Outer
114                 };
115
116                 self.expect(&token::OpenDelim(token::Bracket))?;
117                 let (path, tokens) = self.parse_meta_item_unrestricted()?;
118                 self.expect(&token::CloseDelim(token::Bracket))?;
119                 let hi = self.prev_span;
120
121                 let attr_sp = lo.to(hi);
122
123                 // Emit error if inner attribute is encountered and not permitted
124                 if style == ast::AttrStyle::Inner {
125                     if let InnerAttributeParsePolicy::NotPermitted { reason,
126                         saw_doc_comment, prev_attr_sp } = inner_parse_policy {
127                         let prev_attr_note = if saw_doc_comment {
128                             "previous doc comment"
129                         } else {
130                             "previous outer attribute"
131                         };
132
133                         let mut diagnostic = self
134                             .diagnostic()
135                             .struct_span_err(attr_sp, reason);
136
137                         if let Some(prev_attr_sp) = prev_attr_sp {
138                             diagnostic
139                                 .span_label(attr_sp, "not permitted following an outer attibute")
140                                 .span_label(prev_attr_sp, prev_attr_note);
141                         }
142
143                         diagnostic
144                             .note("inner attributes, like `#![no_std]`, annotate the item \
145                                    enclosing them, and are usually found at the beginning of \
146                                    source files. Outer attributes, like `#[test]`, annotate the \
147                                    item following them.")
148                             .emit()
149                     }
150                 }
151
152                 (attr_sp, path, tokens, style)
153             }
154             _ => {
155                 let token_str = self.this_token_to_string();
156                 return Err(self.fatal(&format!("expected `#`, found `{}`", token_str)));
157             }
158         };
159
160         Ok(ast::Attribute {
161             id: attr::mk_attr_id(),
162             style,
163             path,
164             tokens,
165             is_sugared_doc: false,
166             span,
167         })
168     }
169
170     /// Parse an inner part of attribute - path and following tokens.
171     /// The tokens must be either a delimited token stream, or empty token stream,
172     /// or the "legacy" key-value form.
173     /// PATH `(` TOKEN_STREAM `)`
174     /// PATH `[` TOKEN_STREAM `]`
175     /// PATH `{` TOKEN_STREAM `}`
176     /// PATH
177     /// PATH `=` TOKEN_TREE
178     /// The delimiters or `=` are still put into the resulting token stream.
179     crate fn parse_meta_item_unrestricted(&mut self) -> PResult<'a, (ast::Path, TokenStream)> {
180         let meta = match self.token.kind {
181             token::Interpolated(ref nt) => match **nt {
182                 Nonterminal::NtMeta(ref meta) => Some(meta.clone()),
183                 _ => None,
184             },
185             _ => None,
186         };
187         Ok(if let Some(meta) = meta {
188             self.bump();
189             (meta.path, meta.node.tokens(meta.span))
190         } else {
191             let path = self.parse_path(PathStyle::Mod)?;
192             let tokens = if self.check(&token::OpenDelim(DelimToken::Paren)) ||
193                self.check(&token::OpenDelim(DelimToken::Bracket)) ||
194                self.check(&token::OpenDelim(DelimToken::Brace)) {
195                    self.parse_token_tree().into()
196             } else if self.eat(&token::Eq) {
197                 let eq = TokenTree::token(token::Eq, self.prev_span);
198                 let mut is_interpolated_expr = false;
199                 if let token::Interpolated(nt) = &self.token.kind {
200                     if let token::NtExpr(..) = **nt {
201                         is_interpolated_expr = true;
202                     }
203                 }
204                 let tokens = if is_interpolated_expr {
205                     // We need to accept arbitrary interpolated expressions to continue
206                     // supporting things like `doc = $expr` that work on stable.
207                     // Non-literal interpolated expressions are rejected after expansion.
208                     self.parse_token_tree().into()
209                 } else {
210                     self.parse_unsuffixed_lit()?.tokens()
211                 };
212                 TokenStream::from_streams(smallvec![eq.into(), tokens])
213             } else {
214                 TokenStream::empty()
215             };
216             (path, tokens)
217         })
218     }
219
220     /// Parse attributes that appear after the opening of an item. These should
221     /// be preceded by an exclamation mark, but we accept and warn about one
222     /// terminated by a semicolon.
223
224     /// matches inner_attrs*
225     crate fn parse_inner_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
226         let mut attrs: Vec<ast::Attribute> = vec![];
227         loop {
228             match self.token.kind {
229                 token::Pound => {
230                     // Don't even try to parse if it's not an inner attribute.
231                     if !self.look_ahead(1, |t| t == &token::Not) {
232                         break;
233                     }
234
235                     let attr = self.parse_attribute(true)?;
236                     assert_eq!(attr.style, ast::AttrStyle::Inner);
237                     attrs.push(attr);
238                 }
239                 token::DocComment(s) => {
240                     // we need to get the position of this token before we bump.
241                     let attr = attr::mk_sugared_doc_attr(s, self.token.span);
242                     if attr.style == ast::AttrStyle::Inner {
243                         attrs.push(attr);
244                         self.bump();
245                     } else {
246                         break;
247                     }
248                 }
249                 _ => break,
250             }
251         }
252         Ok(attrs)
253     }
254
255     fn parse_unsuffixed_lit(&mut self) -> PResult<'a, ast::Lit> {
256         let lit = self.parse_lit()?;
257         debug!("checking if {:?} is unusuffixed", lit);
258
259         if !lit.node.is_unsuffixed() {
260             let msg = "suffixed literals are not allowed in attributes";
261             self.diagnostic().struct_span_err(lit.span, msg)
262                              .help("instead of using a suffixed literal \
263                                     (1u8, 1.0f32, etc.), use an unsuffixed version \
264                                     (1, 1.0, etc.).")
265                              .emit()
266         }
267
268         Ok(lit)
269     }
270
271     /// Per RFC#1559, matches the following grammar:
272     ///
273     /// meta_item : IDENT ( '=' UNSUFFIXED_LIT | '(' meta_item_inner? ')' )? ;
274     /// meta_item_inner : (meta_item | UNSUFFIXED_LIT) (',' meta_item_inner)? ;
275     pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> {
276         let nt_meta = match self.token.kind {
277             token::Interpolated(ref nt) => match **nt {
278                 token::NtMeta(ref e) => Some(e.clone()),
279                 _ => None,
280             },
281             _ => None,
282         };
283
284         if let Some(meta) = nt_meta {
285             self.bump();
286             return Ok(meta);
287         }
288
289         let lo = self.token.span;
290         let path = self.parse_path(PathStyle::Mod)?;
291         let node = self.parse_meta_item_kind()?;
292         let span = lo.to(self.prev_span);
293         Ok(ast::MetaItem { path, node, span })
294     }
295
296     crate fn parse_meta_item_kind(&mut self) -> PResult<'a, ast::MetaItemKind> {
297         Ok(if self.eat(&token::Eq) {
298             ast::MetaItemKind::NameValue(self.parse_unsuffixed_lit()?)
299         } else if self.eat(&token::OpenDelim(token::Paren)) {
300             ast::MetaItemKind::List(self.parse_meta_seq()?)
301         } else {
302             ast::MetaItemKind::Word
303         })
304     }
305
306     /// matches meta_item_inner : (meta_item | UNSUFFIXED_LIT) ;
307     fn parse_meta_item_inner(&mut self) -> PResult<'a, ast::NestedMetaItem> {
308         match self.parse_unsuffixed_lit() {
309             Ok(lit) => {
310                 return Ok(ast::NestedMetaItem::Literal(lit))
311             }
312             Err(ref mut err) => self.diagnostic().cancel(err)
313         }
314
315         match self.parse_meta_item() {
316             Ok(mi) => {
317                 return Ok(ast::NestedMetaItem::MetaItem(mi))
318             }
319             Err(ref mut err) => self.diagnostic().cancel(err)
320         }
321
322         let found = self.this_token_to_string();
323         let msg = format!("expected unsuffixed literal or identifier, found `{}`", found);
324         Err(self.diagnostic().struct_span_err(self.token.span, &msg))
325     }
326
327     /// matches meta_seq = ( COMMASEP(meta_item_inner) )
328     fn parse_meta_seq(&mut self) -> PResult<'a, Vec<ast::NestedMetaItem>> {
329         self.parse_seq_to_end(&token::CloseDelim(token::Paren),
330                               SeqSep::trailing_allowed(token::Comma),
331                               |p: &mut Parser<'a>| p.parse_meta_item_inner())
332     }
333 }