Skip to main content

rustc_parse/parser/
item.rs

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    /// Parses a source module as a crate. This is the main entry point for the parser.
31    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    /// Parses a `mod <foo> { ... }` or `mod <foo>;` item.
37    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    /// Parses the contents of a module (inner attributes followed by module items).
53    /// We exit once we hit `term` which can be either
54    /// - EOF (for files)
55    /// - `}` for mod items
56    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        // There shouldn't be any stray semicolons before or after items.
67        // `parse_item` consumes the appropriate semicolons so any leftover is an error.
68        loop {
69            while self.maybe_consume_incorrect_semicolon(items.last().map(|x| &**x)) {} // Eat all bad semicolons
70            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            // At this point, we have failed to parse an item.
194            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    /// Error in-case `default` was parsed in an in-appropriate context.
210    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    /// Parses one of the items allowed by the flags.
221    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            // FUNCTION ITEM
239            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                // EXTERN CRATE
254                self.parse_item_extern_crate()?
255            } else {
256                // EXTERN BLOCK
257                self.parse_item_foreign_mod(attrs, Safety::Default)?
258            }
259        } else if self.is_unsafe_foreign_mod() {
260            // EXTERN BLOCK
261            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            // STATIC ITEM
266            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            // TRAIT ITEM
270            self.parse_item_trait(attrs, lo)?
271        } else if self.check_impl_frontmatter(0) {
272            // IMPL ITEM
273            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            // CONST BLOCK ITEM
279            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            // CONST ITEM
285            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            // MODULE ITEM
302            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                // TYPE CONST (mgca)
306                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                // Make sure this is only allowed if the feature gate is enabled.
310                // #![feature(mgca_type_const_syntax)]
311                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                // TYPE ITEM
322                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            // ENUM ITEM
326            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            // STRUCT ITEM
329            self.parse_item_struct()?
330        } else if self.is_kw_followed_by_ident(kw::Union) {
331            // UNION ITEM
332            self.bump(); // `union`
333            self.parse_item_union()?
334        } else if self.is_builtin() {
335            // BUILTIN# ITEM
336            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            // MACROS 2.0 ITEM
339            self.parse_item_decl_macro(lo)?
340        } else if let IsMacroRulesItem::Yes { has_bang } = self.is_macro_rules_item() {
341            // MACRO_RULES ITEM
342            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            // Recover wrong cased keywords
357            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            // MACRO INVOCATION ITEM
372            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    /// When parsing a statement, would the start of a path be an item?
415    pub(super) fn is_path_start_item(&mut self) -> bool {
416        self.is_kw_followed_by_ident(kw::Union) // no: `union::b`, yes: `union U { .. }`
417        || self.is_reuse_item().is_some() // yes: `reuse impl Trait for Struct { self.0 }`, yes: `reuse some_path::foo;`
418        || self.check_trait_front_matter() // no: `auto::b`, yes: `auto trait X { .. }`
419        || self.is_async_fn() // no(2015): `async::b`, yes: `async fn`
420        || #[allow(non_exhaustive_omitted_patterns)] match self.is_macro_rules_item() {
    IsMacroRulesItem::Yes { .. } => true,
    _ => false,
}matches!(self.is_macro_rules_item(), IsMacroRulesItem::Yes{..}) // no: `macro_rules::b`, yes: `macro_rules! mac`
421    }
422
423    fn is_reuse_item(&mut self) -> Option<ReuseKind> {
424        if !self.token.is_keyword(kw::Reuse) {
425            return None;
426        }
427
428        // no: `reuse ::path` for compatibility reasons with macro invocations
429        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    /// Are we sure this could not possibly be a macro invocation?
439    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    /// Recover on encountering a struct, enum, or method definition where the user
444    /// forgot to add the `struct`, `enum`, or `fn` keyword
445    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(); // `>`
467        }
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            // possible struct or enum definition where `struct` or `enum` was forgotten
471            if self.look_ahead(1, |t| *t == token::CloseBrace) {
472                // `S {}` could be unit enum or struct
473                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                // `S { f:` or `S { pub f:`
478                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            // possible function or tuple struct definition where `fn` or `struct` was forgotten
484            self.bump(); // `(`
485            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(); // `{`
492                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        // To be expanded
527        Ok(None)
528    }
529
530    /// Parses an item macro, e.g., `item!();`.
531    fn parse_item_macro(&mut self, vis: &Visibility) -> PResult<'a, MacCall> {
532        let path = self.parse_path(PathStyle::Mod)?; // `foo::bar`
533        self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Bang,
    token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang))?; // `!`
534        match self.parse_delim_args() {
535            // `( .. )` or `[ .. ]` (followed by `;`), or `{ .. }`.
536            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                // Maybe the user misspelled `macro_rules` (issue #91227)
544                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    /// Recover if we parsed attributes and expected an item but there was none.
561    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        // Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type.
593        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(); // `!`
595            ast::ImplPolarity::Negative(self.prev_token.span)
596        } else {
597            ast::ImplPolarity::Positive
598        }
599    }
600
601    /// Parses an implementation item.
602    ///
603    /// ```ignore (illustrative)
604    /// impl<'a, T> TYPE { /* impl items */ }
605    /// impl<'a, T> TRAIT for TYPE { /* impl items */ }
606    /// impl<'a, T> !TRAIT for TYPE { /* impl items */ }
607    /// impl<'a, T> const TRAIT for TYPE { /* impl items */ }
608    /// ```
609    ///
610    /// We actually parse slightly more relaxed grammar for better error reporting and recovery.
611    /// ```ebnf
612    /// "impl" GENERICS "const"? "!"? TYPE "for"? (TYPE | "..") ("where" PREDICATES)? "{" BODY "}"
613    /// "impl" GENERICS "const"? "!"? TYPE ("where" PREDICATES)? "{" BODY "}"
614    /// ```
615    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        // First, parse generic parameters if necessary.
626        let mut generics = if self.choose_generics_over_qpath(0) {
627            self.parse_generics()?
628        } else {
629            let mut generics = Generics::default();
630            // impl A for B {}
631            //    /\ this is where `generics.span` should point when there are no type params.
632            generics.span = self.prev_token.span.shrink_to_hi();
633            generics
634        };
635
636        if let Const::No = constness {
637            // FIXME(const_trait_impl): disallow `impl const Trait`
638            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        // Parse stray `impl async Trait`
646        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        // Parse both types and traits as a type, then reinterpret if necessary.
657        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        // If `for` is missing we try to recover.
669        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            // We need to report this error after `cfg` expansion for compatibility reasons
674            self.bump(); // `..`, do not add it to expected tokens
675
676            // AST validation later detects this `TyKind::Dummy` and emits an
677            // error. (#121072 will hopefully remove all this special handling
678            // of the obsolete `impl Trait for ..` and then this can go away.)
679            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                // impl Trait for Type
697                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                    // This notably includes paths passed through `ty` macro fragments (#46438).
704                    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                            // Suggest removing extra `impl` keyword:
711                            // `impl<T: Default> impl Default for Wrapper<T>`
712                            //                   ^^^^^
713                            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        // Recover `impl Ty;` instead of `impl Ty {}`
882        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                        // When the close delim is `)` in a case like the following, `token.kind`
901                        // is expected to be `token::CloseParen`, but the actual `token.kind` is
902                        // `token::CloseBrace`. This is because the `token.kind` of the close delim
903                        // is treated as the same as that of the open delim in
904                        // `TokenTreesReader::parse_token_tree`, even if the delimiters of them are
905                        // different. Therefore, `token.kind` should not be compared here.
906                        //
907                        // issue-60075.rs
908                        // ```
909                        // trait T {
910                        //     fn qux() -> Option<usize> {
911                        //         let _ = if true {
912                        //         });
913                        //          ^ this close delim
914                        //         Some(4)
915                        //     }
916                        // ```
917                        && 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                        // #105369, Detect spurious `;` before assoc fn body
924                        is_unnecessary_semicolon =
925                            self.token == token::OpenBrace && self.prev_token == token::Semi;
926                        semicolon_span = self.prev_token.span;
927                    }
928                    // We have to bail or we'll potentially never make progress.
929                    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    /// Recover on a doc comment before `}`.
975    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                // FIXME: merge with `DocCommentDoesNotDocumentAnything` (E0585)
979                {
    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    /// Parses defaultness (i.e., `default` or nothing).
999    fn parse_defaultness(&mut self) -> Defaultness {
1000        // We are interested in `default` followed by another identifier.
1001        // However, we must avoid keywords that occur as binary operators.
1002        // Currently, the only applicable keyword is `as` (`default as Ty`).
1003        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(); // `default`
1007            Defaultness::Default(self.prev_token_uninterpolated_span())
1008        } else {
1009            Defaultness::Final
1010        }
1011    }
1012
1013    /// Is this an `(const unsafe? auto?| unsafe auto? | auto) trait` item?
1014    fn check_trait_front_matter(&mut self) -> bool {
1015        // auto trait
1016        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            // unsafe auto trait
1018            || 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    /// Parses `unsafe? auto? trait Foo { ... }` or `trait Foo = Bar;`.
1024    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        // Parse optional `auto` prefix.
1031        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        // Parse optional colon and supertrait bounds.
1043        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            // It's a trait alias.
1050            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            // It's a normal trait.
1072            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    /// Parses associated items.
1108    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, // due to `AssocItemKind::try_from` below
1118            )?
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    /// Parses a `type` alias with the following grammar:
1149    /// ```ebnf
1150    /// TypeAlias = "type" Ident Generics (":" GenericBounds)? WhereClause ("=" Ty)? WhereClause ";" ;
1151    /// ```
1152    /// The `"type"` has already been eaten.
1153    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        // Parse optional colon and param bounds.
1158        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    /// Parses a `UseTree`.
1178    ///
1179    /// ```text
1180    /// USE_TREE = [`::`] `*` |
1181    ///            [`::`] `{` USE_TREE_LIST `}` |
1182    ///            PATH `::` `*` |
1183    ///            PATH `::` `{` USE_TREE_LIST `}` |
1184    ///            PATH [`as` IDENT]
1185    /// ```
1186    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                // `use *;` or `use ::*;` or `use {...};` or `use ::{...};`
1194                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                // `use path::*;` or `use path::{...};` or `use path;` or `use path as bar;`
1204                prefix = self.parse_path(PathStyle::Mod)?;
1205
1206                if self.eat_path_sep() {
1207                    self.parse_use_tree_glob_or_nested()?
1208                } else {
1209                    // Recover from using a colon as path separator.
1210                    while self.eat_noexpect(&token::Colon) {
1211                        self.dcx()
1212                            .emit_err(errors::SingleColonImportPath { span: self.prev_token.span });
1213
1214                        // We parse the rest of the path and append it to the original prefix.
1215                        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    /// Parses `*` or `{...}`.
1227    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    /// Parses a `UseTreeKind::Nested(list)`.
1240    ///
1241    /// ```text
1242    /// USE_TREE_LIST = ∅ | (USE_TREE `,`)* USE_TREE [`,`]
1243    /// ```
1244    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    /// Parses `extern crate` links.
1271    ///
1272    /// # Examples
1273    ///
1274    /// ```ignore (illustrative)
1275    /// extern crate foo;
1276    /// extern crate bar as foo;
1277    /// ```
1278    fn parse_item_extern_crate(&mut self) -> PResult<'a, ItemKind> {
1279        // Accept `extern crate name-like-this` for better diagnostics
1280        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        // Accept `extern crate name-like-this` for better diagnostics.
1303        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    /// Parses `extern` for foreign ABIs modules.
1325    ///
1326    /// `extern` is expected to have been consumed before calling this method.
1327    ///
1328    /// # Examples
1329    ///
1330    /// ```ignore (only-for-syntax-highlight)
1331    /// extern "C" {}
1332    /// extern {}
1333    /// ```
1334    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(); // ABI?
1341        // FIXME: This recovery should be tested better.
1342        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    /// Parses a foreign item (one in an `extern { ... }` block).
1359    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, // due to `ForeignItemKind::try_from` below
1373            )?
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        // FIXME(#100717): needs variant for each `ItemKind` (instead of using `ItemKind::descr()`)
1409        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            // Check if this could be a closure.
1422            self.look_ahead(1, |token| {
1423                // Move or Async here would be an error but still we're parsing a closure
1424                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        // Look for `unsafe`.
1436        if !self.token.is_keyword(kw::Unsafe) {
1437            return false;
1438        }
1439        // Look for `extern`.
1440        if !self.is_keyword_ahead(1, &[kw::Extern]) {
1441            return false;
1442        }
1443
1444        // Look for the optional ABI string literal.
1445        let n = if self.look_ahead(2, |t| t.can_begin_string_literal()) { 3 } else { 2 };
1446
1447        // Look for the `{`. Use `tree_look_ahead` because the ABI (if present)
1448        // might be a metavariable i.e. an invisible-delimited sequence, and
1449        // `tree_look_ahead` will consider that a single element when looking
1450        // ahead.
1451        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            // Check if this could be a closure.
1458            !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            // `$qual static`
1466            (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    /// Recover on `const mut` with `const` already eaten.
1481    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    /// Parse a static item with the prefix `"static" "mut"?` already parsed and stored in
1501    /// `mutability`.
1502    ///
1503    /// ```ebnf
1504    /// Static = "static" "mut"? $ident ":" $ty (= $expr)? ";" ;
1505    /// ```
1506    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        // Parse the type of a static item. That is, the `":" $ty` fragment.
1519        // FIXME: This could maybe benefit from `.may_recover()`?
1520        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            // If there wasn't a `:` or the colon was followed by a `=` or `;`, recover a missing
1523            // type.
1524            (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    /// Parse a constant item with the prefix `"const"` already parsed.
1536    ///
1537    /// If `const_arg` is true, any expression assigned to the const will be parsed
1538    /// as a const_arg instead of a body expression.
1539    ///
1540    /// ```ebnf
1541    /// Const = "const" ($ident | "_") Generics ":" $ty (= $expr)? WhereClause ";" ;
1542    /// ```
1543    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        // Check the span for emptiness instead of the list of parameters in order to correctly
1552        // recognize and subsequently flag empty parameter lists (`<>`) as unstable.
1553        if !generics.span.is_empty() {
1554            self.psess.gated_spans.gate(sym::generic_const_items, generics.span);
1555        }
1556
1557        // Parse the type of a constant item. That is, the `":" $ty` fragment.
1558        // FIXME: This could maybe benefit from `.may_recover()`?
1559        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            // If there wasn't a `:` or the colon was followed by a `=`, `;` or `where`, recover a missing type.
1565            (colon, _) => self.recover_missing_global_item_type(colon, None),
1566        };
1567
1568        // Proactively parse a where-clause to be able to provide a good error message in case we
1569        // encounter the item body following it.
1570        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        // Provide a nice error message if the user placed a where-clause before the item body.
1587        // Users may be tempted to write such code if they are still used to the deprecated
1588        // where-clause location on type aliases and associated types. See also #89122.
1589        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                    // FIXME(generic_const_items): Provide a structured suggestion to merge the first
1606                    // where-clause into the second one.
1607                    None
1608                },
1609            });
1610        }
1611
1612        // Merge the predicates of both where-clauses since either one can be relevant.
1613        // If we didn't parse a body (which is valid for associated consts in traits) and we were
1614        // allowed to recover, `before_where_clause` contains the predicates, otherwise they are
1615        // in `after_where_clause`. Further, both of them might contain predicates iff two
1616        // where-clauses were provided which is syntactically ill-formed but we want to recover from
1617        // it and treat them as one large where-clause.
1618        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    /// We were supposed to parse `":" $ty` but the `:` or the type was missing.
1643    /// This means that the type is missing.
1644    fn recover_missing_global_item_type(
1645        &mut self,
1646        colon_present: bool,
1647        m: Option<Mutability>,
1648    ) -> Box<Ty> {
1649        // Construct the error and stash it away with the hope
1650        // that typeck will later enrich the error with a type.
1651        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        // The user intended that the type be inferred,
1667        // so treat this as if the user wrote e.g. `const A: _ = expr;`.
1668        Box::new(Ty { kind: TyKind::Infer, span, id: ast::DUMMY_NODE_ID, tokens: None })
1669    }
1670
1671    /// Parses an enum declaration.
1672    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        // Possibly recover `enum Foo;` instead of `enum Foo {}`
1690        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                // Try to recover `enum Foo { ident : Ty }`.
1701                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(); // }
1721                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                // Parse a struct variant.
1757                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                                // We handle `enum` to `struct` suggestion in the caller.
1763                                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(); // }
1767                            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                            // We handle `enum` to `struct` suggestion in the caller.
1780                            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(); // )
1784                        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    /// Parses `struct Foo { ... }`.
1821    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        // There is a special case worth noting here, as reported in issue #17904.
1827        // If we are parsing a tuple struct it is the case that the where clause
1828        // should follow the field list. Like so:
1829        //
1830        // struct Foo<T>(T) where T: Copy;
1831        //
1832        // If we are parsing a normal record-style struct it is the case
1833        // that the where clause comes before the body, and after the generics.
1834        // So if we look ahead and see a brace or a where-clause we begin
1835        // parsing a record style struct.
1836        //
1837        // Otherwise if we look ahead and see a paren we parse a tuple-style
1838        // struct.
1839
1840        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                // If we see a misplaced tuple struct body: `struct Foo<T> where T: Copy, (T);`
1847                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                // If we see a: `struct Foo<T> where T: Copy;` style decl.
1852                VariantData::Unit(DUMMY_NODE_ID)
1853            } else {
1854                // If we see: `struct Foo<T> where T: Copy { ... }`
1855                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        // No `where` so: `struct Foo<T>;`
1863        } 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        // Record-style struct definition
1866        } 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        // Tuple-style struct definition with optional where-clause.
1874        } 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    /// Parses `union Foo { ... }`.
1888    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    /// This function parses the fields of record structs:
1920    ///
1921    ///   - `struct S { ... }`
1922    ///   - `enum E { Variant { ... } }`
1923    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        // not using parse_safety as that also accepts `safe`.
1965        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        // This is the case where we find `struct Foo<T>(T) where T: Copy;`
1976        // Unit like structs are handled in parse_item_struct function
1977        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                    // Account for `<<<<<<<` diff markers. We can't proactively error here because
1983                    // that can be a valid type start, so we snapshot and reparse only we've
1984                    // encountered another parse error.
1985                    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                // Unsafe fields are not supported in tuple structs, as doing so would result in a
1998                // parsing ambiguity for `struct X(unsafe fn())`.
1999                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    /// Parses an element of a struct declaration.
2046    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    /// Parses a structure field declaration.
2060    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(); // consume the doc comment
2096                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                // Try to recover extra trailing angle brackets
2110                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                        // Handle a case like `Vec<u8>>,` where we can continue parsing fields
2119                        // after the comma
2120                        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                        // `check_trailing_angle_brackets` already emitted a nicer error, as
2123                        // proven by the presence of `_guar`. We can continue parsing.
2124                        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                    // This is likely another field, TokenKind::Pound is used for `#[..]`
2135                    // attribute for next field. Emit the diagnostic and continue parsing.
2136                    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                    // We check that we are in a situation like `foo; bar` to avoid bad suggestions
2159                    // when there's no type and `;` was used instead of a comma.
2160                    && 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                // Gracefully handle small typos.
2168                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    /// Parses a structure field.
2183    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                // Encounter the macro invocation
2195                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    /// Parses a field identifier. Specialized version of `parse_ident_common`
2235    /// for better diagnostics and suggestions.
2236    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                // We use `parse_fn` to get a span for the function
2244                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                        // Cancel this error, we don't need it.
2295                        .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    /// Parses a declarative macro 2.0 definition.
2320    /// The `macro` keyword has already been parsed.
2321    /// ```ebnf
2322    /// MacBody = "{" TOKEN_STREAM "}" ;
2323    /// MacParams = "(" TOKEN_STREAM ")" ;
2324    /// DeclMac = "macro" Ident MacParams? MacBody ;
2325    /// ```
2326    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()? // `MacBody`
2330        } 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(); // `MacParams`
2332            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(); // `MacBody`
2337            // Convert `MacParams MacBody` into `{ MacParams => MacBody }`.
2338            let bspan = body.span();
2339            let arrow = TokenTree::token_alone(token::FatArrow, pspan.between(bspan)); // `=>`
2340            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    /// Is this a possibly malformed start of a `macro_rules! foo` item definition?
2355    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                // macro_rules foo
2363                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    /// Parses a `macro_rules! foo { ... }` declarative macro.
2376    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))?; // `macro_rules`
2382
2383        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))?; // `!`
2385        }
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            // Handle macro_rules! foo!
2390            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    /// Item macro invocations or `macro_rules!` definitions need inherited visibility.
2405    /// If that's not the case, emit an error.
2406    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        // FIXME: This will make us not emit the help even for declarative
2433        // macros within the same crate (that we can fix), which is sad.
2434        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    /// Checks if current token is one of tokens which cannot be nested like `kw::Enum`. In case
2452    /// it is, we try to parse the item and report error about nested types.
2453    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, // self.token != kw::Const
2464            )?;
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            // We successfully parsed the item but we must inform the caller about nested problem.
2476            return Ok(false);
2477        }
2478        Ok(true)
2479    }
2480}
2481
2482/// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
2483///
2484/// The function decides if, per-parameter `p`, `p` must have a pattern or just a type.
2485///
2486/// This function pointer accepts an edition, because in edition 2015, trait declarations
2487/// were allowed to omit parameter names. In 2018, they became required. It also accepts an
2488/// `IsDotDotDot` parameter, as `extern` function declarations and function pointer types are
2489/// allowed to omit the name of the `...` but regular function items are not.
2490type 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/// Parsing configuration for functions.
2499///
2500/// The syntax of function items is slightly different within trait definitions,
2501/// impl blocks, and modules. It is still parsed using the same code, just with
2502/// different flags set, so that even when the input is wrong and produces a parse
2503/// error, it still gets into the AST and the rest of the parser and
2504/// type checker can run.
2505#[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    /// A function pointer that decides if, per-parameter `p`, `p` must have a
2508    /// pattern or just a type. This field affects parsing of the parameters list.
2509    ///
2510    /// ```text
2511    /// fn foo(alef: A) -> X { X::new() }
2512    ///        -----^^ affects parsing this part of the function signature
2513    ///        |
2514    ///        if req_name returns false, then this name is optional
2515    ///
2516    /// fn bar(A) -> X;
2517    ///        ^
2518    ///        |
2519    ///        if req_name returns true, this is an error
2520    /// ```
2521    ///
2522    /// Calling this function pointer should only return false if:
2523    ///
2524    ///   * The item is being parsed inside of a trait definition.
2525    ///     Within an impl block or a module, it should always evaluate
2526    ///     to true.
2527    ///   * The span is from Edition 2015. In particular, you can get a
2528    ///     2015 span inside a 2021 crate using macros.
2529    ///
2530    /// Or if `IsDotDotDot::Yes`, this function will also return `false` if the item being parsed
2531    /// is inside an `extern` block.
2532    pub(super) req_name: ReqName,
2533    /// The context in which this function is parsed, used for diagnostics.
2534    /// This indicates the fn is a free function or method and so on.
2535    pub(super) context: FnContext,
2536    /// If this flag is set to `true`, then plain, semicolon-terminated function
2537    /// prototypes are not allowed here.
2538    ///
2539    /// ```text
2540    /// fn foo(alef: A) -> X { X::new() }
2541    ///                      ^^^^^^^^^^^^
2542    ///                      |
2543    ///                      this is always allowed
2544    ///
2545    /// fn bar(alef: A, bet: B) -> X;
2546    ///                             ^
2547    ///                             |
2548    ///                             if req_body is set to true, this is an error
2549    /// ```
2550    ///
2551    /// This field should only be set to false if the item is inside of a trait
2552    /// definition or extern block. Within an impl block or a module, it should
2553    /// always be set to true.
2554    pub(super) req_body: bool,
2555}
2556
2557/// The context in which a function is parsed.
2558/// FIXME(estebank, xizheyin): Use more variants.
2559#[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 context.
2562    Free,
2563    /// A Trait context.
2564    Trait,
2565    /// An Impl block.
2566    Impl,
2567}
2568
2569/// Parsing of functions and methods.
2570impl<'a> Parser<'a> {
2571    /// Parse a function starting from the front matter (`const ...`) to the body `{ ... }` or `;`.
2572    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)?; // `const ... fn`
2582        let ident = self.parse_ident()?; // `foo`
2583        let mut generics = self.parse_generics()?; // `<'a, T, ...>`
2584        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 we see `for Ty ...` then user probably meant `impl` item.
2589                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        // Store the end of function parameters to give better diagnostics
2599        // inside `parse_fn_body()`.
2600        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()?; // `where T: Ord`
2605
2606        // `fn_params_end` is needed only when it's followed by a where clause.
2607        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        // Either `;` or `{ ... }`.
2612        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    /// Provide diagnostics when function body is not found
2619    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                    // The enclosing `mod`, `trait` or `impl` is being closed, so keep the `fn` in
2632                    // the AST for typechecking.
2633                    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                    // Instead of a function body, the parser has encountered a right arrow
2639                    // preceded by a where clause.
2640
2641                    // Find whether token behind the right arrow is a function trait and
2642                    // store its span.
2643                    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                    // Parse the return type (along with the right arrow) and store its span.
2653                    // If there's a parse error, cancel it and return the existing error
2654                    // as we are primarily concerned with the
2655                    // expected-function-body-but-found-something-else error here.
2656                    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                        // Typo'd Fn* trait bounds such as
2672                        // fn foo<F>() where F: FnOnce -> () {}
2673                        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                        // If token behind right arrow is not a Fn* trait, the programmer
2677                        // probably misplaced the return type after the where clause like
2678                        // `fn foo<T>() where T: Default -> u8 {}`
2679                        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    /// Parse the "body" of a function.
2697    /// This can either be `;` when there's no body,
2698    /// or e.g. a block when the function is a provided one.
2699    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            // Only include `;` in list of expected tokens if body is not required
2711            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            // Include the trailing semicolon in the span of the signature
2715            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            // Recover `fn foo() = $expr;`.
2723            self.bump(); // `=`
2724            let eq_sp = self.prev_token.span;
2725            let _ = self.parse_expr()?;
2726            self.expect_semi()?; // `;`
2727            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        // In contrast to the loop below, this call inserts `impl` into the
2744        // list of expected tokens shown in diagnostics.
2745        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                    // Ok, we found a legal keyword, keep looking for `impl`
2756                    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    /// Is the current token the start of an `FnHeader` / not a valid parse?
2770    ///
2771    /// `check_pub` adds additional `pub` to the checks in case users place it
2772    /// wrongly, can be used to ensure `pub` never comes after `default`.
2773    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        // We use an over-approximation here.
2785        // `const const`, `fn const` won't parse, but we're not stepping over other syntax either.
2786        // `pub` is added in case users got confused with the ordering like `async pub fn`,
2787        // only if it wasn't preceded by `default` as `default pub` is invalid.
2788        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) // Definitely an `fn`.
2794            // `$qual fn` or `$qual $qual`:
2795            || quals.iter().any(|&exp| self.check_keyword_case(exp, case))
2796                && self.look_ahead(1, |t| {
2797                    // `$qual fn`, e.g. `const fn` or `async fn`.
2798                    t.is_keyword_case(kw::Fn, case)
2799                    // Two qualifiers `$qual $qual` is enough, e.g. `async unsafe`.
2800                    || (
2801                        (
2802                            t.is_non_raw_ident_where(|i|
2803                                quals.iter().any(|exp| exp.kw == i.name)
2804                                    // Rule out 2015 `const async: T = val`.
2805                                    && 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                        // Rule out `unsafe extern {`.
2813                        && !self.is_unsafe_foreign_mod()
2814                        // Rule out `async gen {` and `async gen move {`
2815                        && !self.is_async_gen_block()
2816                        // Rule out `const unsafe auto` and `const unsafe trait`.
2817                        && !self.is_keyword_ahead(2, &[kw::Auto, kw::Trait])
2818                    )
2819                })
2820            // `extern ABI fn`
2821            || 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                // Use `tree_look_ahead` because `ABI` might be a metavariable,
2823                // i.e. an invisible-delimited sequence, and `tree_look_ahead`
2824                // will consider that a single element when looking ahead.
2825                && 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                    // This branch is only for better diagnostics; `pub`, `unsafe`, etc. are not
2833                    // allowed here.
2834                    (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    /// Parses all the "front matter" (or "qualifiers") for a `fn` declaration,
2855    /// up to and including the `fn` keyword. The formal grammar is:
2856    ///
2857    /// ```text
2858    /// Extern = "extern" StringLit? ;
2859    /// FnQual = "const"? "async"? "unsafe"? Extern? ;
2860    /// FnFrontMatter = FnQual "fn" ;
2861    /// ```
2862    ///
2863    /// `vis` represents the visibility that was already parsed, if any. Use
2864    /// `Visibility::Inherited` when no visibility is known.
2865    ///
2866    /// If `parsing_mode` is `FrontMatterParsingMode::FunctionPtrType`, we error on `const` and `async` qualifiers,
2867    /// which are not allowed in function pointer types.
2868    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        // FIXME(gen_blocks): emit a similar error for `gen fn()`
2896
2897        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            // It is possible for `expect_one_of` to recover given the contents of
2921            // `self.expected_token_types`, therefore, do not use `self.unexpected()` which doesn't
2922            // account for this.
2923            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                    // Qualifier keywords ordering check
2928                    enum WrongKw {
2929                        Duplicated(Span),
2930                        Misplaced(Span),
2931                        /// `MisplacedDisallowedQualifier` is only used instead of `Misplaced`,
2932                        /// when the misplaced keyword is disallowed by the current `FrontMatterParsingMode`.
2933                        /// In this case, we avoid generating the suggestion to swap around the keywords,
2934                        /// as we already generated a suggestion to remove the keyword earlier.
2935                        MisplacedDisallowedQualifier,
2936                    }
2937
2938                    // We may be able to recover
2939                    let mut recover_constness = constness;
2940                    let mut recover_coroutine_kind = coroutine_kind;
2941                    let mut recover_safety = safety;
2942                    // This will allow the machine fix to directly place the keyword in the correct place or to indicate
2943                    // that the keyword is already present and the second instance should be removed.
2944                    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                                // FIXME(gen_blocks): This span is wrong, didn't want to think about it.
2981                                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                    // The keyword is already present, suggest removal of the second instance
3035                    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                    // The keyword has not been seen yet, suggest correct placement in the function front matter
3049                    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                    // Recover incorrect visibility order such as `async pub`
3064                    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(&current_vis);
3075                            let vs = vs.trim_end();
3076
3077                            // There was no explicit visibility
3078                            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                            // There was an explicit visibility
3087                            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                    // FIXME(gen_blocks): add keyword recovery logic for genness
3100
3101                    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                        // Advance past the misplaced keyword and `fn`
3106                        self.bump();
3107                        self.bump();
3108                        // When we recover from a `MisplacedDisallowedQualifier`, we already emitted an error for the disallowed qualifier
3109                        // So we don't emit another error that the qualifier is unexpected.
3110                        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    /// Parses the parameter list and result type of a function declaration.
3132    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    /// Parses the parameter list of a function, including the `(` and `)` delimiters.
3145    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        // Parse the arguments, starting out with `self` being allowed...
3151        if self.token != TokenKind::OpenParen
3152        // might be typo'd trait impl, handled elsewhere
3153        && !self.token.is_keyword(kw::For)
3154        {
3155            // recover from missing argument list, e.g. `fn main -> () {}`
3156            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                // When parsing a param failed, we should check to make the span of the param
3167                // not contain '(' before it.
3168                // For example when parsing `*mut Self` in function `fn oof(*mut Self)`.
3169                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                // Skip every token until next possible arg or end.
3176                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                // Create a placeholder argument for proper arg count (issue #34264).
3178                Ok(dummy_arg(Ident::new(sym::dummy, lo.to(p.prev_token.span)), guar))
3179            });
3180            // ...now that we've parsed the first argument, `self` is no longer allowed.
3181            first_param = false;
3182            param
3183        })?;
3184        // Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
3185        self.deduplicate_recovered_params_names(&mut params);
3186        Ok(params)
3187    }
3188
3189    /// Parses a single function parameter.
3190    ///
3191    /// - `self` is syntactically allowed when `first_param` holds.
3192    /// - `recover_arg_parse` is used to recover from a failed argument parse.
3193    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            // Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
3203            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                    // Check for trailing angle brackets
3258                    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                        // This wasn't actually a type, but a pattern looking like a type,
3272                        // so we are going to rollback and re-parse for recovery.
3273                        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                    // If this is a C-variadic argument and we hit an error, return the error.
3282                    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                        // Recover from attempting to parse the argument as a type without pattern.
3286                        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    /// Returns the parsed optional self parameter and whether a self shortcut was used.
3305    fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
3306        // Extract an identifier *after* having confirmed that the token is one.
3307        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        // is lifetime `n` tokens ahead?
3315        let is_lifetime = |this: &Self, n| this.look_ahead(n, |t| t.is_lifetime());
3316        // Is `self` `n` tokens ahead?
3317        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        // Is `pin const self` `n` tokens ahead?
3322        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        // Is `mut self` `n` tokens ahead?
3328        let is_isolated_mut_self =
3329            |this: &Self, n| this.is_keyword_ahead(n, &[kw::Mut]) && is_isolated_self(this, n + 1);
3330        // Is `pin mut self` `n` tokens ahead?
3331        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        // Parse `self` or `self: TYPE`. We already know the current token is `self`.
3336        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                // Recover `: Type` after a qualified self
3351                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        // Recover for the grammar `*self`, `*const self`, and `*mut self`.
3373        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        // Parse optional `self` parameter of a method.
3380        // Only a limited set of initial token sequences is considered `self` parameters; anything
3381        // else is parsed as a normal function parameter list, so some lookahead is required.
3382        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                    // `&{'lt} self`
3389                    self.bump(); // &
3390                    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                    // `&{'lt} mut self`
3394                    self.bump(); // &
3395                    let lifetime = has_lifetime.then(|| self.expect_lifetime());
3396                    self.bump(); // mut
3397                    SelfKind::Region(lifetime, Mutability::Mut)
3398                } else if is_isolated_pin_const_self(self, skip_lifetime_count + 1) {
3399                    // `&{'lt} pin const self`
3400                    self.bump(); // &
3401                    let lifetime = has_lifetime.then(|| self.expect_lifetime());
3402                    self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
3403                    self.bump(); // pin
3404                    self.bump(); // const
3405                    SelfKind::Pinned(lifetime, Mutability::Not)
3406                } else if is_isolated_pin_mut_self(self, skip_lifetime_count + 1) {
3407                    // `&{'lt} pin mut self`
3408                    self.bump(); // &
3409                    let lifetime = has_lifetime.then(|| self.expect_lifetime());
3410                    self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
3411                    self.bump(); // pin
3412                    self.bump(); // mut
3413                    SelfKind::Pinned(lifetime, Mutability::Mut)
3414                } else {
3415                    // `&not_self`
3416                    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            // `*self`
3423            token::Star if is_isolated_self(self, 1) => {
3424                self.bump();
3425                recover_self_ptr(self)?
3426            }
3427            // `*mut self` and `*const self`
3428            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            // `self` and `self: TYPE`
3436            token::Ident(..) if is_isolated_self(self, 0) => {
3437                parse_self_possibly_typed(self, Mutability::Not)?
3438            }
3439            // `mut self` and `mut self: TYPE`
3440            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    /// Parse the front matter of a function declaration
3486    Function,
3487    /// Parse the front matter of a function pointet type.
3488    /// For function pointer types, the `const` and `async` keywords are not permitted.
3489    FunctionPtrType,
3490}