1 use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle};
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;
10 // Public for rustfmt usage
12 pub enum InnerAttrPolicy<'a> {
14 Forbidden { reason: &'a str, saw_doc_comment: bool, prev_outer_attr_sp: Option<Span> },
17 const DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG: &str = "an inner attribute is not \
18 permitted in this context";
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,
26 enum OuterAttributeType {
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;
39 let attr = if self.check(&token::Pound) {
40 let prev_outer_attr_sp = outer_attrs.last().map(|attr| attr.span);
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"
47 DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG
49 let inner_parse_policy = InnerAttrPolicy::Forbidden {
50 reason: inner_error_reason,
51 saw_doc_comment: just_parsed_doc_comment,
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(
61 "expected outer doc comment",
64 if let Some(replacement_span) = self.annotate_following_item_if_applicable(
68 token::CommentKind::Line => OuterAttributeType::DocComment,
69 token::CommentKind::Block => OuterAttributeType::DocBlockComment,
73 "inner doc comments like this (starting with `//!` or `/*!`) can \
74 only appear before items",
76 err.span_suggestion_verbose(
78 "you might have meant to write a regular comment",
80 rustc_errors::Applicability::MachineApplicable,
86 just_parsed_doc_comment = true;
87 // Always make an outer attribute - this allows us to recover from a misplaced
89 Some(attr::mk_doc_comment(
91 ast::AttrStyle::Outer,
99 if let Some(attr) = attr {
100 if attr.style == ast::AttrStyle::Outer {
101 outer_attrs.push(attr);
107 Ok(AttrWrapper::new(outer_attrs, start_pos))
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(
115 inner_parse_policy: InnerAttrPolicy<'_>,
116 ) -> PResult<'a, ast::Attribute> {
118 "parse_attribute: inner_parse_policy={:?} self.token={:?}",
119 inner_parse_policy, self.token
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
128 ast::AttrStyle::Outer
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);
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);
141 Ok(attr::mk_attr_from_item(item, None, style, attr_sp))
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))
150 fn annotate_following_item_if_applicable(
152 err: &mut Diagnostic,
154 attr_type: OuterAttributeType,
156 let mut snapshot = self.create_snapshot_for_diagnostic();
158 + BytePos(match attr_type {
159 OuterAttributeType::Attribute => 1,
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 {
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) {
172 return Some(replacement_span);
178 match snapshot.parse_item_common(
179 AttrWrapper::empty(),
182 FnParseMode { req_name: |_| true, req_body: true },
186 let attr_name = match attr_type {
187 OuterAttributeType::Attribute => "attribute",
192 &format!("the inner {} doesn't annotate this {}", attr_name, item.kind.descr()),
194 err.span_suggestion_verbose(
197 "to annotate the {}, change the {} from inner to outer style",
202 OuterAttributeType::Attribute => "",
203 OuterAttributeType::DocBlockComment => "*",
204 OuterAttributeType::DocComment => "/",
206 rustc_errors::Applicability::MachineApplicable,
215 Some(replacement_span)
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" };
223 let mut diag = self.struct_span_err(attr_sp, reason);
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);
231 "inner attributes, like `#![no_std]`, annotate the item enclosing them, and \
232 are usually found at the beginning of source files",
235 .annotate_following_item_if_applicable(
238 OuterAttributeType::Attribute,
242 diag.note("outer attributes, like `#[test]`, annotate the item following them");
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 `}`
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()),
265 Ok(if let Some(item) = item {
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 })
274 // Attr items don't have attributes
275 if capture_tokens { self.collect_tokens_no_attrs(do_parse) } else { do_parse(self) }?
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.
283 /// Matches `inner_attrs*`.
284 pub(crate) fn parse_inner_attributes(&mut self) -> PResult<'a, ast::AttrVec> {
285 let mut attrs = ast::AttrVec::new();
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 {
294 Some(attr::mk_doc_comment(comment_kind, attr_style, data, self.prev_token.span))
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![]));
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);
326 if !lit.kind.is_unsuffixed() {
327 self.struct_span_err(lit.span, "suffixed literals are not allowed in attributes")
329 "instead of using a suffixed literal (`1u8`, `1.0f32`, etc.), \
330 use an unsuffixed version (`1`, `1.0`, etc.)",
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)?;
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) {
354 Ok((cfg_predicate, expanded_attrs))
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) {
370 /// Matches the following grammar (per RFC 1559).
372 /// meta_item : PATH ( '=' UNSUFFIXED_LIT | '(' meta_item_inner? ')' )? ;
373 /// meta_item_inner : (meta_item | UNSUFFIXED_LIT) (',' meta_item_inner)? ;
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()),
384 if let Some(item) = nt_meta {
385 return match item.meta(item.path.span) {
390 None => self.unexpected(),
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 })
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)
409 ast::MetaItemKind::Word
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(),
420 match self.parse_meta_item() {
421 Ok(mi) => return Ok(ast::NestedMetaItem::MetaItem(mi)),
422 Err(err) => err.cancel(),
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))
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() {
438 attr.ident().map_or(true, |ident| {
439 ident.name == sym::cfg_attr || !rustc_feature::is_builtin_attr_name(ident.name)