1use std::fmt::Write;
2use std::mem;
3
4use ast::token::IdentIsRaw;
5use rustc_ast::ast::*;
6use rustc_ast::token::{self, Delimiter, InvisibleOrigin, MetaVarKind, TokenKind};
7use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
8use rustc_ast::util::case::Case;
9use rustc_ast::{self as ast};
10use rustc_ast_pretty::pprust;
11use rustc_errors::codes::*;
12use rustc_errors::{Applicability, PResult, StashKey, inline_fluent, struct_span_code_err};
13use rustc_session::lint::builtin::VARARGS_WITHOUT_PATTERN;
14use rustc_span::edit_distance::edit_distance;
15use rustc_span::edition::Edition;
16use rustc_span::{DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, source_map, sym};
17use thin_vec::{ThinVec, thin_vec};
18use tracing::debug;
19
20use super::diagnostics::{ConsumeClosingDelim, dummy_arg};
21use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
22use super::{
23 AllowConstBlockItems, AttrWrapper, ExpKeywordPair, ExpTokenPair, FollowedByType, ForceCollect,
24 Parser, PathStyle, Recovered, Trailing, UsePreAttrPos,
25};
26use crate::errors::{self, FnPointerCannotBeAsync, FnPointerCannotBeConst, MacroExpandsToAdtField};
27use crate::exp;
28
29impl<'a> Parser<'a> {
30 pub fn parse_crate_mod(&mut self) -> PResult<'a, ast::Crate> {
32 let (attrs, items, spans) = self.parse_mod(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eof,
token_type: crate::parser::token_type::TokenType::Eof,
}exp!(Eof))?;
33 Ok(ast::Crate { attrs, items, spans, id: DUMMY_NODE_ID, is_placeholder: false })
34 }
35
36 fn parse_item_mod(&mut self, attrs: &mut AttrVec) -> PResult<'a, ItemKind> {
38 let safety = self.parse_safety(Case::Sensitive);
39 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Mod,
token_type: crate::parser::token_type::TokenType::KwMod,
}exp!(Mod))?;
40 let ident = self.parse_ident()?;
41 let mod_kind = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
42 ModKind::Unloaded
43 } else {
44 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace))?;
45 let (inner_attrs, items, inner_span) = self.parse_mod(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace))?;
46 attrs.extend(inner_attrs);
47 ModKind::Loaded(items, Inline::Yes, inner_span)
48 };
49 Ok(ItemKind::Mod(safety, ident, mod_kind))
50 }
51
52 pub fn parse_mod(
57 &mut self,
58 term: ExpTokenPair,
59 ) -> PResult<'a, (AttrVec, ThinVec<Box<Item>>, ModSpans)> {
60 let lo = self.token.span;
61 let attrs = self.parse_inner_attributes()?;
62
63 let post_attr_lo = self.token.span;
64 let mut items: ThinVec<Box<_>> = ThinVec::new();
65
66 loop {
69 while self.maybe_consume_incorrect_semicolon(items.last().map(|x| &**x)) {} let Some(item) = self.parse_item(ForceCollect::No, AllowConstBlockItems::Yes)? else {
71 break;
72 };
73 items.push(item);
74 }
75
76 if !self.eat(term) {
77 let token_str = super::token_descr(&self.token);
78 if !self.maybe_consume_incorrect_semicolon(items.last().map(|x| &**x)) {
79 let is_let = self.token.is_keyword(kw::Let);
80 let is_let_mut = is_let && self.look_ahead(1, |t| t.is_keyword(kw::Mut));
81 let let_has_ident = is_let && !is_let_mut && self.is_kw_followed_by_ident(kw::Let);
82
83 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected item, found {0}",
token_str))
})format!("expected item, found {token_str}");
84 let mut err = self.dcx().struct_span_err(self.token.span, msg);
85
86 let label = if is_let {
87 "`let` cannot be used for global variables"
88 } else {
89 "expected item"
90 };
91 err.span_label(self.token.span, label);
92
93 if is_let {
94 if is_let_mut {
95 err.help("consider using `static` and a `Mutex` instead of `let mut`");
96 } else if let_has_ident {
97 err.span_suggestion_short(
98 self.token.span,
99 "consider using `static` or `const` instead of `let`",
100 "static",
101 Applicability::MaybeIncorrect,
102 );
103 } else {
104 err.help("consider using `static` or `const` instead of `let`");
105 }
106 }
107 err.note("for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>");
108 return Err(err);
109 }
110 }
111
112 let inject_use_span = post_attr_lo.data().with_hi(post_attr_lo.lo());
113 let mod_spans = ModSpans { inner_span: lo.to(self.prev_token.span), inject_use_span };
114 Ok((attrs, items, mod_spans))
115 }
116}
117
118enum ReuseKind {
119 Path,
120 Impl,
121}
122
123impl<'a> Parser<'a> {
124 pub fn parse_item(
125 &mut self,
126 force_collect: ForceCollect,
127 allow_const_block_items: AllowConstBlockItems,
128 ) -> PResult<'a, Option<Box<Item>>> {
129 let fn_parse_mode =
130 FnParseMode { req_name: |_, _| true, context: FnContext::Free, req_body: true };
131 self.parse_item_(fn_parse_mode, force_collect, allow_const_block_items)
132 .map(|i| i.map(Box::new))
133 }
134
135 fn parse_item_(
136 &mut self,
137 fn_parse_mode: FnParseMode,
138 force_collect: ForceCollect,
139 const_block_items_allowed: AllowConstBlockItems,
140 ) -> PResult<'a, Option<Item>> {
141 self.recover_vcs_conflict_marker();
142 let attrs = self.parse_outer_attributes()?;
143 self.recover_vcs_conflict_marker();
144 self.parse_item_common(
145 attrs,
146 true,
147 false,
148 fn_parse_mode,
149 force_collect,
150 const_block_items_allowed,
151 )
152 }
153
154 pub(super) fn parse_item_common(
155 &mut self,
156 attrs: AttrWrapper,
157 mac_allowed: bool,
158 attrs_allowed: bool,
159 fn_parse_mode: FnParseMode,
160 force_collect: ForceCollect,
161 allow_const_block_items: AllowConstBlockItems,
162 ) -> PResult<'a, Option<Item>> {
163 if let Some(item) = self.eat_metavar_seq(MetaVarKind::Item, |this| {
164 this.parse_item(ForceCollect::Yes, allow_const_block_items)
165 }) {
166 let mut item = item.expect("an actual item");
167 attrs.prepend_to_nt_inner(&mut item.attrs);
168 return Ok(Some(*item));
169 }
170
171 self.collect_tokens(None, attrs, force_collect, |this, mut attrs| {
172 let lo = this.token.span;
173 let vis = this.parse_visibility(FollowedByType::No)?;
174 let mut def = this.parse_defaultness();
175 let kind = this.parse_item_kind(
176 &mut attrs,
177 mac_allowed,
178 allow_const_block_items,
179 lo,
180 &vis,
181 &mut def,
182 fn_parse_mode,
183 Case::Sensitive,
184 )?;
185 if let Some(kind) = kind {
186 this.error_on_unconsumed_default(def, &kind);
187 let span = lo.to(this.prev_token.span);
188 let id = DUMMY_NODE_ID;
189 let item = Item { attrs, id, kind, vis, span, tokens: None };
190 return Ok((Some(item), Trailing::No, UsePreAttrPos::No));
191 }
192
193 if !#[allow(non_exhaustive_omitted_patterns)] match vis.kind {
VisibilityKind::Inherited => true,
_ => false,
}matches!(vis.kind, VisibilityKind::Inherited) {
195 this.dcx().emit_err(errors::VisibilityNotFollowedByItem { span: vis.span, vis });
196 }
197
198 if let Defaultness::Default(span) = def {
199 this.dcx().emit_err(errors::DefaultNotFollowedByItem { span });
200 }
201
202 if !attrs_allowed {
203 this.recover_attrs_no_item(&attrs)?;
204 }
205 Ok((None, Trailing::No, UsePreAttrPos::No))
206 })
207 }
208
209 fn error_on_unconsumed_default(&self, def: Defaultness, kind: &ItemKind) {
211 if let Defaultness::Default(span) = def {
212 self.dcx().emit_err(errors::InappropriateDefault {
213 span,
214 article: kind.article(),
215 descr: kind.descr(),
216 });
217 }
218 }
219
220 fn parse_item_kind(
222 &mut self,
223 attrs: &mut AttrVec,
224 macros_allowed: bool,
225 allow_const_block_items: AllowConstBlockItems,
226 lo: Span,
227 vis: &Visibility,
228 def: &mut Defaultness,
229 fn_parse_mode: FnParseMode,
230 case: Case,
231 ) -> PResult<'a, Option<ItemKind>> {
232 let check_pub = def == &Defaultness::Final;
233 let mut def_ = || mem::replace(def, Defaultness::Final);
234
235 let info = if !self.is_use_closure() && self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Use,
token_type: crate::parser::token_type::TokenType::KwUse,
}exp!(Use), case) {
236 self.parse_use_item()?
237 } else if self.check_fn_front_matter(check_pub, case) {
238 let (ident, sig, generics, contract, body) =
240 self.parse_fn(attrs, fn_parse_mode, lo, vis, case)?;
241 ItemKind::Fn(Box::new(Fn {
242 defaultness: def_(),
243 ident,
244 sig,
245 generics,
246 contract,
247 body,
248 define_opaque: None,
249 eii_impls: ThinVec::new(),
250 }))
251 } else if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Extern,
token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern), case) {
252 if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Crate,
token_type: crate::parser::token_type::TokenType::KwCrate,
}exp!(Crate), case) {
253 self.parse_item_extern_crate()?
255 } else {
256 self.parse_item_foreign_mod(attrs, Safety::Default)?
258 }
259 } else if self.is_unsafe_foreign_mod() {
260 let safety = self.parse_safety(Case::Sensitive);
262 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Extern,
token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern))?;
263 self.parse_item_foreign_mod(attrs, safety)?
264 } else if let Some(safety) = self.parse_global_static_front_matter(case) {
265 let mutability = self.parse_mutability();
267 self.parse_static_item(safety, mutability)?
268 } else if self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Trait,
token_type: crate::parser::token_type::TokenType::KwTrait,
}exp!(Trait), case) || self.check_trait_front_matter() {
269 self.parse_item_trait(attrs, lo)?
271 } else if self.check_impl_frontmatter(0) {
272 self.parse_item_impl(attrs, def_(), false)?
274 } else if let AllowConstBlockItems::Yes | AllowConstBlockItems::DoesNotMatter =
275 allow_const_block_items
276 && self.check_inline_const(0)
277 {
278 if let AllowConstBlockItems::DoesNotMatter = allow_const_block_items {
280 {
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/item.rs:280",
"rustc_parse::parser::item", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/parser/item.rs"),
::tracing_core::__macro_support::Option::Some(280u32),
::tracing_core::__macro_support::Option::Some("rustc_parse::parser::item"),
::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!("Parsing a const block item that does not matter: {0:?}",
self.token.span) as &dyn Value))])
});
} else { ; }
};debug!("Parsing a const block item that does not matter: {:?}", self.token.span);
281 };
282 ItemKind::ConstBlock(self.parse_const_block_item()?)
283 } else if let Const::Yes(const_span) = self.parse_constness(case) {
284 self.recover_const_mut(const_span);
286 self.recover_missing_kw_before_item()?;
287 let (ident, generics, ty, rhs_kind) = self.parse_const_item(false)?;
288 ItemKind::Const(Box::new(ConstItem {
289 defaultness: def_(),
290 ident,
291 generics,
292 ty,
293 rhs_kind,
294 define_opaque: None,
295 }))
296 } else if let Some(kind) = self.is_reuse_item() {
297 self.parse_item_delegation(attrs, def_(), kind)?
298 } else if self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Mod,
token_type: crate::parser::token_type::TokenType::KwMod,
}exp!(Mod), case)
299 || self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe), case) && self.is_keyword_ahead(1, &[kw::Mod])
300 {
301 self.parse_item_mod(attrs)?
303 } else if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Type,
token_type: crate::parser::token_type::TokenType::KwType,
}exp!(Type), case) {
304 if let Const::Yes(const_span) = self.parse_constness(case) {
305 self.recover_const_mut(const_span);
307 self.recover_missing_kw_before_item()?;
308 let (ident, generics, ty, rhs_kind) = self.parse_const_item(true)?;
309 self.psess.gated_spans.gate(sym::mgca_type_const_syntax, lo.to(const_span));
312 ItemKind::Const(Box::new(ConstItem {
313 defaultness: def_(),
314 ident,
315 generics,
316 ty,
317 rhs_kind,
318 define_opaque: None,
319 }))
320 } else {
321 self.parse_type_alias(def_())?
323 }
324 } else if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Enum,
token_type: crate::parser::token_type::TokenType::KwEnum,
}exp!(Enum), case) {
325 self.parse_item_enum()?
327 } else if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Struct,
token_type: crate::parser::token_type::TokenType::KwStruct,
}exp!(Struct), case) {
328 self.parse_item_struct()?
330 } else if self.is_kw_followed_by_ident(kw::Union) {
331 self.bump(); self.parse_item_union()?
334 } else if self.is_builtin() {
335 return self.parse_item_builtin();
337 } else if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Macro,
token_type: crate::parser::token_type::TokenType::KwMacro,
}exp!(Macro), case) {
338 self.parse_item_decl_macro(lo)?
340 } else if let IsMacroRulesItem::Yes { has_bang } = self.is_macro_rules_item() {
341 self.parse_item_macro_rules(vis, has_bang)?
343 } else if self.isnt_macro_invocation()
344 && (self.token.is_ident_named(sym::import)
345 || self.token.is_ident_named(sym::using)
346 || self.token.is_ident_named(sym::include)
347 || self.token.is_ident_named(sym::require))
348 {
349 return self.recover_import_as_use();
350 } else if self.isnt_macro_invocation() && vis.kind.is_pub() {
351 self.recover_missing_kw_before_item()?;
352 return Ok(None);
353 } else if self.isnt_macro_invocation() && case == Case::Sensitive {
354 _ = def_;
355
356 return self.parse_item_kind(
358 attrs,
359 macros_allowed,
360 allow_const_block_items,
361 lo,
362 vis,
363 def,
364 fn_parse_mode,
365 Case::Insensitive,
366 );
367 } else if macros_allowed && self.check_path() {
368 if self.isnt_macro_invocation() {
369 self.recover_missing_kw_before_item()?;
370 }
371 ItemKind::MacCall(Box::new(self.parse_item_macro(vis)?))
373 } else {
374 return Ok(None);
375 };
376 Ok(Some(info))
377 }
378
379 fn recover_import_as_use(&mut self) -> PResult<'a, Option<ItemKind>> {
380 let span = self.token.span;
381 let token_name = super::token_descr(&self.token);
382 let snapshot = self.create_snapshot_for_diagnostic();
383 self.bump();
384 match self.parse_use_item() {
385 Ok(u) => {
386 self.dcx().emit_err(errors::RecoverImportAsUse { span, token_name });
387 Ok(Some(u))
388 }
389 Err(e) => {
390 e.cancel();
391 self.restore_snapshot(snapshot);
392 Ok(None)
393 }
394 }
395 }
396
397 fn parse_use_item(&mut self) -> PResult<'a, ItemKind> {
398 let tree = self.parse_use_tree()?;
399 if let Err(mut e) = self.expect_semi() {
400 match tree.kind {
401 UseTreeKind::Glob => {
402 e.note("the wildcard token must be last on the path");
403 }
404 UseTreeKind::Nested { .. } => {
405 e.note("glob-like brace syntax must be last on the path");
406 }
407 _ => (),
408 }
409 return Err(e);
410 }
411 Ok(ItemKind::Use(tree))
412 }
413
414 pub(super) fn is_path_start_item(&mut self) -> bool {
416 self.is_kw_followed_by_ident(kw::Union) || self.is_reuse_item().is_some() || self.check_trait_front_matter() || self.is_async_fn() || #[allow(non_exhaustive_omitted_patterns)] match self.is_macro_rules_item() {
IsMacroRulesItem::Yes { .. } => true,
_ => false,
}matches!(self.is_macro_rules_item(), IsMacroRulesItem::Yes{..}) }
422
423 fn is_reuse_item(&mut self) -> Option<ReuseKind> {
424 if !self.token.is_keyword(kw::Reuse) {
425 return None;
426 }
427
428 if self.look_ahead(1, |t| t.is_path_start() && *t != token::PathSep) {
430 Some(ReuseKind::Path)
431 } else if self.check_impl_frontmatter(1) {
432 Some(ReuseKind::Impl)
433 } else {
434 None
435 }
436 }
437
438 fn isnt_macro_invocation(&mut self) -> bool {
440 self.check_ident() && self.look_ahead(1, |t| *t != token::Bang && *t != token::PathSep)
441 }
442
443 fn recover_missing_kw_before_item(&mut self) -> PResult<'a, ()> {
446 let is_pub = self.prev_token.is_keyword(kw::Pub);
447 let is_const = self.prev_token.is_keyword(kw::Const);
448 let ident_span = self.token.span;
449 let span = if is_pub { self.prev_token.span.to(ident_span) } else { ident_span };
450 let insert_span = ident_span.shrink_to_lo();
451
452 let ident = if self.token.is_ident()
453 && (!is_const || self.look_ahead(1, |t| *t == token::OpenParen))
454 && self.look_ahead(1, |t| {
455 #[allow(non_exhaustive_omitted_patterns)] match t.kind {
token::Lt | token::OpenBrace | token::OpenParen => true,
_ => false,
}matches!(t.kind, token::Lt | token::OpenBrace | token::OpenParen)
456 }) {
457 self.parse_ident_common(true).unwrap()
458 } else {
459 return Ok(());
460 };
461
462 let mut found_generics = false;
463 if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Lt,
token_type: crate::parser::token_type::TokenType::Lt,
}exp!(Lt)) {
464 found_generics = true;
465 self.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Gt,
token_type: crate::parser::token_type::TokenType::Gt,
}exp!(Gt)]);
466 self.bump(); }
468
469 let err = if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
470 if self.look_ahead(1, |t| *t == token::CloseBrace) {
472 Some(errors::MissingKeywordForItemDefinition::EnumOrStruct { span })
474 } else if self.look_ahead(2, |t| *t == token::Colon)
475 || self.look_ahead(3, |t| *t == token::Colon)
476 {
477 Some(errors::MissingKeywordForItemDefinition::Struct { span, insert_span, ident })
479 } else {
480 Some(errors::MissingKeywordForItemDefinition::Enum { span, insert_span, ident })
481 }
482 } else if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenParen,
token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
483 self.bump(); let is_method = self.recover_self_param();
486
487 self.consume_block(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenParen,
token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseParen,
token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen), ConsumeClosingDelim::Yes);
488
489 let err = if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::RArrow,
token_type: crate::parser::token_type::TokenType::RArrow,
}exp!(RArrow)) || self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
490 self.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)]);
491 self.bump(); self.consume_block(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), ConsumeClosingDelim::Yes);
493 if is_method {
494 errors::MissingKeywordForItemDefinition::Method { span, insert_span, ident }
495 } else {
496 errors::MissingKeywordForItemDefinition::Function { span, insert_span, ident }
497 }
498 } else if is_pub && self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
499 errors::MissingKeywordForItemDefinition::Struct { span, insert_span, ident }
500 } else {
501 errors::MissingKeywordForItemDefinition::Ambiguous {
502 span,
503 subdiag: if found_generics {
504 None
505 } else if let Ok(snippet) = self.span_to_snippet(ident_span) {
506 Some(errors::AmbiguousMissingKwForItemSub::SuggestMacro {
507 span: ident_span,
508 snippet,
509 })
510 } else {
511 Some(errors::AmbiguousMissingKwForItemSub::HelpMacro)
512 },
513 }
514 };
515 Some(err)
516 } else if found_generics {
517 Some(errors::MissingKeywordForItemDefinition::Ambiguous { span, subdiag: None })
518 } else {
519 None
520 };
521
522 if let Some(err) = err { Err(self.dcx().create_err(err)) } else { Ok(()) }
523 }
524
525 fn parse_item_builtin(&mut self) -> PResult<'a, Option<ItemKind>> {
526 Ok(None)
528 }
529
530 fn parse_item_macro(&mut self, vis: &Visibility) -> PResult<'a, MacCall> {
532 let path = self.parse_path(PathStyle::Mod)?; self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Bang,
token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang))?; match self.parse_delim_args() {
535 Ok(args) => {
537 self.eat_semi_for_macro_if_needed(&args);
538 self.complain_if_pub_macro(vis, false);
539 Ok(MacCall { path, args })
540 }
541
542 Err(mut err) => {
543 if self.token.is_ident()
545 && let [segment] = path.segments.as_slice()
546 && edit_distance("macro_rules", &segment.ident.to_string(), 2).is_some()
547 {
548 err.span_suggestion(
549 path.span,
550 "perhaps you meant to define a macro",
551 "macro_rules",
552 Applicability::MachineApplicable,
553 );
554 }
555 Err(err)
556 }
557 }
558 }
559
560 fn recover_attrs_no_item(&mut self, attrs: &[Attribute]) -> PResult<'a, ()> {
562 let ([start @ end] | [start, .., end]) = attrs else {
563 return Ok(());
564 };
565 let msg = if end.is_doc_comment() {
566 "expected item after doc comment"
567 } else {
568 "expected item after attributes"
569 };
570 let mut err = self.dcx().struct_span_err(end.span, msg);
571 if end.is_doc_comment() {
572 err.span_label(end.span, "this doc comment doesn't document anything");
573 } else if self.token == TokenKind::Semi {
574 err.span_suggestion_verbose(
575 self.token.span,
576 "consider removing this semicolon",
577 "",
578 Applicability::MaybeIncorrect,
579 );
580 }
581 if let [.., penultimate, _] = attrs {
582 err.span_label(start.span.to(penultimate.span), "other attributes here");
583 }
584 Err(err)
585 }
586
587 fn is_async_fn(&self) -> bool {
588 self.token.is_keyword(kw::Async) && self.is_keyword_ahead(1, &[kw::Fn])
589 }
590
591 fn parse_polarity(&mut self) -> ast::ImplPolarity {
592 if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Bang,
token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang)) && self.look_ahead(1, |t| t.can_begin_type()) {
594 self.bump(); ast::ImplPolarity::Negative(self.prev_token.span)
596 } else {
597 ast::ImplPolarity::Positive
598 }
599 }
600
601 fn parse_item_impl(
616 &mut self,
617 attrs: &mut AttrVec,
618 defaultness: Defaultness,
619 is_reuse: bool,
620 ) -> PResult<'a, ItemKind> {
621 let mut constness = self.parse_constness(Case::Sensitive);
622 let safety = self.parse_safety(Case::Sensitive);
623 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Impl,
token_type: crate::parser::token_type::TokenType::KwImpl,
}exp!(Impl))?;
624
625 let mut generics = if self.choose_generics_over_qpath(0) {
627 self.parse_generics()?
628 } else {
629 let mut generics = Generics::default();
630 generics.span = self.prev_token.span.shrink_to_hi();
633 generics
634 };
635
636 if let Const::No = constness {
637 constness = self.parse_constness(Case::Sensitive);
639 }
640
641 if let Const::Yes(span) = constness {
642 self.psess.gated_spans.gate(sym::const_trait_impl, span);
643 }
644
645 if (self.token_uninterpolated_span().at_least_rust_2018()
647 && self.token.is_keyword(kw::Async))
648 || self.is_kw_followed_by_ident(kw::Async)
649 {
650 self.bump();
651 self.dcx().emit_err(errors::AsyncImpl { span: self.prev_token.span });
652 }
653
654 let polarity = self.parse_polarity();
655
656 let ty_first = if self.token.is_keyword(kw::For) && self.look_ahead(1, |t| t != &token::Lt)
658 {
659 let span = self.prev_token.span.between(self.token.span);
660 return Err(self.dcx().create_err(errors::MissingTraitInTraitImpl {
661 span,
662 for_span: span.to(self.token.span),
663 }));
664 } else {
665 self.parse_ty_with_generics_recovery(&generics)?
666 };
667
668 let has_for = self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::For,
token_type: crate::parser::token_type::TokenType::KwFor,
}exp!(For));
670 let missing_for_span = self.prev_token.span.between(self.token.span);
671
672 let ty_second = if self.token == token::DotDot {
673 self.bump(); Some(self.mk_ty(self.prev_token.span, TyKind::Dummy))
680 } else if has_for || self.token.can_begin_type() {
681 Some(self.parse_ty()?)
682 } else {
683 None
684 };
685
686 generics.where_clause = self.parse_where_clause()?;
687
688 let impl_items = if is_reuse {
689 Default::default()
690 } else {
691 self.parse_item_list(attrs, |p| p.parse_impl_item(ForceCollect::No))?
692 };
693
694 let (of_trait, self_ty) = match ty_second {
695 Some(ty_second) => {
696 if !has_for {
698 self.dcx().emit_err(errors::MissingForInTraitImpl { span: missing_for_span });
699 }
700
701 let ty_first = *ty_first;
702 let path = match ty_first.kind {
703 TyKind::Path(None, path) => path,
705 other => {
706 if let TyKind::ImplTrait(_, bounds) = other
707 && let [bound] = bounds.as_slice()
708 && let GenericBound::Trait(poly_trait_ref) = bound
709 {
710 let extra_impl_kw = ty_first.span.until(bound.span());
714 self.dcx().emit_err(errors::ExtraImplKeywordInTraitImpl {
715 extra_impl_kw,
716 impl_trait_span: ty_first.span,
717 });
718 poly_trait_ref.trait_ref.path.clone()
719 } else {
720 return Err(self.dcx().create_err(
721 errors::ExpectedTraitInTraitImplFoundType { span: ty_first.span },
722 ));
723 }
724 }
725 };
726 let trait_ref = TraitRef { path, ref_id: ty_first.id };
727
728 let of_trait =
729 Some(Box::new(TraitImplHeader { defaultness, safety, polarity, trait_ref }));
730 (of_trait, ty_second)
731 }
732 None => {
733 let self_ty = ty_first;
734 let error = |modifier, modifier_name, modifier_span| {
735 self.dcx().create_err(errors::TraitImplModifierInInherentImpl {
736 span: self_ty.span,
737 modifier,
738 modifier_name,
739 modifier_span,
740 self_ty: self_ty.span,
741 })
742 };
743
744 if let Safety::Unsafe(span) = safety {
745 error("unsafe", "unsafe", span).with_code(E0197).emit();
746 }
747 if let ImplPolarity::Negative(span) = polarity {
748 error("!", "negative", span).emit();
749 }
750 if let Defaultness::Default(def_span) = defaultness {
751 error("default", "default", def_span).emit();
752 }
753 if let Const::Yes(span) = constness {
754 self.psess.gated_spans.gate(sym::const_trait_impl, span);
755 }
756 (None, self_ty)
757 }
758 };
759
760 Ok(ItemKind::Impl(Impl { generics, of_trait, self_ty, items: impl_items, constness }))
761 }
762
763 fn parse_item_delegation(
764 &mut self,
765 attrs: &mut AttrVec,
766 defaultness: Defaultness,
767 kind: ReuseKind,
768 ) -> PResult<'a, ItemKind> {
769 let span = self.token.span;
770 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Reuse,
token_type: crate::parser::token_type::TokenType::KwReuse,
}exp!(Reuse))?;
771
772 let item_kind = match kind {
773 ReuseKind::Path => self.parse_path_like_delegation(),
774 ReuseKind::Impl => self.parse_impl_delegation(span, attrs, defaultness),
775 }?;
776
777 self.psess.gated_spans.gate(sym::fn_delegation, span.to(self.prev_token.span));
778
779 Ok(item_kind)
780 }
781
782 fn parse_delegation_body(&mut self) -> PResult<'a, Option<Box<Block>>> {
783 Ok(if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
784 Some(self.parse_block()?)
785 } else {
786 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi))?;
787 None
788 })
789 }
790
791 fn parse_impl_delegation(
792 &mut self,
793 span: Span,
794 attrs: &mut AttrVec,
795 defaultness: Defaultness,
796 ) -> PResult<'a, ItemKind> {
797 let mut impl_item = self.parse_item_impl(attrs, defaultness, true)?;
798 let ItemKind::Impl(Impl { items, of_trait, .. }) = &mut impl_item else { ::core::panicking::panic("internal error: entered unreachable code")unreachable!() };
799
800 let until_expr_span = span.to(self.prev_token.span);
801
802 let Some(of_trait) = of_trait else {
803 return Err(self
804 .dcx()
805 .create_err(errors::ImplReuseInherentImpl { span: until_expr_span }));
806 };
807
808 let body = self.parse_delegation_body()?;
809 let whole_reuse_span = span.to(self.prev_token.span);
810
811 items.push(Box::new(AssocItem {
812 id: DUMMY_NODE_ID,
813 attrs: Default::default(),
814 span: whole_reuse_span,
815 tokens: None,
816 vis: Visibility {
817 kind: VisibilityKind::Inherited,
818 span: whole_reuse_span,
819 tokens: None,
820 },
821 kind: AssocItemKind::DelegationMac(Box::new(DelegationMac {
822 qself: None,
823 prefix: of_trait.trait_ref.path.clone(),
824 suffixes: None,
825 body,
826 })),
827 }));
828
829 Ok(impl_item)
830 }
831
832 fn parse_path_like_delegation(&mut self) -> PResult<'a, ItemKind> {
833 let (qself, path) = if self.eat_lt() {
834 let (qself, path) = self.parse_qpath(PathStyle::Expr)?;
835 (Some(qself), path)
836 } else {
837 (None, self.parse_path(PathStyle::Expr)?)
838 };
839
840 let rename = |this: &mut Self| {
841 Ok(if this.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::As,
token_type: crate::parser::token_type::TokenType::KwAs,
}exp!(As)) { Some(this.parse_ident()?) } else { None })
842 };
843
844 Ok(if self.eat_path_sep() {
845 let suffixes = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Star,
token_type: crate::parser::token_type::TokenType::Star,
}exp!(Star)) {
846 None
847 } else {
848 let parse_suffix = |p: &mut Self| Ok((p.parse_path_segment_ident()?, rename(p)?));
849 Some(self.parse_delim_comma_seq(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), parse_suffix)?.0)
850 };
851
852 ItemKind::DelegationMac(Box::new(DelegationMac {
853 qself,
854 prefix: path,
855 suffixes,
856 body: self.parse_delegation_body()?,
857 }))
858 } else {
859 let rename = rename(self)?;
860 let ident = rename.unwrap_or_else(|| path.segments.last().unwrap().ident);
861
862 ItemKind::Delegation(Box::new(Delegation {
863 id: DUMMY_NODE_ID,
864 qself,
865 path,
866 ident,
867 rename,
868 body: self.parse_delegation_body()?,
869 from_glob: false,
870 }))
871 })
872 }
873
874 fn parse_item_list<T>(
875 &mut self,
876 attrs: &mut AttrVec,
877 mut parse_item: impl FnMut(&mut Parser<'a>) -> PResult<'a, Option<Option<T>>>,
878 ) -> PResult<'a, ThinVec<T>> {
879 let open_brace_span = self.token.span;
880
881 if self.token == TokenKind::Semi {
883 self.dcx().emit_err(errors::UseEmptyBlockNotSemi { span: self.token.span });
884 self.bump();
885 return Ok(ThinVec::new());
886 }
887
888 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace))?;
889 attrs.extend(self.parse_inner_attributes()?);
890
891 let mut items = ThinVec::new();
892 while !self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace)) {
893 if self.recover_doc_comment_before_brace() {
894 continue;
895 }
896 self.recover_vcs_conflict_marker();
897 match parse_item(self) {
898 Ok(None) => {
899 let mut is_unnecessary_semicolon = !items.is_empty()
900 && self
918 .span_to_snippet(self.prev_token.span)
919 .is_ok_and(|snippet| snippet == "}")
920 && self.token == token::Semi;
921 let mut semicolon_span = self.token.span;
922 if !is_unnecessary_semicolon {
923 is_unnecessary_semicolon =
925 self.token == token::OpenBrace && self.prev_token == token::Semi;
926 semicolon_span = self.prev_token.span;
927 }
928 let non_item_span = self.token.span;
930 let is_let = self.token.is_keyword(kw::Let);
931
932 let mut err =
933 self.dcx().struct_span_err(non_item_span, "non-item in item list");
934 self.consume_block(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), ConsumeClosingDelim::Yes);
935 if is_let {
936 err.span_suggestion_verbose(
937 non_item_span,
938 "consider using `const` instead of `let` for associated const",
939 "const",
940 Applicability::MachineApplicable,
941 );
942 } else {
943 err.span_label(open_brace_span, "item list starts here")
944 .span_label(non_item_span, "non-item starts here")
945 .span_label(self.prev_token.span, "item list ends here");
946 }
947 if is_unnecessary_semicolon {
948 err.span_suggestion(
949 semicolon_span,
950 "consider removing this semicolon",
951 "",
952 Applicability::MaybeIncorrect,
953 );
954 }
955 err.emit();
956 break;
957 }
958 Ok(Some(item)) => items.extend(item),
959 Err(err) => {
960 self.consume_block(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), ConsumeClosingDelim::Yes);
961 err.with_span_label(
962 open_brace_span,
963 "while parsing this item list starting here",
964 )
965 .with_span_label(self.prev_token.span, "the item list ends here")
966 .emit();
967 break;
968 }
969 }
970 }
971 Ok(items)
972 }
973
974 fn recover_doc_comment_before_brace(&mut self) -> bool {
976 if let token::DocComment(..) = self.token.kind {
977 if self.look_ahead(1, |tok| tok == &token::CloseBrace) {
978 {
self.dcx().struct_span_err(self.token.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("found a documentation comment that doesn\'t document anything"))
})).with_code(E0584)
}struct_span_code_err!(
980 self.dcx(),
981 self.token.span,
982 E0584,
983 "found a documentation comment that doesn't document anything",
984 )
985 .with_span_label(self.token.span, "this doc comment doesn't document anything")
986 .with_help(
987 "doc comments must come before what they document, if a comment was \
988 intended use `//`",
989 )
990 .emit();
991 self.bump();
992 return true;
993 }
994 }
995 false
996 }
997
998 fn parse_defaultness(&mut self) -> Defaultness {
1000 if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Default,
token_type: crate::parser::token_type::TokenType::KwDefault,
}exp!(Default))
1004 && self.look_ahead(1, |t| t.is_non_raw_ident_where(|i| i.name != kw::As))
1005 {
1006 self.bump(); Defaultness::Default(self.prev_token_uninterpolated_span())
1008 } else {
1009 Defaultness::Final
1010 }
1011 }
1012
1013 fn check_trait_front_matter(&mut self) -> bool {
1015 self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Auto,
token_type: crate::parser::token_type::TokenType::KwAuto,
}exp!(Auto)) && self.is_keyword_ahead(1, &[kw::Trait])
1017 || self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe)) && self.is_keyword_ahead(1, &[kw::Trait, kw::Auto])
1019 || self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const)) && ((self.is_keyword_ahead(1, &[kw::Trait]) || self.is_keyword_ahead(1, &[kw::Auto]) && self.is_keyword_ahead(2, &[kw::Trait]))
1020 || self.is_keyword_ahead(1, &[kw::Unsafe]) && self.is_keyword_ahead(2, &[kw::Trait, kw::Auto]))
1021 }
1022
1023 fn parse_item_trait(&mut self, attrs: &mut AttrVec, lo: Span) -> PResult<'a, ItemKind> {
1025 let constness = self.parse_constness(Case::Sensitive);
1026 if let Const::Yes(span) = constness {
1027 self.psess.gated_spans.gate(sym::const_trait_impl, span);
1028 }
1029 let safety = self.parse_safety(Case::Sensitive);
1030 let is_auto = if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Auto,
token_type: crate::parser::token_type::TokenType::KwAuto,
}exp!(Auto)) {
1032 self.psess.gated_spans.gate(sym::auto_traits, self.prev_token.span);
1033 IsAuto::Yes
1034 } else {
1035 IsAuto::No
1036 };
1037
1038 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Trait,
token_type: crate::parser::token_type::TokenType::KwTrait,
}exp!(Trait))?;
1039 let ident = self.parse_ident()?;
1040 let mut generics = self.parse_generics()?;
1041
1042 let had_colon = self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon));
1044 let span_at_colon = self.prev_token.span;
1045 let bounds = if had_colon { self.parse_generic_bounds()? } else { Vec::new() };
1046
1047 let span_before_eq = self.prev_token.span;
1048 if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) {
1049 if had_colon {
1051 let span = span_at_colon.to(span_before_eq);
1052 self.dcx().emit_err(errors::BoundsNotAllowedOnTraitAliases { span });
1053 }
1054
1055 let bounds = self.parse_generic_bounds()?;
1056 generics.where_clause = self.parse_where_clause()?;
1057 self.expect_semi()?;
1058
1059 let whole_span = lo.to(self.prev_token.span);
1060 if is_auto == IsAuto::Yes {
1061 self.dcx().emit_err(errors::TraitAliasCannotBeAuto { span: whole_span });
1062 }
1063 if let Safety::Unsafe(_) = safety {
1064 self.dcx().emit_err(errors::TraitAliasCannotBeUnsafe { span: whole_span });
1065 }
1066
1067 self.psess.gated_spans.gate(sym::trait_alias, whole_span);
1068
1069 Ok(ItemKind::TraitAlias(Box::new(TraitAlias { constness, ident, generics, bounds })))
1070 } else {
1071 generics.where_clause = self.parse_where_clause()?;
1073 let items = self.parse_item_list(attrs, |p| p.parse_trait_item(ForceCollect::No))?;
1074 Ok(ItemKind::Trait(Box::new(Trait {
1075 constness,
1076 is_auto,
1077 safety,
1078 ident,
1079 generics,
1080 bounds,
1081 items,
1082 })))
1083 }
1084 }
1085
1086 pub fn parse_impl_item(
1087 &mut self,
1088 force_collect: ForceCollect,
1089 ) -> PResult<'a, Option<Option<Box<AssocItem>>>> {
1090 let fn_parse_mode =
1091 FnParseMode { req_name: |_, _| true, context: FnContext::Impl, req_body: true };
1092 self.parse_assoc_item(fn_parse_mode, force_collect)
1093 }
1094
1095 pub fn parse_trait_item(
1096 &mut self,
1097 force_collect: ForceCollect,
1098 ) -> PResult<'a, Option<Option<Box<AssocItem>>>> {
1099 let fn_parse_mode = FnParseMode {
1100 req_name: |edition, _| edition >= Edition::Edition2018,
1101 context: FnContext::Trait,
1102 req_body: false,
1103 };
1104 self.parse_assoc_item(fn_parse_mode, force_collect)
1105 }
1106
1107 fn parse_assoc_item(
1109 &mut self,
1110 fn_parse_mode: FnParseMode,
1111 force_collect: ForceCollect,
1112 ) -> PResult<'a, Option<Option<Box<AssocItem>>>> {
1113 Ok(self
1114 .parse_item_(
1115 fn_parse_mode,
1116 force_collect,
1117 AllowConstBlockItems::DoesNotMatter, )?
1119 .map(|Item { attrs, id, span, vis, kind, tokens }| {
1120 let kind = match AssocItemKind::try_from(kind) {
1121 Ok(kind) => kind,
1122 Err(kind) => match kind {
1123 ItemKind::Static(box StaticItem {
1124 ident,
1125 ty,
1126 safety: _,
1127 mutability: _,
1128 expr,
1129 define_opaque,
1130 }) => {
1131 self.dcx().emit_err(errors::AssociatedStaticItemNotAllowed { span });
1132 AssocItemKind::Const(Box::new(ConstItem {
1133 defaultness: Defaultness::Final,
1134 ident,
1135 generics: Generics::default(),
1136 ty,
1137 rhs_kind: ConstItemRhsKind::Body { rhs: expr },
1138 define_opaque,
1139 }))
1140 }
1141 _ => return self.error_bad_item_kind(span, &kind, "`trait`s or `impl`s"),
1142 },
1143 };
1144 Some(Box::new(Item { attrs, id, span, vis, kind, tokens }))
1145 }))
1146 }
1147
1148 fn parse_type_alias(&mut self, defaultness: Defaultness) -> PResult<'a, ItemKind> {
1154 let ident = self.parse_ident()?;
1155 let mut generics = self.parse_generics()?;
1156
1157 let bounds = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)) { self.parse_generic_bounds()? } else { Vec::new() };
1159 generics.where_clause = self.parse_where_clause()?;
1160
1161 let ty = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) { Some(self.parse_ty()?) } else { None };
1162
1163 let after_where_clause = self.parse_where_clause()?;
1164
1165 self.expect_semi()?;
1166
1167 Ok(ItemKind::TyAlias(Box::new(TyAlias {
1168 defaultness,
1169 ident,
1170 generics,
1171 after_where_clause,
1172 bounds,
1173 ty,
1174 })))
1175 }
1176
1177 fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
1187 let lo = self.token.span;
1188
1189 let mut prefix =
1190 ast::Path { segments: ThinVec::new(), span: lo.shrink_to_lo(), tokens: None };
1191 let kind =
1192 if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) || self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Star,
token_type: crate::parser::token_type::TokenType::Star,
}exp!(Star)) || self.is_import_coupler() {
1193 let mod_sep_ctxt = self.token.span.ctxt();
1195 if self.eat_path_sep() {
1196 prefix
1197 .segments
1198 .push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
1199 }
1200
1201 self.parse_use_tree_glob_or_nested()?
1202 } else {
1203 prefix = self.parse_path(PathStyle::Mod)?;
1205
1206 if self.eat_path_sep() {
1207 self.parse_use_tree_glob_or_nested()?
1208 } else {
1209 while self.eat_noexpect(&token::Colon) {
1211 self.dcx()
1212 .emit_err(errors::SingleColonImportPath { span: self.prev_token.span });
1213
1214 self.parse_path_segments(&mut prefix.segments, PathStyle::Mod, None)?;
1216 prefix.span = lo.to(self.prev_token.span);
1217 }
1218
1219 UseTreeKind::Simple(self.parse_rename()?)
1220 }
1221 };
1222
1223 Ok(UseTree { prefix, kind, span: lo.to(self.prev_token.span) })
1224 }
1225
1226 fn parse_use_tree_glob_or_nested(&mut self) -> PResult<'a, UseTreeKind> {
1228 Ok(if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Star,
token_type: crate::parser::token_type::TokenType::Star,
}exp!(Star)) {
1229 UseTreeKind::Glob
1230 } else {
1231 let lo = self.token.span;
1232 UseTreeKind::Nested {
1233 items: self.parse_use_tree_list()?,
1234 span: lo.to(self.prev_token.span),
1235 }
1236 })
1237 }
1238
1239 fn parse_use_tree_list(&mut self) -> PResult<'a, ThinVec<(UseTree, ast::NodeId)>> {
1245 self.parse_delim_comma_seq(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), |p| {
1246 p.recover_vcs_conflict_marker();
1247 Ok((p.parse_use_tree()?, DUMMY_NODE_ID))
1248 })
1249 .map(|(r, _)| r)
1250 }
1251
1252 fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
1253 if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::As,
token_type: crate::parser::token_type::TokenType::KwAs,
}exp!(As)) {
1254 self.parse_ident_or_underscore().map(Some)
1255 } else {
1256 Ok(None)
1257 }
1258 }
1259
1260 fn parse_ident_or_underscore(&mut self) -> PResult<'a, Ident> {
1261 match self.token.ident() {
1262 Some((ident @ Ident { name: kw::Underscore, .. }, IdentIsRaw::No)) => {
1263 self.bump();
1264 Ok(ident)
1265 }
1266 _ => self.parse_ident(),
1267 }
1268 }
1269
1270 fn parse_item_extern_crate(&mut self) -> PResult<'a, ItemKind> {
1279 let orig_ident = self.parse_crate_name_with_dashes()?;
1281 let (orig_name, item_ident) = if let Some(rename) = self.parse_rename()? {
1282 (Some(orig_ident.name), rename)
1283 } else {
1284 (None, orig_ident)
1285 };
1286 self.expect_semi()?;
1287 Ok(ItemKind::ExternCrate(orig_name, item_ident))
1288 }
1289
1290 fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, Ident> {
1291 let ident = if self.token.is_keyword(kw::SelfLower) {
1292 self.parse_path_segment_ident()
1293 } else {
1294 self.parse_ident()
1295 }?;
1296
1297 let dash = crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Minus,
token_type: crate::parser::token_type::TokenType::Minus,
}exp!(Minus);
1298 if self.token != dash.tok {
1299 return Ok(ident);
1300 }
1301
1302 let mut dashes = ::alloc::vec::Vec::new()vec![];
1304 let mut idents = ::alloc::vec::Vec::new()vec![];
1305 while self.eat(dash) {
1306 dashes.push(self.prev_token.span);
1307 idents.push(self.parse_ident()?);
1308 }
1309
1310 let fixed_name_sp = ident.span.to(idents.last().unwrap().span);
1311 let mut fixed_name = ident.name.to_string();
1312 for part in idents {
1313 fixed_name.write_fmt(format_args!("_{0}", part.name))write!(fixed_name, "_{}", part.name).unwrap();
1314 }
1315
1316 self.dcx().emit_err(errors::ExternCrateNameWithDashes {
1317 span: fixed_name_sp,
1318 sugg: errors::ExternCrateNameWithDashesSugg { dashes },
1319 });
1320
1321 Ok(Ident::from_str_and_span(&fixed_name, fixed_name_sp))
1322 }
1323
1324 fn parse_item_foreign_mod(
1335 &mut self,
1336 attrs: &mut AttrVec,
1337 mut safety: Safety,
1338 ) -> PResult<'a, ItemKind> {
1339 let extern_span = self.prev_token_uninterpolated_span();
1340 let abi = self.parse_abi(); if safety == Safety::Default
1343 && self.token.is_keyword(kw::Unsafe)
1344 && self.look_ahead(1, |t| *t == token::OpenBrace)
1345 {
1346 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)).unwrap_err().emit();
1347 safety = Safety::Unsafe(self.token.span);
1348 let _ = self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe));
1349 }
1350 Ok(ItemKind::ForeignMod(ast::ForeignMod {
1351 extern_span,
1352 safety,
1353 abi,
1354 items: self.parse_item_list(attrs, |p| p.parse_foreign_item(ForceCollect::No))?,
1355 }))
1356 }
1357
1358 pub fn parse_foreign_item(
1360 &mut self,
1361 force_collect: ForceCollect,
1362 ) -> PResult<'a, Option<Option<Box<ForeignItem>>>> {
1363 let fn_parse_mode = FnParseMode {
1364 req_name: |_, is_dot_dot_dot| is_dot_dot_dot == IsDotDotDot::No,
1365 context: FnContext::Free,
1366 req_body: false,
1367 };
1368 Ok(self
1369 .parse_item_(
1370 fn_parse_mode,
1371 force_collect,
1372 AllowConstBlockItems::DoesNotMatter, )?
1374 .map(|Item { attrs, id, span, vis, kind, tokens }| {
1375 let kind = match ForeignItemKind::try_from(kind) {
1376 Ok(kind) => kind,
1377 Err(kind) => match kind {
1378 ItemKind::Const(box ConstItem { ident, ty, rhs_kind, .. }) => {
1379 let const_span = Some(span.with_hi(ident.span.lo()))
1380 .filter(|span| span.can_be_used_for_suggestions());
1381 self.dcx().emit_err(errors::ExternItemCannotBeConst {
1382 ident_span: ident.span,
1383 const_span,
1384 });
1385 ForeignItemKind::Static(Box::new(StaticItem {
1386 ident,
1387 ty,
1388 mutability: Mutability::Not,
1389 expr: match rhs_kind {
1390 ConstItemRhsKind::Body { rhs } => rhs,
1391 ConstItemRhsKind::TypeConst { rhs: Some(anon) } => {
1392 Some(anon.value)
1393 }
1394 ConstItemRhsKind::TypeConst { rhs: None } => None,
1395 },
1396 safety: Safety::Default,
1397 define_opaque: None,
1398 }))
1399 }
1400 _ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
1401 },
1402 };
1403 Some(Box::new(Item { attrs, id, span, vis, kind, tokens }))
1404 }))
1405 }
1406
1407 fn error_bad_item_kind<T>(&self, span: Span, kind: &ItemKind, ctx: &'static str) -> Option<T> {
1408 let span = self.psess.source_map().guess_head_span(span);
1410 let descr = kind.descr();
1411 let help = match kind {
1412 ItemKind::DelegationMac(deleg) if deleg.suffixes.is_none() => false,
1413 _ => true,
1414 };
1415 self.dcx().emit_err(errors::BadItemKind { span, descr, ctx, help });
1416 None
1417 }
1418
1419 fn is_use_closure(&self) -> bool {
1420 if self.token.is_keyword(kw::Use) {
1421 self.look_ahead(1, |token| {
1423 let dist =
1425 if token.is_keyword(kw::Move) || token.is_keyword(kw::Async) { 2 } else { 1 };
1426
1427 self.look_ahead(dist, |token| #[allow(non_exhaustive_omitted_patterns)] match token.kind {
token::Or | token::OrOr => true,
_ => false,
}matches!(token.kind, token::Or | token::OrOr))
1428 })
1429 } else {
1430 false
1431 }
1432 }
1433
1434 fn is_unsafe_foreign_mod(&self) -> bool {
1435 if !self.token.is_keyword(kw::Unsafe) {
1437 return false;
1438 }
1439 if !self.is_keyword_ahead(1, &[kw::Extern]) {
1441 return false;
1442 }
1443
1444 let n = if self.look_ahead(2, |t| t.can_begin_string_literal()) { 3 } else { 2 };
1446
1447 self.tree_look_ahead(n, |t| #[allow(non_exhaustive_omitted_patterns)] match t {
TokenTree::Delimited(_, _, Delimiter::Brace, _) => true,
_ => false,
}matches!(t, TokenTree::Delimited(_, _, Delimiter::Brace, _)))
1452 == Some(true)
1453 }
1454
1455 fn parse_global_static_front_matter(&mut self, case: Case) -> Option<Safety> {
1456 let is_global_static = if self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Static,
token_type: crate::parser::token_type::TokenType::KwStatic,
}exp!(Static), case) {
1457 !self.look_ahead(1, |token| {
1459 if token.is_keyword_case(kw::Move, case) || token.is_keyword_case(kw::Use, case) {
1460 return true;
1461 }
1462 #[allow(non_exhaustive_omitted_patterns)] match token.kind {
token::Or | token::OrOr => true,
_ => false,
}matches!(token.kind, token::Or | token::OrOr)
1463 })
1464 } else {
1465 (self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe), case)
1467 || self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Safe,
token_type: crate::parser::token_type::TokenType::KwSafe,
}exp!(Safe), case))
1468 && self.look_ahead(1, |t| t.is_keyword_case(kw::Static, case))
1469 };
1470
1471 if is_global_static {
1472 let safety = self.parse_safety(case);
1473 let _ = self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Static,
token_type: crate::parser::token_type::TokenType::KwStatic,
}exp!(Static), case);
1474 Some(safety)
1475 } else {
1476 None
1477 }
1478 }
1479
1480 fn recover_const_mut(&mut self, const_span: Span) {
1482 if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Mut,
token_type: crate::parser::token_type::TokenType::KwMut,
}exp!(Mut)) {
1483 let span = self.prev_token.span;
1484 self.dcx()
1485 .emit_err(errors::ConstGlobalCannotBeMutable { ident_span: span, const_span });
1486 } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Let,
token_type: crate::parser::token_type::TokenType::KwLet,
}exp!(Let)) {
1487 let span = self.prev_token.span;
1488 self.dcx().emit_err(errors::ConstLetMutuallyExclusive { span: const_span.to(span) });
1489 }
1490 }
1491
1492 fn parse_const_block_item(&mut self) -> PResult<'a, ConstBlockItem> {
1493 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const))?;
1494 let const_span = self.prev_token.span;
1495 self.psess.gated_spans.gate(sym::const_block_items, const_span);
1496 let block = self.parse_block()?;
1497 Ok(ConstBlockItem { id: DUMMY_NODE_ID, span: const_span.to(block.span), block })
1498 }
1499
1500 fn parse_static_item(
1507 &mut self,
1508 safety: Safety,
1509 mutability: Mutability,
1510 ) -> PResult<'a, ItemKind> {
1511 let ident = self.parse_ident()?;
1512
1513 if self.token == TokenKind::Lt && self.may_recover() {
1514 let generics = self.parse_generics()?;
1515 self.dcx().emit_err(errors::StaticWithGenerics { span: generics.span });
1516 }
1517
1518 let ty = match (self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)), self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) | self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi))) {
1521 (true, false) => self.parse_ty()?,
1522 (colon, _) => self.recover_missing_global_item_type(colon, Some(mutability)),
1525 };
1526
1527 let expr = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) { Some(self.parse_expr()?) } else { None };
1528
1529 self.expect_semi()?;
1530
1531 let item = StaticItem { ident, ty, safety, mutability, expr, define_opaque: None };
1532 Ok(ItemKind::Static(Box::new(item)))
1533 }
1534
1535 fn parse_const_item(
1544 &mut self,
1545 const_arg: bool,
1546 ) -> PResult<'a, (Ident, Generics, Box<Ty>, ConstItemRhsKind)> {
1547 let ident = self.parse_ident_or_underscore()?;
1548
1549 let mut generics = self.parse_generics()?;
1550
1551 if !generics.span.is_empty() {
1554 self.psess.gated_spans.gate(sym::generic_const_items, generics.span);
1555 }
1556
1557 let ty = match (
1560 self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)),
1561 self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) | self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) | self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Where,
token_type: crate::parser::token_type::TokenType::KwWhere,
}exp!(Where)),
1562 ) {
1563 (true, false) => self.parse_ty()?,
1564 (colon, _) => self.recover_missing_global_item_type(colon, None),
1566 };
1567
1568 let before_where_clause =
1571 if self.may_recover() { self.parse_where_clause()? } else { WhereClause::default() };
1572
1573 let rhs = match (self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)), const_arg) {
1574 (true, true) => ConstItemRhsKind::TypeConst {
1575 rhs: Some(
1576 self.parse_expr_anon_const(|this, expr| this.mgca_direct_lit_hack(expr))?,
1577 ),
1578 },
1579 (true, false) => ConstItemRhsKind::Body { rhs: Some(self.parse_expr()?) },
1580 (false, true) => ConstItemRhsKind::TypeConst { rhs: None },
1581 (false, false) => ConstItemRhsKind::Body { rhs: None },
1582 };
1583
1584 let after_where_clause = self.parse_where_clause()?;
1585
1586 if before_where_clause.has_where_token
1590 && let Some(rhs_span) = rhs.span()
1591 {
1592 self.dcx().emit_err(errors::WhereClauseBeforeConstBody {
1593 span: before_where_clause.span,
1594 name: ident.span,
1595 body: rhs_span,
1596 sugg: if !after_where_clause.has_where_token {
1597 self.psess.source_map().span_to_snippet(rhs_span).ok().map(|body_s| {
1598 errors::WhereClauseBeforeConstBodySugg {
1599 left: before_where_clause.span.shrink_to_lo(),
1600 snippet: body_s,
1601 right: before_where_clause.span.shrink_to_hi().to(rhs_span),
1602 }
1603 })
1604 } else {
1605 None
1608 },
1609 });
1610 }
1611
1612 let mut predicates = before_where_clause.predicates;
1619 predicates.extend(after_where_clause.predicates);
1620 let where_clause = WhereClause {
1621 has_where_token: before_where_clause.has_where_token
1622 || after_where_clause.has_where_token,
1623 predicates,
1624 span: if after_where_clause.has_where_token {
1625 after_where_clause.span
1626 } else {
1627 before_where_clause.span
1628 },
1629 };
1630
1631 if where_clause.has_where_token {
1632 self.psess.gated_spans.gate(sym::generic_const_items, where_clause.span);
1633 }
1634
1635 generics.where_clause = where_clause;
1636
1637 self.expect_semi()?;
1638
1639 Ok((ident, generics, ty, rhs))
1640 }
1641
1642 fn recover_missing_global_item_type(
1645 &mut self,
1646 colon_present: bool,
1647 m: Option<Mutability>,
1648 ) -> Box<Ty> {
1649 let kind = match m {
1652 Some(Mutability::Mut) => "static mut",
1653 Some(Mutability::Not) => "static",
1654 None => "const",
1655 };
1656
1657 let colon = match colon_present {
1658 true => "",
1659 false => ":",
1660 };
1661
1662 let span = self.prev_token.span.shrink_to_hi();
1663 let err = self.dcx().create_err(errors::MissingConstType { span, colon, kind });
1664 err.stash(span, StashKey::ItemNoType);
1665
1666 Box::new(Ty { kind: TyKind::Infer, span, id: ast::DUMMY_NODE_ID, tokens: None })
1669 }
1670
1671 fn parse_item_enum(&mut self) -> PResult<'a, ItemKind> {
1673 if self.token.is_keyword(kw::Struct) {
1674 let span = self.prev_token.span.to(self.token.span);
1675 let err = errors::EnumStructMutuallyExclusive { span };
1676 if self.look_ahead(1, |t| t.is_ident()) {
1677 self.bump();
1678 self.dcx().emit_err(err);
1679 } else {
1680 return Err(self.dcx().create_err(err));
1681 }
1682 }
1683
1684 let prev_span = self.prev_token.span;
1685 let ident = self.parse_ident()?;
1686 let mut generics = self.parse_generics()?;
1687 generics.where_clause = self.parse_where_clause()?;
1688
1689 let (variants, _) = if self.token == TokenKind::Semi {
1691 self.dcx().emit_err(errors::UseEmptyBlockNotSemi { span: self.token.span });
1692 self.bump();
1693 (::thin_vec::ThinVec::new()thin_vec![], Trailing::No)
1694 } else {
1695 self.parse_delim_comma_seq(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), |p| {
1696 p.parse_enum_variant(ident.span)
1697 })
1698 .map_err(|mut err| {
1699 err.span_label(ident.span, "while parsing this enum");
1700 if self.prev_token.is_non_reserved_ident() && self.token == token::Colon {
1702 let snapshot = self.create_snapshot_for_diagnostic();
1703 self.bump();
1704 match self.parse_ty() {
1705 Ok(_) => {
1706 err.span_suggestion_verbose(
1707 prev_span,
1708 "perhaps you meant to use `struct` here",
1709 "struct",
1710 Applicability::MaybeIncorrect,
1711 );
1712 }
1713 Err(e) => {
1714 e.cancel();
1715 }
1716 }
1717 self.restore_snapshot(snapshot);
1718 }
1719 self.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace)]);
1720 self.bump(); err
1722 })?
1723 };
1724
1725 let enum_definition = EnumDef { variants: variants.into_iter().flatten().collect() };
1726 Ok(ItemKind::Enum(ident, generics, enum_definition))
1727 }
1728
1729 fn parse_enum_variant(&mut self, span: Span) -> PResult<'a, Option<Variant>> {
1730 self.recover_vcs_conflict_marker();
1731 let variant_attrs = self.parse_outer_attributes()?;
1732 self.recover_vcs_conflict_marker();
1733 let help = "enum variants can be `Variant`, `Variant = <integer>`, \
1734 `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`";
1735 self.collect_tokens(None, variant_attrs, ForceCollect::No, |this, variant_attrs| {
1736 let vlo = this.token.span;
1737
1738 let vis = this.parse_visibility(FollowedByType::No)?;
1739 if !this.recover_nested_adt_item(kw::Enum)? {
1740 return Ok((None, Trailing::No, UsePreAttrPos::No));
1741 }
1742 let ident = this.parse_field_ident("enum", vlo)?;
1743
1744 if this.token == token::Bang {
1745 if let Err(err) = this.unexpected() {
1746 err.with_note(rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("macros cannot expand to enum variants"))inline_fluent!("macros cannot expand to enum variants")).emit();
1747 }
1748
1749 this.bump();
1750 this.parse_delim_args()?;
1751
1752 return Ok((None, Trailing::from(this.token == token::Comma), UsePreAttrPos::No));
1753 }
1754
1755 let struct_def = if this.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
1756 let (fields, recovered) =
1758 match this.parse_record_struct_body("struct", ident.span, false) {
1759 Ok((fields, recovered)) => (fields, recovered),
1760 Err(mut err) => {
1761 if this.token == token::Colon {
1762 return Err(err);
1764 }
1765 this.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace)]);
1766 this.bump(); err.span_label(span, "while parsing this enum");
1768 err.help(help);
1769 let guar = err.emit();
1770 (::thin_vec::ThinVec::new()thin_vec![], Recovered::Yes(guar))
1771 }
1772 };
1773 VariantData::Struct { fields, recovered }
1774 } else if this.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenParen,
token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
1775 let body = match this.parse_tuple_struct_body() {
1776 Ok(body) => body,
1777 Err(mut err) => {
1778 if this.token == token::Colon {
1779 return Err(err);
1781 }
1782 this.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseParen,
token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen)]);
1783 this.bump(); err.span_label(span, "while parsing this enum");
1785 err.help(help);
1786 err.emit();
1787 ::thin_vec::ThinVec::new()thin_vec![]
1788 }
1789 };
1790 VariantData::Tuple(body, DUMMY_NODE_ID)
1791 } else {
1792 VariantData::Unit(DUMMY_NODE_ID)
1793 };
1794
1795 let disr_expr = if this.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) {
1796 Some(this.parse_expr_anon_const(|_, _| MgcaDisambiguation::AnonConst)?)
1797 } else {
1798 None
1799 };
1800
1801 let vr = ast::Variant {
1802 ident,
1803 vis,
1804 id: DUMMY_NODE_ID,
1805 attrs: variant_attrs,
1806 data: struct_def,
1807 disr_expr,
1808 span: vlo.to(this.prev_token.span),
1809 is_placeholder: false,
1810 };
1811
1812 Ok((Some(vr), Trailing::from(this.token == token::Comma), UsePreAttrPos::No))
1813 })
1814 .map_err(|mut err| {
1815 err.help(help);
1816 err
1817 })
1818 }
1819
1820 fn parse_item_struct(&mut self) -> PResult<'a, ItemKind> {
1822 let ident = self.parse_ident()?;
1823
1824 let mut generics = self.parse_generics()?;
1825
1826 let vdata = if self.token.is_keyword(kw::Where) {
1841 let tuple_struct_body;
1842 (generics.where_clause, tuple_struct_body) =
1843 self.parse_struct_where_clause(ident, generics.span)?;
1844
1845 if let Some(body) = tuple_struct_body {
1846 let body = VariantData::Tuple(body, DUMMY_NODE_ID);
1848 self.expect_semi()?;
1849 body
1850 } else if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
1851 VariantData::Unit(DUMMY_NODE_ID)
1853 } else {
1854 let (fields, recovered) = self.parse_record_struct_body(
1856 "struct",
1857 ident.span,
1858 generics.where_clause.has_where_token,
1859 )?;
1860 VariantData::Struct { fields, recovered }
1861 }
1862 } else if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
1864 VariantData::Unit(DUMMY_NODE_ID)
1865 } else if self.token == token::OpenBrace {
1867 let (fields, recovered) = self.parse_record_struct_body(
1868 "struct",
1869 ident.span,
1870 generics.where_clause.has_where_token,
1871 )?;
1872 VariantData::Struct { fields, recovered }
1873 } else if self.token == token::OpenParen {
1875 let body = VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID);
1876 generics.where_clause = self.parse_where_clause()?;
1877 self.expect_semi()?;
1878 body
1879 } else {
1880 let err = errors::UnexpectedTokenAfterStructName::new(self.token.span, self.token);
1881 return Err(self.dcx().create_err(err));
1882 };
1883
1884 Ok(ItemKind::Struct(ident, generics, vdata))
1885 }
1886
1887 fn parse_item_union(&mut self) -> PResult<'a, ItemKind> {
1889 let ident = self.parse_ident()?;
1890
1891 let mut generics = self.parse_generics()?;
1892
1893 let vdata = if self.token.is_keyword(kw::Where) {
1894 generics.where_clause = self.parse_where_clause()?;
1895 let (fields, recovered) = self.parse_record_struct_body(
1896 "union",
1897 ident.span,
1898 generics.where_clause.has_where_token,
1899 )?;
1900 VariantData::Struct { fields, recovered }
1901 } else if self.token == token::OpenBrace {
1902 let (fields, recovered) = self.parse_record_struct_body(
1903 "union",
1904 ident.span,
1905 generics.where_clause.has_where_token,
1906 )?;
1907 VariantData::Struct { fields, recovered }
1908 } else {
1909 let token_str = super::token_descr(&self.token);
1910 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected `where` or `{{` after union name, found {0}",
token_str))
})format!("expected `where` or `{{` after union name, found {token_str}");
1911 let mut err = self.dcx().struct_span_err(self.token.span, msg);
1912 err.span_label(self.token.span, "expected `where` or `{` after union name");
1913 return Err(err);
1914 };
1915
1916 Ok(ItemKind::Union(ident, generics, vdata))
1917 }
1918
1919 pub(crate) fn parse_record_struct_body(
1924 &mut self,
1925 adt_ty: &str,
1926 ident_span: Span,
1927 parsed_where: bool,
1928 ) -> PResult<'a, (ThinVec<FieldDef>, Recovered)> {
1929 let mut fields = ThinVec::new();
1930 let mut recovered = Recovered::No;
1931 if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
1932 while self.token != token::CloseBrace {
1933 match self.parse_field_def(adt_ty, ident_span) {
1934 Ok(field) => {
1935 fields.push(field);
1936 }
1937 Err(mut err) => {
1938 self.consume_block(
1939 crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace),
1940 crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace),
1941 ConsumeClosingDelim::No,
1942 );
1943 err.span_label(ident_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("while parsing this {0}", adt_ty))
})format!("while parsing this {adt_ty}"));
1944 let guar = err.emit();
1945 recovered = Recovered::Yes(guar);
1946 break;
1947 }
1948 }
1949 }
1950 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace))?;
1951 } else {
1952 let token_str = super::token_descr(&self.token);
1953 let where_str = if parsed_where { "" } else { "`where`, or " };
1954 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected {0}`{{` after struct name, found {1}",
where_str, token_str))
})format!("expected {where_str}`{{` after struct name, found {token_str}");
1955 let mut err = self.dcx().struct_span_err(self.token.span, msg);
1956 err.span_label(self.token.span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected {0}`{{` after struct name",
where_str))
})format!("expected {where_str}`{{` after struct name",));
1957 return Err(err);
1958 }
1959
1960 Ok((fields, recovered))
1961 }
1962
1963 fn parse_unsafe_field(&mut self) -> Safety {
1964 if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe)) {
1966 let span = self.prev_token.span;
1967 self.psess.gated_spans.gate(sym::unsafe_fields, span);
1968 Safety::Unsafe(span)
1969 } else {
1970 Safety::Default
1971 }
1972 }
1973
1974 pub(super) fn parse_tuple_struct_body(&mut self) -> PResult<'a, ThinVec<FieldDef>> {
1975 self.parse_paren_comma_seq(|p| {
1978 let attrs = p.parse_outer_attributes()?;
1979 p.collect_tokens(None, attrs, ForceCollect::No, |p, attrs| {
1980 let mut snapshot = None;
1981 if p.is_vcs_conflict_marker(&TokenKind::Shl, &TokenKind::Lt) {
1982 snapshot = Some(p.create_snapshot_for_diagnostic());
1986 }
1987 let lo = p.token.span;
1988 let vis = match p.parse_visibility(FollowedByType::Yes) {
1989 Ok(vis) => vis,
1990 Err(err) => {
1991 if let Some(ref mut snapshot) = snapshot {
1992 snapshot.recover_vcs_conflict_marker();
1993 }
1994 return Err(err);
1995 }
1996 };
1997 let ty = match p.parse_ty() {
2000 Ok(ty) => ty,
2001 Err(err) => {
2002 if let Some(ref mut snapshot) = snapshot {
2003 snapshot.recover_vcs_conflict_marker();
2004 }
2005 return Err(err);
2006 }
2007 };
2008 let mut default = None;
2009 if p.token == token::Eq {
2010 let mut snapshot = p.create_snapshot_for_diagnostic();
2011 snapshot.bump();
2012 match snapshot.parse_expr_anon_const(|_, _| MgcaDisambiguation::AnonConst) {
2013 Ok(const_expr) => {
2014 let sp = ty.span.shrink_to_hi().to(const_expr.value.span);
2015 p.psess.gated_spans.gate(sym::default_field_values, sp);
2016 p.restore_snapshot(snapshot);
2017 default = Some(const_expr);
2018 }
2019 Err(err) => {
2020 err.cancel();
2021 }
2022 }
2023 }
2024
2025 Ok((
2026 FieldDef {
2027 span: lo.to(ty.span),
2028 vis,
2029 safety: Safety::Default,
2030 ident: None,
2031 id: DUMMY_NODE_ID,
2032 ty,
2033 default,
2034 attrs,
2035 is_placeholder: false,
2036 },
2037 Trailing::from(p.token == token::Comma),
2038 UsePreAttrPos::No,
2039 ))
2040 })
2041 })
2042 .map(|(r, _)| r)
2043 }
2044
2045 fn parse_field_def(&mut self, adt_ty: &str, ident_span: Span) -> PResult<'a, FieldDef> {
2047 self.recover_vcs_conflict_marker();
2048 let attrs = self.parse_outer_attributes()?;
2049 self.recover_vcs_conflict_marker();
2050 self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
2051 let lo = this.token.span;
2052 let vis = this.parse_visibility(FollowedByType::No)?;
2053 let safety = this.parse_unsafe_field();
2054 this.parse_single_struct_field(adt_ty, lo, vis, safety, attrs, ident_span)
2055 .map(|field| (field, Trailing::No, UsePreAttrPos::No))
2056 })
2057 }
2058
2059 fn parse_single_struct_field(
2061 &mut self,
2062 adt_ty: &str,
2063 lo: Span,
2064 vis: Visibility,
2065 safety: Safety,
2066 attrs: AttrVec,
2067 ident_span: Span,
2068 ) -> PResult<'a, FieldDef> {
2069 let a_var = self.parse_name_and_ty(adt_ty, lo, vis, safety, attrs)?;
2070 match self.token.kind {
2071 token::Comma => {
2072 self.bump();
2073 }
2074 token::Semi => {
2075 self.bump();
2076 let sp = self.prev_token.span;
2077 let mut err =
2078 self.dcx().struct_span_err(sp, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} fields are separated by `,`",
adt_ty))
})format!("{adt_ty} fields are separated by `,`"));
2079 err.span_suggestion_short(
2080 sp,
2081 "replace `;` with `,`",
2082 ",",
2083 Applicability::MachineApplicable,
2084 );
2085 err.span_label(ident_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("while parsing this {0}", adt_ty))
})format!("while parsing this {adt_ty}"));
2086 err.emit();
2087 }
2088 token::CloseBrace => {}
2089 token::DocComment(..) => {
2090 let previous_span = self.prev_token.span;
2091 let mut err = errors::DocCommentDoesNotDocumentAnything {
2092 span: self.token.span,
2093 missing_comma: None,
2094 };
2095 self.bump(); if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma)) || self.token == token::CloseBrace {
2097 self.dcx().emit_err(err);
2098 } else {
2099 let sp = previous_span.shrink_to_hi();
2100 err.missing_comma = Some(sp);
2101 return Err(self.dcx().create_err(err));
2102 }
2103 }
2104 _ => {
2105 let sp = self.prev_token.span.shrink_to_hi();
2106 let msg =
2107 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected `,`, or `}}`, found {0}",
super::token_descr(&self.token)))
})format!("expected `,`, or `}}`, found {}", super::token_descr(&self.token));
2108
2109 if let TyKind::Path(_, Path { segments, .. }) = &a_var.ty.kind
2111 && let Some(last_segment) = segments.last()
2112 {
2113 let guar = self.check_trailing_angle_brackets(
2114 last_segment,
2115 &[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace)],
2116 );
2117 if let Some(_guar) = guar {
2118 let _ = self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma));
2121
2122 return Ok(a_var);
2125 }
2126 }
2127
2128 let mut err = self.dcx().struct_span_err(sp, msg);
2129
2130 if self.token.is_ident()
2131 || (self.token == TokenKind::Pound
2132 && (self.look_ahead(1, |t| t == &token::OpenBracket)))
2133 {
2134 err.span_suggestion(
2137 sp,
2138 "try adding a comma",
2139 ",",
2140 Applicability::MachineApplicable,
2141 );
2142 err.emit();
2143 } else {
2144 return Err(err);
2145 }
2146 }
2147 }
2148 Ok(a_var)
2149 }
2150
2151 fn expect_field_ty_separator(&mut self) -> PResult<'a, ()> {
2152 if let Err(err) = self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)) {
2153 let sm = self.psess.source_map();
2154 let eq_typo = self.token == token::Eq && self.look_ahead(1, |t| t.is_path_start());
2155 let semi_typo = self.token == token::Semi
2156 && self.look_ahead(1, |t| {
2157 t.is_path_start()
2158 && match (sm.lookup_line(self.token.span.hi()), sm.lookup_line(t.span.lo())) {
2161 (Ok(l), Ok(r)) => l.line == r.line,
2162 _ => true,
2163 }
2164 });
2165 if eq_typo || semi_typo {
2166 self.bump();
2167 err.with_span_suggestion_short(
2169 self.prev_token.span,
2170 "field names and their types are separated with `:`",
2171 ":",
2172 Applicability::MachineApplicable,
2173 )
2174 .emit();
2175 } else {
2176 return Err(err);
2177 }
2178 }
2179 Ok(())
2180 }
2181
2182 fn parse_name_and_ty(
2184 &mut self,
2185 adt_ty: &str,
2186 lo: Span,
2187 vis: Visibility,
2188 safety: Safety,
2189 attrs: AttrVec,
2190 ) -> PResult<'a, FieldDef> {
2191 let name = self.parse_field_ident(adt_ty, lo)?;
2192 if self.token == token::Bang {
2193 if let Err(mut err) = self.unexpected() {
2194 err.subdiagnostic(MacroExpandsToAdtField { adt_ty });
2196 return Err(err);
2197 }
2198 }
2199 self.expect_field_ty_separator()?;
2200 let ty = self.parse_ty()?;
2201 if self.token == token::Colon && self.look_ahead(1, |&t| t != token::Colon) {
2202 self.dcx()
2203 .struct_span_err(self.token.span, "found single colon in a struct field type path")
2204 .with_span_suggestion_verbose(
2205 self.token.span,
2206 "write a path separator here",
2207 "::",
2208 Applicability::MaybeIncorrect,
2209 )
2210 .emit();
2211 }
2212 let default = if self.token == token::Eq {
2213 self.bump();
2214 let const_expr = self.parse_expr_anon_const(|_, _| MgcaDisambiguation::AnonConst)?;
2215 let sp = ty.span.shrink_to_hi().to(const_expr.value.span);
2216 self.psess.gated_spans.gate(sym::default_field_values, sp);
2217 Some(const_expr)
2218 } else {
2219 None
2220 };
2221 Ok(FieldDef {
2222 span: lo.to(self.prev_token.span),
2223 ident: Some(name),
2224 vis,
2225 safety,
2226 id: DUMMY_NODE_ID,
2227 ty,
2228 default,
2229 attrs,
2230 is_placeholder: false,
2231 })
2232 }
2233
2234 fn parse_field_ident(&mut self, adt_ty: &str, lo: Span) -> PResult<'a, Ident> {
2237 let (ident, is_raw) = self.ident_or_err(true)?;
2238 if is_raw == IdentIsRaw::No && ident.is_reserved() {
2239 let snapshot = self.create_snapshot_for_diagnostic();
2240 let err = if self.check_fn_front_matter(false, Case::Sensitive) {
2241 let inherited_vis =
2242 Visibility { span: DUMMY_SP, kind: VisibilityKind::Inherited, tokens: None };
2243 let fn_parse_mode =
2245 FnParseMode { req_name: |_, _| true, context: FnContext::Free, req_body: true };
2246 match self.parse_fn(
2247 &mut AttrVec::new(),
2248 fn_parse_mode,
2249 lo,
2250 &inherited_vis,
2251 Case::Insensitive,
2252 ) {
2253 Ok(_) => {
2254 self.dcx().struct_span_err(
2255 lo.to(self.prev_token.span),
2256 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("functions are not allowed in {0} definitions",
adt_ty))
})format!("functions are not allowed in {adt_ty} definitions"),
2257 )
2258 .with_help(
2259 "unlike in C++, Java, and C#, functions are declared in `impl` blocks",
2260 )
2261 .with_help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information")
2262 }
2263 Err(err) => {
2264 err.cancel();
2265 self.restore_snapshot(snapshot);
2266 self.expected_ident_found_err()
2267 }
2268 }
2269 } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Struct,
token_type: crate::parser::token_type::TokenType::KwStruct,
}exp!(Struct)) {
2270 match self.parse_item_struct() {
2271 Ok(item) => {
2272 let ItemKind::Struct(ident, ..) = item else { ::core::panicking::panic("internal error: entered unreachable code")unreachable!() };
2273 self.dcx()
2274 .struct_span_err(
2275 lo.with_hi(ident.span.hi()),
2276 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("structs are not allowed in {0} definitions",
adt_ty))
})format!("structs are not allowed in {adt_ty} definitions"),
2277 )
2278 .with_help(
2279 "consider creating a new `struct` definition instead of nesting",
2280 )
2281 }
2282 Err(err) => {
2283 err.cancel();
2284 self.restore_snapshot(snapshot);
2285 self.expected_ident_found_err()
2286 }
2287 }
2288 } else {
2289 let mut err = self.expected_ident_found_err();
2290 if self.eat_keyword_noexpect(kw::Let)
2291 && let removal_span = self.prev_token.span.until(self.token.span)
2292 && let Ok(ident) = self
2293 .parse_ident_common(false)
2294 .map_err(|err| err.cancel())
2296 && self.token == TokenKind::Colon
2297 {
2298 err.span_suggestion(
2299 removal_span,
2300 "remove this `let` keyword",
2301 String::new(),
2302 Applicability::MachineApplicable,
2303 );
2304 err.note("the `let` keyword is not allowed in `struct` fields");
2305 err.note("see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information");
2306 err.emit();
2307 return Ok(ident);
2308 } else {
2309 self.restore_snapshot(snapshot);
2310 }
2311 err
2312 };
2313 return Err(err);
2314 }
2315 self.bump();
2316 Ok(ident)
2317 }
2318
2319 fn parse_item_decl_macro(&mut self, lo: Span) -> PResult<'a, ItemKind> {
2327 let ident = self.parse_ident()?;
2328 let body = if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
2329 self.parse_delim_args()? } else if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenParen,
token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
2331 let params = self.parse_token_tree(); let pspan = params.span();
2333 if !self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
2334 self.unexpected()?;
2335 }
2336 let body = self.parse_token_tree(); let bspan = body.span();
2339 let arrow = TokenTree::token_alone(token::FatArrow, pspan.between(bspan)); let tokens = TokenStream::new(<[_]>::into_vec(::alloc::boxed::box_new([params, arrow, body]))vec![params, arrow, body]);
2341 let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
2342 Box::new(DelimArgs { dspan, delim: Delimiter::Brace, tokens })
2343 } else {
2344 self.unexpected_any()?
2345 };
2346
2347 self.psess.gated_spans.gate(sym::decl_macro, lo.to(self.prev_token.span));
2348 Ok(ItemKind::MacroDef(
2349 ident,
2350 ast::MacroDef { body, macro_rules: false, eii_declaration: None },
2351 ))
2352 }
2353
2354 fn is_macro_rules_item(&mut self) -> IsMacroRulesItem {
2356 if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::MacroRules,
token_type: crate::parser::token_type::TokenType::KwMacroRules,
}exp!(MacroRules)) {
2357 let macro_rules_span = self.token.span;
2358
2359 if self.look_ahead(1, |t| *t == token::Bang) && self.look_ahead(2, |t| t.is_ident()) {
2360 return IsMacroRulesItem::Yes { has_bang: true };
2361 } else if self.look_ahead(1, |t| t.is_ident()) {
2362 self.dcx().emit_err(errors::MacroRulesMissingBang {
2364 span: macro_rules_span,
2365 hi: macro_rules_span.shrink_to_hi(),
2366 });
2367
2368 return IsMacroRulesItem::Yes { has_bang: false };
2369 }
2370 }
2371
2372 IsMacroRulesItem::No
2373 }
2374
2375 fn parse_item_macro_rules(
2377 &mut self,
2378 vis: &Visibility,
2379 has_bang: bool,
2380 ) -> PResult<'a, ItemKind> {
2381 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::MacroRules,
token_type: crate::parser::token_type::TokenType::KwMacroRules,
}exp!(MacroRules))?; if has_bang {
2384 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Bang,
token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang))?; }
2386 let ident = self.parse_ident()?;
2387
2388 if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Bang,
token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang)) {
2389 let span = self.prev_token.span;
2391 self.dcx().emit_err(errors::MacroNameRemoveBang { span });
2392 }
2393
2394 let body = self.parse_delim_args()?;
2395 self.eat_semi_for_macro_if_needed(&body);
2396 self.complain_if_pub_macro(vis, true);
2397
2398 Ok(ItemKind::MacroDef(
2399 ident,
2400 ast::MacroDef { body, macro_rules: true, eii_declaration: None },
2401 ))
2402 }
2403
2404 fn complain_if_pub_macro(&self, vis: &Visibility, macro_rules: bool) {
2407 if let VisibilityKind::Inherited = vis.kind {
2408 return;
2409 }
2410
2411 let vstr = pprust::vis_to_string(vis);
2412 let vstr = vstr.trim_end();
2413 if macro_rules {
2414 self.dcx().emit_err(errors::MacroRulesVisibility { span: vis.span, vis: vstr });
2415 } else {
2416 self.dcx().emit_err(errors::MacroInvocationVisibility { span: vis.span, vis: vstr });
2417 }
2418 }
2419
2420 fn eat_semi_for_macro_if_needed(&mut self, args: &DelimArgs) {
2421 if args.need_semicolon() && !self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
2422 self.report_invalid_macro_expansion_item(args);
2423 }
2424 }
2425
2426 fn report_invalid_macro_expansion_item(&self, args: &DelimArgs) {
2427 let span = args.dspan.entire();
2428 let mut err = self.dcx().struct_span_err(
2429 span,
2430 "macros that expand to items must be delimited with braces or followed by a semicolon",
2431 );
2432 if !span.from_expansion() {
2435 let DelimSpan { open, close } = args.dspan;
2436 err.multipart_suggestion(
2437 "change the delimiters to curly braces",
2438 <[_]>::into_vec(::alloc::boxed::box_new([(open, "{".to_string()),
(close, '}'.to_string())]))vec![(open, "{".to_string()), (close, '}'.to_string())],
2439 Applicability::MaybeIncorrect,
2440 );
2441 err.span_suggestion(
2442 span.with_neighbor(self.token.span).shrink_to_hi(),
2443 "add a semicolon",
2444 ';',
2445 Applicability::MaybeIncorrect,
2446 );
2447 }
2448 err.emit();
2449 }
2450
2451 fn recover_nested_adt_item(&mut self, keyword: Symbol) -> PResult<'a, bool> {
2454 if (self.token.is_keyword(kw::Enum)
2455 || self.token.is_keyword(kw::Struct)
2456 || self.token.is_keyword(kw::Union))
2457 && self.look_ahead(1, |t| t.is_ident())
2458 {
2459 let kw_token = self.token;
2460 let kw_str = pprust::token_to_string(&kw_token);
2461 let item = self.parse_item(
2462 ForceCollect::No,
2463 AllowConstBlockItems::DoesNotMatter, )?;
2465 let mut item = item.unwrap().span;
2466 if self.token == token::Comma {
2467 item = item.to(self.token.span);
2468 }
2469 self.dcx().emit_err(errors::NestedAdt {
2470 span: kw_token.span,
2471 item,
2472 kw_str,
2473 keyword: keyword.as_str(),
2474 });
2475 return Ok(false);
2477 }
2478 Ok(true)
2479 }
2480}
2481
2482type ReqName = fn(Edition, IsDotDotDot) -> bool;
2491
2492#[derive(#[automatically_derived]
impl ::core::marker::Copy for IsDotDotDot { }Copy, #[automatically_derived]
impl ::core::clone::Clone for IsDotDotDot {
#[inline]
fn clone(&self) -> IsDotDotDot { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for IsDotDotDot {
#[inline]
fn eq(&self, other: &IsDotDotDot) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
2493pub(crate) enum IsDotDotDot {
2494 Yes,
2495 No,
2496}
2497
2498#[derive(#[automatically_derived]
impl ::core::clone::Clone for FnParseMode {
#[inline]
fn clone(&self) -> FnParseMode {
let _: ::core::clone::AssertParamIsClone<ReqName>;
let _: ::core::clone::AssertParamIsClone<FnContext>;
let _: ::core::clone::AssertParamIsClone<bool>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for FnParseMode { }Copy)]
2506pub(crate) struct FnParseMode {
2507 pub(super) req_name: ReqName,
2533 pub(super) context: FnContext,
2536 pub(super) req_body: bool,
2555}
2556
2557#[derive(#[automatically_derived]
impl ::core::clone::Clone for FnContext {
#[inline]
fn clone(&self) -> FnContext { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for FnContext { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for FnContext {
#[inline]
fn eq(&self, other: &FnContext) -> 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 FnContext {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) {}
}Eq)]
2560pub(crate) enum FnContext {
2561 Free,
2563 Trait,
2565 Impl,
2567}
2568
2569impl<'a> Parser<'a> {
2571 fn parse_fn(
2573 &mut self,
2574 attrs: &mut AttrVec,
2575 fn_parse_mode: FnParseMode,
2576 sig_lo: Span,
2577 vis: &Visibility,
2578 case: Case,
2579 ) -> PResult<'a, (Ident, FnSig, Generics, Option<Box<FnContract>>, Option<Box<Block>>)> {
2580 let fn_span = self.token.span;
2581 let header = self.parse_fn_front_matter(vis, case, FrontMatterParsingMode::Function)?; let ident = self.parse_ident()?; let mut generics = self.parse_generics()?; let decl = match self.parse_fn_decl(&fn_parse_mode, AllowPlus::Yes, RecoverReturnSign::Yes)
2585 {
2586 Ok(decl) => decl,
2587 Err(old_err) => {
2588 if self.token.is_keyword(kw::For) {
2590 old_err.cancel();
2591 return Err(self.dcx().create_err(errors::FnTypoWithImpl { fn_span }));
2592 } else {
2593 return Err(old_err);
2594 }
2595 }
2596 };
2597
2598 let fn_params_end = self.prev_token.span.shrink_to_hi();
2601
2602 let contract = self.parse_contract()?;
2603
2604 generics.where_clause = self.parse_where_clause()?; let fn_params_end =
2608 if generics.where_clause.has_where_token { Some(fn_params_end) } else { None };
2609
2610 let mut sig_hi = self.prev_token.span;
2611 let body =
2613 self.parse_fn_body(attrs, &ident, &mut sig_hi, fn_parse_mode.req_body, fn_params_end)?;
2614 let fn_sig_span = sig_lo.to(sig_hi);
2615 Ok((ident, FnSig { header, decl, span: fn_sig_span }, generics, contract, body))
2616 }
2617
2618 fn error_fn_body_not_found(
2620 &mut self,
2621 ident_span: Span,
2622 req_body: bool,
2623 fn_params_end: Option<Span>,
2624 ) -> PResult<'a, ErrorGuaranteed> {
2625 let expected: &[_] =
2626 if req_body { &[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)] } else { &[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)] };
2627 match self.expected_one_of_not_found(&[], expected) {
2628 Ok(error_guaranteed) => Ok(error_guaranteed),
2629 Err(mut err) => {
2630 if self.token == token::CloseBrace {
2631 err.span_label(ident_span, "while parsing this `fn`");
2634 Ok(err.emit())
2635 } else if self.token == token::RArrow
2636 && let Some(fn_params_end) = fn_params_end
2637 {
2638 let fn_trait_span =
2644 [sym::FnOnce, sym::FnMut, sym::Fn].into_iter().find_map(|symbol| {
2645 if self.prev_token.is_ident_named(symbol) {
2646 Some(self.prev_token.span)
2647 } else {
2648 None
2649 }
2650 });
2651
2652 let arrow_span = self.token.span;
2657 let ty_span = match self.parse_ret_ty(
2658 AllowPlus::Yes,
2659 RecoverQPath::Yes,
2660 RecoverReturnSign::Yes,
2661 ) {
2662 Ok(ty_span) => ty_span.span().shrink_to_hi(),
2663 Err(parse_error) => {
2664 parse_error.cancel();
2665 return Err(err);
2666 }
2667 };
2668 let ret_ty_span = arrow_span.to(ty_span);
2669
2670 if let Some(fn_trait_span) = fn_trait_span {
2671 err.subdiagnostic(errors::FnTraitMissingParen { span: fn_trait_span });
2674 } else if let Ok(snippet) = self.psess.source_map().span_to_snippet(ret_ty_span)
2675 {
2676 err.primary_message(
2680 "return type should be specified after the function parameters",
2681 );
2682 err.subdiagnostic(errors::MisplacedReturnType {
2683 fn_params_end,
2684 snippet,
2685 ret_ty_span,
2686 });
2687 }
2688 Err(err)
2689 } else {
2690 Err(err)
2691 }
2692 }
2693 }
2694 }
2695
2696 fn parse_fn_body(
2700 &mut self,
2701 attrs: &mut AttrVec,
2702 ident: &Ident,
2703 sig_hi: &mut Span,
2704 req_body: bool,
2705 fn_params_end: Option<Span>,
2706 ) -> PResult<'a, Option<Box<Block>>> {
2707 let has_semi = if req_body {
2708 self.token == TokenKind::Semi
2709 } else {
2710 self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi))
2712 };
2713 let (inner_attrs, body) = if has_semi {
2714 self.expect_semi()?;
2716 *sig_hi = self.prev_token.span;
2717 (AttrVec::new(), None)
2718 } else if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) || self.token.is_metavar_block() {
2719 self.parse_block_common(self.token.span, BlockCheckMode::Default, None)
2720 .map(|(attrs, body)| (attrs, Some(body)))?
2721 } else if self.token == token::Eq {
2722 self.bump(); let eq_sp = self.prev_token.span;
2725 let _ = self.parse_expr()?;
2726 self.expect_semi()?; let span = eq_sp.to(self.prev_token.span);
2728 let guar = self.dcx().emit_err(errors::FunctionBodyEqualsExpr {
2729 span,
2730 sugg: errors::FunctionBodyEqualsExprSugg { eq: eq_sp, semi: self.prev_token.span },
2731 });
2732 (AttrVec::new(), Some(self.mk_block_err(span, guar)))
2733 } else {
2734 self.error_fn_body_not_found(ident.span, req_body, fn_params_end)?;
2735 (AttrVec::new(), None)
2736 };
2737 attrs.extend(inner_attrs);
2738 Ok(body)
2739 }
2740
2741 fn check_impl_frontmatter(&mut self, look_ahead: usize) -> bool {
2742 const ALL_QUALS: &[Symbol] = &[kw::Const, kw::Unsafe];
2743 if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Impl,
token_type: crate::parser::token_type::TokenType::KwImpl,
}exp!(Impl)) {
2746 return true;
2747 }
2748 let mut i = 0;
2749 while i < ALL_QUALS.len() {
2750 let action = self.look_ahead(i + look_ahead, |token| {
2751 if token.is_keyword(kw::Impl) {
2752 return Some(true);
2753 }
2754 if ALL_QUALS.iter().any(|&qual| token.is_keyword(qual)) {
2755 return None;
2757 }
2758 Some(false)
2759 });
2760 if let Some(ret) = action {
2761 return ret;
2762 }
2763 i += 1;
2764 }
2765
2766 self.is_keyword_ahead(i, &[kw::Impl])
2767 }
2768
2769 pub(super) fn check_fn_front_matter(&mut self, check_pub: bool, case: Case) -> bool {
2774 const ALL_QUALS: &[ExpKeywordPair] = &[
2775 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Pub,
token_type: crate::parser::token_type::TokenType::KwPub,
}exp!(Pub),
2776 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Gen,
token_type: crate::parser::token_type::TokenType::KwGen,
}exp!(Gen),
2777 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const),
2778 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Async,
token_type: crate::parser::token_type::TokenType::KwAsync,
}exp!(Async),
2779 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe),
2780 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Safe,
token_type: crate::parser::token_type::TokenType::KwSafe,
}exp!(Safe),
2781 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Extern,
token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern),
2782 ];
2783
2784 let quals: &[_] = if check_pub {
2789 ALL_QUALS
2790 } else {
2791 &[crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Gen,
token_type: crate::parser::token_type::TokenType::KwGen,
}exp!(Gen), crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const), crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Async,
token_type: crate::parser::token_type::TokenType::KwAsync,
}exp!(Async), crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe), crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Safe,
token_type: crate::parser::token_type::TokenType::KwSafe,
}exp!(Safe), crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Extern,
token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern)]
2792 };
2793 self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Fn,
token_type: crate::parser::token_type::TokenType::KwFn,
}exp!(Fn), case) || quals.iter().any(|&exp| self.check_keyword_case(exp, case))
2796 && self.look_ahead(1, |t| {
2797 t.is_keyword_case(kw::Fn, case)
2799 || (
2801 (
2802 t.is_non_raw_ident_where(|i|
2803 quals.iter().any(|exp| exp.kw == i.name)
2804 && i.is_reserved()
2806 )
2807 || case == Case::Insensitive
2808 && t.is_non_raw_ident_where(|i| quals.iter().any(|exp| {
2809 exp.kw.as_str() == i.name.as_str().to_lowercase()
2810 }))
2811 )
2812 && !self.is_unsafe_foreign_mod()
2814 && !self.is_async_gen_block()
2816 && !self.is_keyword_ahead(2, &[kw::Auto, kw::Trait])
2818 )
2819 })
2820 || self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Extern,
token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern), case)
2822 && self.look_ahead(1, |t| t.can_begin_string_literal())
2826 && (self.tree_look_ahead(2, |tt| {
2827 match tt {
2828 TokenTree::Token(t, _) => t.is_keyword_case(kw::Fn, case),
2829 TokenTree::Delimited(..) => false,
2830 }
2831 }) == Some(true) ||
2832 (self.may_recover()
2835 && self.tree_look_ahead(2, |tt| {
2836 match tt {
2837 TokenTree::Token(t, _) =>
2838 ALL_QUALS.iter().any(|exp| {
2839 t.is_keyword(exp.kw)
2840 }),
2841 TokenTree::Delimited(..) => false,
2842 }
2843 }) == Some(true)
2844 && self.tree_look_ahead(3, |tt| {
2845 match tt {
2846 TokenTree::Token(t, _) => t.is_keyword_case(kw::Fn, case),
2847 TokenTree::Delimited(..) => false,
2848 }
2849 }) == Some(true)
2850 )
2851 )
2852 }
2853
2854 pub(super) fn parse_fn_front_matter(
2869 &mut self,
2870 orig_vis: &Visibility,
2871 case: Case,
2872 parsing_mode: FrontMatterParsingMode,
2873 ) -> PResult<'a, FnHeader> {
2874 let sp_start = self.token.span;
2875 let constness = self.parse_constness(case);
2876 if parsing_mode == FrontMatterParsingMode::FunctionPtrType
2877 && let Const::Yes(const_span) = constness
2878 {
2879 self.dcx().emit_err(FnPointerCannotBeConst {
2880 span: const_span,
2881 suggestion: const_span.until(self.token.span),
2882 });
2883 }
2884
2885 let async_start_sp = self.token.span;
2886 let coroutine_kind = self.parse_coroutine_kind(case);
2887 if parsing_mode == FrontMatterParsingMode::FunctionPtrType
2888 && let Some(ast::CoroutineKind::Async { span: async_span, .. }) = coroutine_kind
2889 {
2890 self.dcx().emit_err(FnPointerCannotBeAsync {
2891 span: async_span,
2892 suggestion: async_span.until(self.token.span),
2893 });
2894 }
2895 let unsafe_start_sp = self.token.span;
2898 let safety = self.parse_safety(case);
2899
2900 let ext_start_sp = self.token.span;
2901 let ext = self.parse_extern(case);
2902
2903 if let Some(CoroutineKind::Async { span, .. }) = coroutine_kind {
2904 if span.is_rust_2015() {
2905 self.dcx().emit_err(errors::AsyncFnIn2015 {
2906 span,
2907 help: errors::HelpUseLatestEdition::new(),
2908 });
2909 }
2910 }
2911
2912 match coroutine_kind {
2913 Some(CoroutineKind::Gen { span, .. }) | Some(CoroutineKind::AsyncGen { span, .. }) => {
2914 self.psess.gated_spans.gate(sym::gen_blocks, span);
2915 }
2916 Some(CoroutineKind::Async { .. }) | None => {}
2917 }
2918
2919 if !self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Fn,
token_type: crate::parser::token_type::TokenType::KwFn,
}exp!(Fn), case) {
2920 match self.expect_one_of(&[], &[]) {
2924 Ok(Recovered::Yes(_)) => {}
2925 Ok(Recovered::No) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
2926 Err(mut err) => {
2927 enum WrongKw {
2929 Duplicated(Span),
2930 Misplaced(Span),
2931 MisplacedDisallowedQualifier,
2936 }
2937
2938 let mut recover_constness = constness;
2940 let mut recover_coroutine_kind = coroutine_kind;
2941 let mut recover_safety = safety;
2942 let wrong_kw = if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const)) {
2945 match constness {
2946 Const::Yes(sp) => Some(WrongKw::Duplicated(sp)),
2947 Const::No => {
2948 recover_constness = Const::Yes(self.token.span);
2949 match parsing_mode {
2950 FrontMatterParsingMode::Function => {
2951 Some(WrongKw::Misplaced(async_start_sp))
2952 }
2953 FrontMatterParsingMode::FunctionPtrType => {
2954 self.dcx().emit_err(FnPointerCannotBeConst {
2955 span: self.token.span,
2956 suggestion: self
2957 .token
2958 .span
2959 .with_lo(self.prev_token.span.hi()),
2960 });
2961 Some(WrongKw::MisplacedDisallowedQualifier)
2962 }
2963 }
2964 }
2965 }
2966 } else if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Async,
token_type: crate::parser::token_type::TokenType::KwAsync,
}exp!(Async)) {
2967 match coroutine_kind {
2968 Some(CoroutineKind::Async { span, .. }) => {
2969 Some(WrongKw::Duplicated(span))
2970 }
2971 Some(CoroutineKind::AsyncGen { span, .. }) => {
2972 Some(WrongKw::Duplicated(span))
2973 }
2974 Some(CoroutineKind::Gen { .. }) => {
2975 recover_coroutine_kind = Some(CoroutineKind::AsyncGen {
2976 span: self.token.span,
2977 closure_id: DUMMY_NODE_ID,
2978 return_impl_trait_id: DUMMY_NODE_ID,
2979 });
2980 Some(WrongKw::Misplaced(unsafe_start_sp))
2982 }
2983 None => {
2984 recover_coroutine_kind = Some(CoroutineKind::Async {
2985 span: self.token.span,
2986 closure_id: DUMMY_NODE_ID,
2987 return_impl_trait_id: DUMMY_NODE_ID,
2988 });
2989 match parsing_mode {
2990 FrontMatterParsingMode::Function => {
2991 Some(WrongKw::Misplaced(async_start_sp))
2992 }
2993 FrontMatterParsingMode::FunctionPtrType => {
2994 self.dcx().emit_err(FnPointerCannotBeAsync {
2995 span: self.token.span,
2996 suggestion: self
2997 .token
2998 .span
2999 .with_lo(self.prev_token.span.hi()),
3000 });
3001 Some(WrongKw::MisplacedDisallowedQualifier)
3002 }
3003 }
3004 }
3005 }
3006 } else if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe)) {
3007 match safety {
3008 Safety::Unsafe(sp) => Some(WrongKw::Duplicated(sp)),
3009 Safety::Safe(sp) => {
3010 recover_safety = Safety::Unsafe(self.token.span);
3011 Some(WrongKw::Misplaced(sp))
3012 }
3013 Safety::Default => {
3014 recover_safety = Safety::Unsafe(self.token.span);
3015 Some(WrongKw::Misplaced(ext_start_sp))
3016 }
3017 }
3018 } else if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Safe,
token_type: crate::parser::token_type::TokenType::KwSafe,
}exp!(Safe)) {
3019 match safety {
3020 Safety::Safe(sp) => Some(WrongKw::Duplicated(sp)),
3021 Safety::Unsafe(sp) => {
3022 recover_safety = Safety::Safe(self.token.span);
3023 Some(WrongKw::Misplaced(sp))
3024 }
3025 Safety::Default => {
3026 recover_safety = Safety::Safe(self.token.span);
3027 Some(WrongKw::Misplaced(ext_start_sp))
3028 }
3029 }
3030 } else {
3031 None
3032 };
3033
3034 if let Some(WrongKw::Duplicated(original_sp)) = wrong_kw {
3036 let original_kw = self
3037 .span_to_snippet(original_sp)
3038 .expect("Span extracted directly from keyword should always work");
3039
3040 err.span_suggestion(
3041 self.token_uninterpolated_span(),
3042 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` already used earlier, remove this one",
original_kw))
})format!("`{original_kw}` already used earlier, remove this one"),
3043 "",
3044 Applicability::MachineApplicable,
3045 )
3046 .span_note(original_sp, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` first seen here",
original_kw))
})format!("`{original_kw}` first seen here"));
3047 }
3048 else if let Some(WrongKw::Misplaced(correct_pos_sp)) = wrong_kw {
3050 let correct_pos_sp = correct_pos_sp.to(self.prev_token.span);
3051 if let Ok(current_qual) = self.span_to_snippet(correct_pos_sp) {
3052 let misplaced_qual_sp = self.token_uninterpolated_span();
3053 let misplaced_qual = self.span_to_snippet(misplaced_qual_sp).unwrap();
3054
3055 err.span_suggestion(
3056 correct_pos_sp.to(misplaced_qual_sp),
3057 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` must come before `{1}`",
misplaced_qual, current_qual))
})format!("`{misplaced_qual}` must come before `{current_qual}`"),
3058 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} {1}", misplaced_qual,
current_qual))
})format!("{misplaced_qual} {current_qual}"),
3059 Applicability::MachineApplicable,
3060 ).note("keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern`");
3061 }
3062 }
3063 else if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Pub,
token_type: crate::parser::token_type::TokenType::KwPub,
}exp!(Pub)) {
3065 let sp = sp_start.to(self.prev_token.span);
3066 if let Ok(snippet) = self.span_to_snippet(sp) {
3067 let current_vis = match self.parse_visibility(FollowedByType::No) {
3068 Ok(v) => v,
3069 Err(d) => {
3070 d.cancel();
3071 return Err(err);
3072 }
3073 };
3074 let vs = pprust::vis_to_string(¤t_vis);
3075 let vs = vs.trim_end();
3076
3077 if #[allow(non_exhaustive_omitted_patterns)] match orig_vis.kind {
VisibilityKind::Inherited => true,
_ => false,
}matches!(orig_vis.kind, VisibilityKind::Inherited) {
3079 err.span_suggestion(
3080 sp_start.to(self.prev_token.span),
3081 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("visibility `{0}` must come before `{1}`",
vs, snippet))
})format!("visibility `{vs}` must come before `{snippet}`"),
3082 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} {1}", vs, snippet))
})format!("{vs} {snippet}"),
3083 Applicability::MachineApplicable,
3084 );
3085 }
3086 else {
3088 err.span_suggestion(
3089 current_vis.span,
3090 "there is already a visibility modifier, remove one",
3091 "",
3092 Applicability::MachineApplicable,
3093 )
3094 .span_note(orig_vis.span, "explicit visibility first seen here");
3095 }
3096 }
3097 }
3098
3099 if let Some(wrong_kw) = wrong_kw
3102 && self.may_recover()
3103 && self.look_ahead(1, |tok| tok.is_keyword_case(kw::Fn, case))
3104 {
3105 self.bump();
3107 self.bump();
3108 if #[allow(non_exhaustive_omitted_patterns)] match wrong_kw {
WrongKw::MisplacedDisallowedQualifier => true,
_ => false,
}matches!(wrong_kw, WrongKw::MisplacedDisallowedQualifier) {
3111 err.cancel();
3112 } else {
3113 err.emit();
3114 }
3115 return Ok(FnHeader {
3116 constness: recover_constness,
3117 safety: recover_safety,
3118 coroutine_kind: recover_coroutine_kind,
3119 ext,
3120 });
3121 }
3122
3123 return Err(err);
3124 }
3125 }
3126 }
3127
3128 Ok(FnHeader { constness, safety, coroutine_kind, ext })
3129 }
3130
3131 pub(super) fn parse_fn_decl(
3133 &mut self,
3134 fn_parse_mode: &FnParseMode,
3135 ret_allow_plus: AllowPlus,
3136 recover_return_sign: RecoverReturnSign,
3137 ) -> PResult<'a, Box<FnDecl>> {
3138 Ok(Box::new(FnDecl {
3139 inputs: self.parse_fn_params(fn_parse_mode)?,
3140 output: self.parse_ret_ty(ret_allow_plus, RecoverQPath::Yes, recover_return_sign)?,
3141 }))
3142 }
3143
3144 pub(super) fn parse_fn_params(
3146 &mut self,
3147 fn_parse_mode: &FnParseMode,
3148 ) -> PResult<'a, ThinVec<Param>> {
3149 let mut first_param = true;
3150 if self.token != TokenKind::OpenParen
3152 && !self.token.is_keyword(kw::For)
3154 {
3155 self.dcx()
3157 .emit_err(errors::MissingFnParams { span: self.prev_token.span.shrink_to_hi() });
3158 return Ok(ThinVec::new());
3159 }
3160
3161 let (mut params, _) = self.parse_paren_comma_seq(|p| {
3162 p.recover_vcs_conflict_marker();
3163 let snapshot = p.create_snapshot_for_diagnostic();
3164 let param = p.parse_param_general(fn_parse_mode, first_param, true).or_else(|e| {
3165 let guar = e.emit();
3166 let lo = if let TokenKind::OpenParen = p.prev_token.kind {
3170 p.prev_token.span.shrink_to_hi()
3171 } else {
3172 p.prev_token.span
3173 };
3174 p.restore_snapshot(snapshot);
3175 p.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseParen,
token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen)]);
3177 Ok(dummy_arg(Ident::new(sym::dummy, lo.to(p.prev_token.span)), guar))
3179 });
3180 first_param = false;
3182 param
3183 })?;
3184 self.deduplicate_recovered_params_names(&mut params);
3186 Ok(params)
3187 }
3188
3189 pub(super) fn parse_param_general(
3194 &mut self,
3195 fn_parse_mode: &FnParseMode,
3196 first_param: bool,
3197 recover_arg_parse: bool,
3198 ) -> PResult<'a, Param> {
3199 let lo = self.token.span;
3200 let attrs = self.parse_outer_attributes()?;
3201 self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
3202 if let Some(mut param) = this.parse_self_param()? {
3204 param.attrs = attrs;
3205 let res = if first_param { Ok(param) } else { this.recover_bad_self_param(param) };
3206 return Ok((res?, Trailing::No, UsePreAttrPos::No));
3207 }
3208
3209 let is_dot_dot_dot = if this.token.kind == token::DotDotDot {
3210 IsDotDotDot::Yes
3211 } else {
3212 IsDotDotDot::No
3213 };
3214 let is_name_required = (fn_parse_mode.req_name)(
3215 this.token.span.with_neighbor(this.prev_token.span).edition(),
3216 is_dot_dot_dot,
3217 );
3218 let is_name_required = if is_name_required && is_dot_dot_dot == IsDotDotDot::Yes {
3219 this.psess.buffer_lint(
3220 VARARGS_WITHOUT_PATTERN,
3221 this.token.span,
3222 ast::CRATE_NODE_ID,
3223 errors::VarargsWithoutPattern { span: this.token.span },
3224 );
3225 false
3226 } else {
3227 is_name_required
3228 };
3229 let (pat, ty) = if is_name_required || this.is_named_param() {
3230 {
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/item.rs:3230",
"rustc_parse::parser::item", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/parser/item.rs"),
::tracing_core::__macro_support::Option::Some(3230u32),
::tracing_core::__macro_support::Option::Some("rustc_parse::parser::item"),
::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_param_general parse_pat (is_name_required:{0})",
is_name_required) as &dyn Value))])
});
} else { ; }
};debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);
3231 let (pat, colon) = this.parse_fn_param_pat_colon()?;
3232 if !colon {
3233 let mut err = this.unexpected().unwrap_err();
3234 return if let Some(ident) = this.parameter_without_type(
3235 &mut err,
3236 pat,
3237 is_name_required,
3238 first_param,
3239 fn_parse_mode,
3240 ) {
3241 let guar = err.emit();
3242 Ok((dummy_arg(ident, guar), Trailing::No, UsePreAttrPos::No))
3243 } else {
3244 Err(err)
3245 };
3246 }
3247
3248 this.eat_incorrect_doc_comment_for_param_type();
3249 (pat, this.parse_ty_for_param()?)
3250 } else {
3251 {
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/item.rs:3251",
"rustc_parse::parser::item", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/parser/item.rs"),
::tracing_core::__macro_support::Option::Some(3251u32),
::tracing_core::__macro_support::Option::Some("rustc_parse::parser::item"),
::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_param_general ident_to_pat")
as &dyn Value))])
});
} else { ; }
};debug!("parse_param_general ident_to_pat");
3252 let parser_snapshot_before_ty = this.create_snapshot_for_diagnostic();
3253 this.eat_incorrect_doc_comment_for_param_type();
3254 let mut ty = this.parse_ty_for_param();
3255
3256 if let Ok(t) = &ty {
3257 if let TyKind::Path(_, Path { segments, .. }) = &t.kind
3259 && let Some(segment) = segments.last()
3260 && let Some(guar) =
3261 this.check_trailing_angle_brackets(segment, &[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseParen,
token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen)])
3262 {
3263 return Ok((
3264 dummy_arg(segment.ident, guar),
3265 Trailing::No,
3266 UsePreAttrPos::No,
3267 ));
3268 }
3269
3270 if this.token != token::Comma && this.token != token::CloseParen {
3271 ty = this.unexpected_any();
3274 }
3275 }
3276 match ty {
3277 Ok(ty) => {
3278 let pat = this.mk_pat(ty.span, PatKind::Missing);
3279 (Box::new(pat), ty)
3280 }
3281 Err(err) if this.token == token::DotDotDot => return Err(err),
3283 Err(err) if this.unmatched_angle_bracket_count > 0 => return Err(err),
3284 Err(err) if recover_arg_parse => {
3285 err.cancel();
3287 this.restore_snapshot(parser_snapshot_before_ty);
3288 this.recover_arg_parse()?
3289 }
3290 Err(err) => return Err(err),
3291 }
3292 };
3293
3294 let span = lo.to(this.prev_token.span);
3295
3296 Ok((
3297 Param { attrs, id: ast::DUMMY_NODE_ID, is_placeholder: false, pat, span, ty },
3298 Trailing::No,
3299 UsePreAttrPos::No,
3300 ))
3301 })
3302 }
3303
3304 fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
3306 let expect_self_ident = |this: &mut Self| match this.token.ident() {
3308 Some((ident, IdentIsRaw::No)) => {
3309 this.bump();
3310 ident
3311 }
3312 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
3313 };
3314 let is_lifetime = |this: &Self, n| this.look_ahead(n, |t| t.is_lifetime());
3316 let is_isolated_self = |this: &Self, n| {
3318 this.is_keyword_ahead(n, &[kw::SelfLower])
3319 && this.look_ahead(n + 1, |t| t != &token::PathSep)
3320 };
3321 let is_isolated_pin_const_self = |this: &Self, n| {
3323 this.look_ahead(n, |token| token.is_ident_named(sym::pin))
3324 && this.is_keyword_ahead(n + 1, &[kw::Const])
3325 && is_isolated_self(this, n + 2)
3326 };
3327 let is_isolated_mut_self =
3329 |this: &Self, n| this.is_keyword_ahead(n, &[kw::Mut]) && is_isolated_self(this, n + 1);
3330 let is_isolated_pin_mut_self = |this: &Self, n| {
3332 this.look_ahead(n, |token| token.is_ident_named(sym::pin))
3333 && is_isolated_mut_self(this, n + 1)
3334 };
3335 let parse_self_possibly_typed = |this: &mut Self, m| {
3337 let eself_ident = expect_self_ident(this);
3338 let eself_hi = this.prev_token.span;
3339 let eself = if this.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)) {
3340 SelfKind::Explicit(this.parse_ty()?, m)
3341 } else {
3342 SelfKind::Value(m)
3343 };
3344 Ok((eself, eself_ident, eself_hi))
3345 };
3346 let expect_self_ident_not_typed =
3347 |this: &mut Self, modifier: &SelfKind, modifier_span: Span| {
3348 let eself_ident = expect_self_ident(this);
3349
3350 if this.may_recover() && this.eat_noexpect(&token::Colon) {
3352 let snap = this.create_snapshot_for_diagnostic();
3353 match this.parse_ty() {
3354 Ok(ty) => {
3355 this.dcx().emit_err(errors::IncorrectTypeOnSelf {
3356 span: ty.span,
3357 move_self_modifier: errors::MoveSelfModifier {
3358 removal_span: modifier_span,
3359 insertion_span: ty.span.shrink_to_lo(),
3360 modifier: modifier.to_ref_suggestion(),
3361 },
3362 });
3363 }
3364 Err(diag) => {
3365 diag.cancel();
3366 this.restore_snapshot(snap);
3367 }
3368 }
3369 }
3370 eself_ident
3371 };
3372 let recover_self_ptr = |this: &mut Self| {
3374 this.dcx().emit_err(errors::SelfArgumentPointer { span: this.token.span });
3375
3376 Ok((SelfKind::Value(Mutability::Not), expect_self_ident(this), this.prev_token.span))
3377 };
3378
3379 let eself_lo = self.token.span;
3383 let (eself, eself_ident, eself_hi) = match self.token.uninterpolate().kind {
3384 token::And => {
3385 let has_lifetime = is_lifetime(self, 1);
3386 let skip_lifetime_count = has_lifetime as usize;
3387 let eself = if is_isolated_self(self, skip_lifetime_count + 1) {
3388 self.bump(); let lifetime = has_lifetime.then(|| self.expect_lifetime());
3391 SelfKind::Region(lifetime, Mutability::Not)
3392 } else if is_isolated_mut_self(self, skip_lifetime_count + 1) {
3393 self.bump(); let lifetime = has_lifetime.then(|| self.expect_lifetime());
3396 self.bump(); SelfKind::Region(lifetime, Mutability::Mut)
3398 } else if is_isolated_pin_const_self(self, skip_lifetime_count + 1) {
3399 self.bump(); let lifetime = has_lifetime.then(|| self.expect_lifetime());
3402 self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
3403 self.bump(); self.bump(); SelfKind::Pinned(lifetime, Mutability::Not)
3406 } else if is_isolated_pin_mut_self(self, skip_lifetime_count + 1) {
3407 self.bump(); let lifetime = has_lifetime.then(|| self.expect_lifetime());
3410 self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
3411 self.bump(); self.bump(); SelfKind::Pinned(lifetime, Mutability::Mut)
3414 } else {
3415 return Ok(None);
3417 };
3418 let hi = self.token.span;
3419 let self_ident = expect_self_ident_not_typed(self, &eself, eself_lo.until(hi));
3420 (eself, self_ident, hi)
3421 }
3422 token::Star if is_isolated_self(self, 1) => {
3424 self.bump();
3425 recover_self_ptr(self)?
3426 }
3427 token::Star
3429 if self.look_ahead(1, |t| t.is_mutability()) && is_isolated_self(self, 2) =>
3430 {
3431 self.bump();
3432 self.bump();
3433 recover_self_ptr(self)?
3434 }
3435 token::Ident(..) if is_isolated_self(self, 0) => {
3437 parse_self_possibly_typed(self, Mutability::Not)?
3438 }
3439 token::Ident(..) if is_isolated_mut_self(self, 0) => {
3441 self.bump();
3442 parse_self_possibly_typed(self, Mutability::Mut)?
3443 }
3444 _ => return Ok(None),
3445 };
3446
3447 let eself = source_map::respan(eself_lo.to(eself_hi), eself);
3448 Ok(Some(Param::from_self(AttrVec::default(), eself, eself_ident)))
3449 }
3450
3451 fn is_named_param(&self) -> bool {
3452 let offset = match &self.token.kind {
3453 token::OpenInvisible(origin) => match origin {
3454 InvisibleOrigin::MetaVar(MetaVarKind::Pat(_)) => {
3455 return self.check_noexpect_past_close_delim(&token::Colon);
3456 }
3457 _ => 0,
3458 },
3459 token::And | token::AndAnd => 1,
3460 _ if self.token.is_keyword(kw::Mut) => 1,
3461 _ => 0,
3462 };
3463
3464 self.look_ahead(offset, |t| t.is_ident())
3465 && self.look_ahead(offset + 1, |t| t == &token::Colon)
3466 }
3467
3468 fn recover_self_param(&mut self) -> bool {
3469 #[allow(non_exhaustive_omitted_patterns)] match self.parse_outer_attributes().and_then(|_|
self.parse_self_param()).map_err(|e| e.cancel()) {
Ok(Some(_)) => true,
_ => false,
}matches!(
3470 self.parse_outer_attributes()
3471 .and_then(|_| self.parse_self_param())
3472 .map_err(|e| e.cancel()),
3473 Ok(Some(_))
3474 )
3475 }
3476}
3477
3478enum IsMacroRulesItem {
3479 Yes { has_bang: bool },
3480 No,
3481}
3482
3483#[derive(#[automatically_derived]
impl ::core::marker::Copy for FrontMatterParsingMode { }Copy, #[automatically_derived]
impl ::core::clone::Clone for FrontMatterParsingMode {
#[inline]
fn clone(&self) -> FrontMatterParsingMode { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for FrontMatterParsingMode {
#[inline]
fn eq(&self, other: &FrontMatterParsingMode) -> 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 FrontMatterParsingMode {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) {}
}Eq)]
3484pub(super) enum FrontMatterParsingMode {
3485 Function,
3487 FunctionPtrType,
3490}