1use rustc_ast as ast;
2use rustc_ast::token::{self, MetaVarKind};
3use rustc_ast::tokenstream::ParserRange;
4use rustc_ast::{AttrItemKind, Attribute, attr};
5use rustc_errors::codes::*;
6use rustc_errors::{Diag, PResult, msg};
7use rustc_span::{BytePos, Span};
8use thin_vec::ThinVec;
9use tracing::debug;
10
11use super::{
12 AllowConstBlockItems, AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle,
13 Trailing, UsePreAttrPos,
14};
15use crate::parser::FnContext;
16use crate::{errors, exp};
17
18#[derive(#[automatically_derived]
impl ::core::fmt::Debug for InnerAttrPolicy {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
InnerAttrPolicy::Permitted =>
::core::fmt::Formatter::write_str(f, "Permitted"),
InnerAttrPolicy::Forbidden(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Forbidden", &__self_0),
}
}
}Debug)]
20pub enum InnerAttrPolicy {
21 Permitted,
22 Forbidden(Option<InnerAttrForbiddenReason>),
23}
24
25#[derive(#[automatically_derived]
impl ::core::clone::Clone for InnerAttrForbiddenReason {
#[inline]
fn clone(&self) -> InnerAttrForbiddenReason {
let _: ::core::clone::AssertParamIsClone<Span>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for InnerAttrForbiddenReason { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for InnerAttrForbiddenReason {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
InnerAttrForbiddenReason::InCodeBlock =>
::core::fmt::Formatter::write_str(f, "InCodeBlock"),
InnerAttrForbiddenReason::AfterOuterDocComment {
prev_doc_comment_span: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"AfterOuterDocComment", "prev_doc_comment_span", &__self_0),
InnerAttrForbiddenReason::AfterOuterAttribute {
prev_outer_attr_sp: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"AfterOuterAttribute", "prev_outer_attr_sp", &__self_0),
}
}
}Debug)]
26pub enum InnerAttrForbiddenReason {
27 InCodeBlock,
28 AfterOuterDocComment { prev_doc_comment_span: Span },
29 AfterOuterAttribute { prev_outer_attr_sp: Span },
30}
31
32enum OuterAttributeType {
33 DocComment,
34 DocBlockComment,
35 Attribute,
36}
37
38#[derive(#[automatically_derived]
impl ::core::clone::Clone for AllowLeadingUnsafe {
#[inline]
fn clone(&self) -> AllowLeadingUnsafe { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for AllowLeadingUnsafe { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for AllowLeadingUnsafe {
#[inline]
fn eq(&self, other: &AllowLeadingUnsafe) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for AllowLeadingUnsafe {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) {}
}Eq)]
39pub enum AllowLeadingUnsafe {
40 Yes,
41 No,
42}
43
44impl<'a> Parser<'a> {
45 pub(super) fn parse_outer_attributes(&mut self) -> PResult<'a, AttrWrapper> {
47 let mut outer_attrs = ast::AttrVec::new();
48 let mut just_parsed_doc_comment = false;
49 let start_pos = self.num_bump_calls;
50 loop {
51 let attr = if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Pound,
token_type: crate::parser::token_type::TokenType::Pound,
}exp!(Pound)) {
52 let prev_outer_attr_sp = outer_attrs.last().map(|attr: &Attribute| attr.span);
53
54 let inner_error_reason = if just_parsed_doc_comment {
55 Some(InnerAttrForbiddenReason::AfterOuterDocComment {
56 prev_doc_comment_span: prev_outer_attr_sp.unwrap(),
57 })
58 } else {
59 prev_outer_attr_sp.map(|prev_outer_attr_sp| {
60 InnerAttrForbiddenReason::AfterOuterAttribute { prev_outer_attr_sp }
61 })
62 };
63 let inner_parse_policy = InnerAttrPolicy::Forbidden(inner_error_reason);
64 just_parsed_doc_comment = false;
65 Some(self.parse_attribute(inner_parse_policy)?)
66 } else if let token::DocComment(comment_kind, attr_style, data) = self.token.kind {
67 if attr_style != ast::AttrStyle::Outer {
68 let span = self.token.span;
69 let mut err =
70 self.dcx().struct_span_err(span, rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("expected outer doc comment"))msg!("expected outer doc comment"));
71 err.code(E0753);
72 if let Some(replacement_span) = self.annotate_following_item_if_applicable(
73 &mut err,
74 span,
75 match comment_kind {
76 token::CommentKind::Line => OuterAttributeType::DocComment,
77 token::CommentKind::Block => OuterAttributeType::DocBlockComment,
78 },
79 true,
80 ) {
81 err.note(rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("inner doc comments like this (starting with `//!` or `/*!`) can only appear before items"))msg!(
82 "inner doc comments like this (starting with `//!` or `/*!`) can only appear before items"
83 ));
84 err.span_suggestion_verbose(
85 replacement_span,
86 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("you might have meant to write a regular comment"))msg!("you might have meant to write a regular comment"),
87 "",
88 rustc_errors::Applicability::MachineApplicable,
89 );
90 }
91 err.emit();
92 }
93 self.bump();
94 just_parsed_doc_comment = true;
95 Some(attr::mk_doc_comment(
98 &self.psess.attr_id_generator,
99 comment_kind,
100 ast::AttrStyle::Outer,
101 data,
102 self.prev_token.span,
103 ))
104 } else {
105 None
106 };
107
108 if let Some(attr) = attr {
109 if attr.style == ast::AttrStyle::Outer {
110 outer_attrs.push(attr);
111 }
112 } else {
113 break;
114 }
115 }
116 Ok(AttrWrapper::new(outer_attrs, start_pos))
117 }
118
119 pub fn parse_attribute(
123 &mut self,
124 inner_parse_policy: InnerAttrPolicy,
125 ) -> PResult<'a, ast::Attribute> {
126 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_parse/src/parser/attr.rs:126",
"rustc_parse::parser::attr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/parser/attr.rs"),
::tracing_core::__macro_support::Option::Some(126u32),
::tracing_core::__macro_support::Option::Some("rustc_parse::parser::attr"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("parse_attribute: inner_parse_policy={0:?} self.token={1:?}",
inner_parse_policy, self.token) as &dyn Value))])
});
} else { ; }
};debug!(
127 "parse_attribute: inner_parse_policy={:?} self.token={:?}",
128 inner_parse_policy, self.token
129 );
130 let lo = self.token.span;
131 self.collect_tokens_no_attrs(|this| {
133 let pound_hi = this.token.span.hi();
134 if !this.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Pound,
token_type: crate::parser::token_type::TokenType::Pound,
}) {
{
::core::panicking::panic_fmt(format_args!("parse_attribute called in non-attribute position"));
}
};assert!(this.eat(exp!(Pound)), "parse_attribute called in non-attribute position");
135
136 let not_lo = this.token.span.lo();
137 let style =
138 if this.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Bang,
token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang)) { ast::AttrStyle::Inner } else { ast::AttrStyle::Outer };
139
140 let mut bracket_res = this.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBracket,
token_type: crate::parser::token_type::TokenType::OpenBracket,
}exp!(OpenBracket));
141 if let Err(err) = &mut bracket_res
143 && style == ast::AttrStyle::Inner
144 && pound_hi == not_lo
145 {
146 err.note(
147 "the token sequence `#!` here looks like the start of \
148 a shebang interpreter directive but it is not",
149 );
150 err.help(
151 "if you meant this to be a shebang interpreter directive, \
152 move it to the very start of the file",
153 );
154 }
155 bracket_res?;
156 let item = this.parse_attr_item(ForceCollect::No)?;
157 this.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBracket,
token_type: crate::parser::token_type::TokenType::CloseBracket,
}exp!(CloseBracket))?;
158 let attr_sp = lo.to(this.prev_token.span);
159
160 if style == ast::AttrStyle::Inner {
162 this.error_on_forbidden_inner_attr(
163 attr_sp,
164 inner_parse_policy,
165 item.is_valid_for_outer_style(),
166 );
167 }
168
169 Ok(attr::mk_attr_from_item(&self.psess.attr_id_generator, item, None, style, attr_sp))
170 })
171 }
172
173 fn annotate_following_item_if_applicable(
174 &self,
175 err: &mut Diag<'_>,
176 span: Span,
177 attr_type: OuterAttributeType,
178 suggest_to_outer: bool,
179 ) -> Option<Span> {
180 let mut snapshot = self.create_snapshot_for_diagnostic();
181 let lo = span.lo()
182 + BytePos(match attr_type {
183 OuterAttributeType::Attribute => 1,
184 _ => 2,
185 });
186 let hi = lo + BytePos(1);
187 let replacement_span = span.with_lo(lo).with_hi(hi);
188 if let OuterAttributeType::DocBlockComment | OuterAttributeType::DocComment = attr_type {
189 snapshot.bump();
190 }
191 loop {
192 if snapshot.token == token::Pound {
194 if let Err(err) = snapshot.parse_attribute(InnerAttrPolicy::Permitted) {
195 err.cancel();
196 return Some(replacement_span);
197 }
198 } else {
199 break;
200 }
201 }
202 match snapshot.parse_item_common(
203 AttrWrapper::empty(),
204 true,
205 false,
206 FnParseMode { req_name: |_, _| true, context: FnContext::Free, req_body: true },
207 ForceCollect::No,
208 AllowConstBlockItems::Yes,
209 ) {
210 Ok(Some(item)) => {
211 err.arg("item", item.kind.descr());
212 err.span_label(
213 item.span,
214 match attr_type {
215 OuterAttributeType::Attribute => {
216 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("the inner attribute doesn't annotate this {$item}"))msg!("the inner attribute doesn't annotate this {$item}")
217 }
218 OuterAttributeType::DocComment | OuterAttributeType::DocBlockComment => {
219 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("the inner doc comment doesn't annotate this {$item}"))msg!("the inner doc comment doesn't annotate this {$item}")
220 }
221 },
222 );
223 if suggest_to_outer {
224 err.span_suggestion_verbose(
225 replacement_span,
226 match attr_type {
227 OuterAttributeType::Attribute => rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("to annotate the {$item}, change the attribute from inner to outer style"))msg!("to annotate the {$item}, change the attribute from inner to outer style"),
228 OuterAttributeType::DocComment | OuterAttributeType::DocBlockComment => rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("to annotate the {$item}, change the doc comment from inner to outer style"))msg!("to annotate the {$item}, change the doc comment from inner to outer style"),
229 },
230 match attr_type {
231 OuterAttributeType::Attribute => "",
232 OuterAttributeType::DocBlockComment => "*",
233 OuterAttributeType::DocComment => "/",
234 },
235 rustc_errors::Applicability::MachineApplicable,
236 );
237 }
238 return None;
239 }
240 Err(item_err) => {
241 item_err.cancel();
242 }
243 Ok(None) => {}
244 }
245 Some(replacement_span)
246 }
247
248 pub(super) fn error_on_forbidden_inner_attr(
249 &self,
250 attr_sp: Span,
251 policy: InnerAttrPolicy,
252 suggest_to_outer: bool,
253 ) {
254 if let InnerAttrPolicy::Forbidden(reason) = policy {
255 let mut diag = match reason.as_ref().copied() {
256 Some(InnerAttrForbiddenReason::AfterOuterDocComment { prev_doc_comment_span }) => {
257 self.dcx()
258 .struct_span_err(
259 attr_sp,
260 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("an inner attribute is not permitted following an outer doc comment"))msg!(
261 "an inner attribute is not permitted following an outer doc comment"
262 ),
263 )
264 .with_span_label(
265 attr_sp,
266 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("not permitted following an outer doc comment"))msg!("not permitted following an outer doc comment"),
267 )
268 .with_span_label(prev_doc_comment_span, rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("previous doc comment"))msg!("previous doc comment"))
269 }
270 Some(InnerAttrForbiddenReason::AfterOuterAttribute { prev_outer_attr_sp }) => self
271 .dcx()
272 .struct_span_err(
273 attr_sp,
274 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("an inner attribute is not permitted following an outer attribute"))msg!("an inner attribute is not permitted following an outer attribute"),
275 )
276 .with_span_label(attr_sp, rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("not permitted following an outer attribute"))msg!("not permitted following an outer attribute"))
277 .with_span_label(prev_outer_attr_sp, rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("previous outer attribute"))msg!("previous outer attribute")),
278 Some(InnerAttrForbiddenReason::InCodeBlock) | None => self.dcx().struct_span_err(
279 attr_sp,
280 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("an inner attribute is not permitted in this context"))msg!("an inner attribute is not permitted in this context"),
281 ),
282 };
283
284 diag.note(rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files"))msg!("inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files"));
285 if self
286 .annotate_following_item_if_applicable(
287 &mut diag,
288 attr_sp,
289 OuterAttributeType::Attribute,
290 suggest_to_outer,
291 )
292 .is_some()
293 {
294 diag.note(rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("outer attributes, like `#[test]`, annotate the item following them"))msg!(
295 "outer attributes, like `#[test]`, annotate the item following them"
296 ));
297 };
298 diag.emit();
299 }
300 }
301
302 pub fn parse_attr_item(&mut self, force_collect: ForceCollect) -> PResult<'a, ast::AttrItem> {
312 if let Some(item) = self.eat_metavar_seq_with_matcher(
313 |mv_kind| #[allow(non_exhaustive_omitted_patterns)] match mv_kind {
MetaVarKind::Meta { .. } => true,
_ => false,
}matches!(mv_kind, MetaVarKind::Meta { .. }),
314 |this| this.parse_attr_item(force_collect),
315 ) {
316 return Ok(item);
317 }
318
319 self.collect_tokens(None, AttrWrapper::empty(), force_collect, |this, _empty_attrs| {
321 let is_unsafe = this.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe));
322 let unsafety = if is_unsafe {
323 let unsafe_span = this.prev_token.span;
324 this.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenParen,
token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen))?;
325 ast::Safety::Unsafe(unsafe_span)
326 } else {
327 ast::Safety::Default
328 };
329
330 let path = this.parse_path(PathStyle::Mod)?;
331 let args = this.parse_attr_args()?;
332 if is_unsafe {
333 this.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseParen,
token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen))?;
334 }
335 Ok((
336 ast::AttrItem { unsafety, path, args: AttrItemKind::Unparsed(args), tokens: None },
337 Trailing::No,
338 UsePreAttrPos::No,
339 ))
340 })
341 }
342
343 pub fn parse_inner_attributes(&mut self) -> PResult<'a, ast::AttrVec> {
349 let mut attrs = ast::AttrVec::new();
350 loop {
351 let start_pos = self.num_bump_calls;
352 let attr = if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Pound,
token_type: crate::parser::token_type::TokenType::Pound,
}exp!(Pound)) && self.look_ahead(1, |t| t == &token::Bang) {
354 Some(self.parse_attribute(InnerAttrPolicy::Permitted)?)
355 } else if let token::DocComment(comment_kind, attr_style, data) = self.token.kind {
356 if attr_style == ast::AttrStyle::Inner {
357 self.bump();
358 Some(attr::mk_doc_comment(
359 &self.psess.attr_id_generator,
360 comment_kind,
361 attr_style,
362 data,
363 self.prev_token.span,
364 ))
365 } else {
366 None
367 }
368 } else {
369 None
370 };
371 if let Some(attr) = attr {
372 if let Capturing::Yes = self.capture_state.capturing {
376 let end_pos = self.num_bump_calls;
377 let parser_range = ParserRange(start_pos..end_pos);
378 self.capture_state.inner_attr_parser_ranges.insert(attr.id, parser_range);
379 }
380 attrs.push(attr);
381 } else {
382 break;
383 }
384 }
385 Ok(attrs)
386 }
387
388 pub(crate) fn parse_unsuffixed_meta_item_lit(&mut self) -> PResult<'a, ast::MetaItemLit> {
390 let lit = self.parse_meta_item_lit()?;
391 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_parse/src/parser/attr.rs:391",
"rustc_parse::parser::attr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/parser/attr.rs"),
::tracing_core::__macro_support::Option::Some(391u32),
::tracing_core::__macro_support::Option::Some("rustc_parse::parser::attr"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("checking if {0:?} is unsuffixed",
lit) as &dyn Value))])
});
} else { ; }
};debug!("checking if {:?} is unsuffixed", lit);
392
393 if !lit.kind.is_unsuffixed() {
394 self.dcx().emit_err(errors::SuffixedLiteralInAttribute { span: lit.span });
395 }
396
397 Ok(lit)
398 }
399
400 pub fn parse_meta_seq_top(&mut self) -> PResult<'a, ThinVec<ast::MetaItemInner>> {
402 let mut nmis = ThinVec::with_capacity(1);
404 while self.token != token::Eof {
405 nmis.push(self.parse_meta_item_inner()?);
406 if !self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma)) {
407 break;
408 }
409 }
410 Ok(nmis)
411 }
412
413 pub fn parse_meta_item(
420 &mut self,
421 unsafe_allowed: AllowLeadingUnsafe,
422 ) -> PResult<'a, ast::MetaItem> {
423 if let Some(MetaVarKind::Meta { has_meta_form }) = self.token.is_metavar_seq() {
424 return if has_meta_form {
425 let attr_item = self
426 .eat_metavar_seq(MetaVarKind::Meta { has_meta_form: true }, |this| {
427 this.parse_attr_item(ForceCollect::No)
428 })
429 .unwrap();
430 Ok(attr_item.meta(attr_item.path.span).unwrap())
431 } else {
432 self.unexpected_any()
433 };
434 }
435
436 let lo = self.token.span;
437 let is_unsafe = if unsafe_allowed == AllowLeadingUnsafe::Yes {
438 self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe))
439 } else {
440 false
441 };
442 let unsafety = if is_unsafe {
443 let unsafe_span = self.prev_token.span;
444 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenParen,
token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen))?;
445
446 ast::Safety::Unsafe(unsafe_span)
447 } else {
448 ast::Safety::Default
449 };
450
451 let path = self.parse_path(PathStyle::Mod)?;
452 let kind = self.parse_meta_item_kind()?;
453 if is_unsafe {
454 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseParen,
token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen))?;
455 }
456 let span = lo.to(self.prev_token.span);
457
458 Ok(ast::MetaItem { unsafety, path, kind, span })
459 }
460
461 pub(crate) fn parse_meta_item_kind(&mut self) -> PResult<'a, ast::MetaItemKind> {
462 Ok(if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) {
463 ast::MetaItemKind::NameValue(self.parse_unsuffixed_meta_item_lit()?)
464 } else if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenParen,
token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
465 let (list, _) = self.parse_paren_comma_seq(|p| p.parse_meta_item_inner())?;
466 ast::MetaItemKind::List(list)
467 } else {
468 ast::MetaItemKind::Word
469 })
470 }
471
472 pub fn parse_meta_item_inner(&mut self) -> PResult<'a, ast::MetaItemInner> {
478 match self.parse_unsuffixed_meta_item_lit() {
479 Ok(lit) => return Ok(ast::MetaItemInner::Lit(lit)),
480 Err(err) => err.cancel(), }
482
483 match self.parse_meta_item(AllowLeadingUnsafe::No) {
484 Ok(mi) => return Ok(ast::MetaItemInner::MetaItem(mi)),
485 Err(err) => err.cancel(), }
487
488 let mut err = errors::InvalidMetaItem {
489 span: self.token.span,
490 descr: super::token_descr(&self.token),
491 quote_ident_sugg: None,
492 };
493
494 if self.prev_token == token::Eq
498 && let token::Ident(..) = self.token.kind
499 {
500 let before = self.token.span.shrink_to_lo();
501 while let token::Ident(..) = self.token.kind {
502 self.bump();
503 }
504 err.quote_ident_sugg = Some(errors::InvalidMetaItemQuoteIdentSugg {
505 before,
506 after: self.prev_token.span.shrink_to_hi(),
507 });
508 }
509
510 Err(self.dcx().create_err(err))
511 }
512}