Skip to main content

rustc_parse/parser/
item.rs

1use std::fmt::Write;
2use std::mem;
3
4use ast::token::IdentIsRaw;
5use rustc_ast as ast;
6use rustc_ast::ast::*;
7use rustc_ast::token::{self, Delimiter, InvisibleOrigin, MetaVarKind, TokenKind};
8use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
9use rustc_ast::util::case::Case;
10use rustc_ast_pretty::pprust;
11use rustc_errors::codes::*;
12use rustc_errors::{Applicability, PResult, StashKey, msg, 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            } else if let Defaultness::Final(span) = def {
201                this.dcx().emit_err(errors::FinalNotFollowedByItem { span });
202            }
203
204            if !attrs_allowed {
205                this.recover_attrs_no_item(&attrs)?;
206            }
207            Ok((None, Trailing::No, UsePreAttrPos::No))
208        })
209    }
210
211    /// Error in-case `default`/`final` was parsed in an in-appropriate context.
212    fn error_on_unconsumed_default(&self, def: Defaultness, kind: &ItemKind) {
213        match def {
214            Defaultness::Default(span) => {
215                self.dcx().emit_err(errors::InappropriateDefault {
216                    span,
217                    article: kind.article(),
218                    descr: kind.descr(),
219                });
220            }
221            Defaultness::Final(span) => {
222                self.dcx().emit_err(errors::InappropriateFinal {
223                    span,
224                    article: kind.article(),
225                    descr: kind.descr(),
226                });
227            }
228            Defaultness::Implicit => (),
229        }
230    }
231
232    /// Parses one of the items allowed by the flags.
233    fn parse_item_kind(
234        &mut self,
235        attrs: &mut AttrVec,
236        macros_allowed: bool,
237        allow_const_block_items: AllowConstBlockItems,
238        lo: Span,
239        vis: &Visibility,
240        def: &mut Defaultness,
241        fn_parse_mode: FnParseMode,
242        case: Case,
243    ) -> PResult<'a, Option<ItemKind>> {
244        let check_pub = def == &Defaultness::Implicit;
245        let mut def_ = || mem::replace(def, Defaultness::Implicit);
246
247        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) {
248            self.parse_use_item()?
249        } else if self.check_fn_front_matter(check_pub, case) {
250            // FUNCTION ITEM
251            let (ident, sig, generics, contract, body) =
252                self.parse_fn(attrs, fn_parse_mode, lo, vis, case)?;
253            ItemKind::Fn(Box::new(Fn {
254                defaultness: def_(),
255                ident,
256                sig,
257                generics,
258                contract,
259                body,
260                define_opaque: None,
261                eii_impls: ThinVec::new(),
262            }))
263        } 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) {
264            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) {
265                // EXTERN CRATE
266                self.parse_item_extern_crate()?
267            } else {
268                // EXTERN BLOCK
269                self.parse_item_foreign_mod(attrs, Safety::Default)?
270            }
271        } else if self.is_unsafe_foreign_mod() {
272            // EXTERN BLOCK
273            let safety = self.parse_safety(Case::Sensitive);
274            self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Extern,
    token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern))?;
275            self.parse_item_foreign_mod(attrs, safety)?
276        } else if let Some(safety) = self.parse_global_static_front_matter(case) {
277            // STATIC ITEM
278            let mutability = self.parse_mutability();
279            self.parse_static_item(safety, mutability)?
280        } 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() {
281            // TRAIT ITEM
282            self.parse_item_trait(attrs, lo)?
283        } else if self.check_impl_frontmatter(0) {
284            // IMPL ITEM
285            self.parse_item_impl(attrs, def_(), false)?
286        } else if let AllowConstBlockItems::Yes | AllowConstBlockItems::DoesNotMatter =
287            allow_const_block_items
288            && self.check_inline_const(0)
289        {
290            // CONST BLOCK ITEM
291            if let AllowConstBlockItems::DoesNotMatter = allow_const_block_items {
292                {
    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:292",
                        "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(292u32),
                        ::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);
293            };
294            ItemKind::ConstBlock(self.parse_const_block_item()?)
295        } else if let Const::Yes(const_span) = self.parse_constness(case) {
296            // CONST ITEM
297            self.recover_const_mut(const_span);
298            self.recover_missing_kw_before_item()?;
299            let (ident, generics, ty, rhs_kind) = self.parse_const_item(false, const_span)?;
300            ItemKind::Const(Box::new(ConstItem {
301                defaultness: def_(),
302                ident,
303                generics,
304                ty,
305                rhs_kind,
306                define_opaque: None,
307            }))
308        } else if let Some(kind) = self.is_reuse_item() {
309            self.parse_item_delegation(attrs, def_(), kind)?
310        } 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)
311            || 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])
312        {
313            // MODULE ITEM
314            self.parse_item_mod(attrs)?
315        } 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) {
316            if let Const::Yes(const_span) = self.parse_constness(case) {
317                // TYPE CONST (mgca)
318                self.recover_const_mut(const_span);
319                self.recover_missing_kw_before_item()?;
320                let (ident, generics, ty, rhs_kind) = self.parse_const_item(true, const_span)?;
321                // Make sure this is only allowed if the feature gate is enabled.
322                // #![feature(mgca_type_const_syntax)]
323                self.psess.gated_spans.gate(sym::mgca_type_const_syntax, lo.to(const_span));
324                ItemKind::Const(Box::new(ConstItem {
325                    defaultness: def_(),
326                    ident,
327                    generics,
328                    ty,
329                    rhs_kind,
330                    define_opaque: None,
331                }))
332            } else {
333                // TYPE ITEM
334                self.parse_type_alias(def_())?
335            }
336        } 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) {
337            // ENUM ITEM
338            self.parse_item_enum()?
339        } 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) {
340            // STRUCT ITEM
341            self.parse_item_struct()?
342        } else if self.is_kw_followed_by_ident(kw::Union) {
343            // UNION ITEM
344            self.bump(); // `union`
345            self.parse_item_union()?
346        } else if self.is_builtin() {
347            // BUILTIN# ITEM
348            return self.parse_item_builtin();
349        } 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) {
350            // MACROS 2.0 ITEM
351            self.parse_item_decl_macro(lo)?
352        } else if let IsMacroRulesItem::Yes { has_bang } = self.is_macro_rules_item() {
353            // MACRO_RULES ITEM
354            self.parse_item_macro_rules(vis, has_bang)?
355        } else if self.isnt_macro_invocation()
356            && (self.token.is_ident_named(sym::import)
357                || self.token.is_ident_named(sym::using)
358                || self.token.is_ident_named(sym::include)
359                || self.token.is_ident_named(sym::require))
360        {
361            return self.recover_import_as_use();
362        } else if self.isnt_macro_invocation() && vis.kind.is_pub() {
363            self.recover_missing_kw_before_item()?;
364            return Ok(None);
365        } else if self.isnt_macro_invocation() && case == Case::Sensitive {
366            _ = def_;
367
368            // Recover wrong cased keywords
369            return self.parse_item_kind(
370                attrs,
371                macros_allowed,
372                allow_const_block_items,
373                lo,
374                vis,
375                def,
376                fn_parse_mode,
377                Case::Insensitive,
378            );
379        } else if macros_allowed && self.check_path() {
380            if self.isnt_macro_invocation() {
381                self.recover_missing_kw_before_item()?;
382            }
383            // MACRO INVOCATION ITEM
384            ItemKind::MacCall(Box::new(self.parse_item_macro(vis)?))
385        } else {
386            return Ok(None);
387        };
388        Ok(Some(info))
389    }
390
391    fn recover_import_as_use(&mut self) -> PResult<'a, Option<ItemKind>> {
392        let span = self.token.span;
393        let token_name = super::token_descr(&self.token);
394        let snapshot = self.create_snapshot_for_diagnostic();
395        self.bump();
396        match self.parse_use_item() {
397            Ok(u) => {
398                self.dcx().emit_err(errors::RecoverImportAsUse { span, token_name });
399                Ok(Some(u))
400            }
401            Err(e) => {
402                e.cancel();
403                self.restore_snapshot(snapshot);
404                Ok(None)
405            }
406        }
407    }
408
409    fn parse_use_item(&mut self) -> PResult<'a, ItemKind> {
410        let tree = self.parse_use_tree()?;
411        if let Err(mut e) = self.expect_semi() {
412            match tree.kind {
413                UseTreeKind::Glob => {
414                    e.note("the wildcard token must be last on the path");
415                }
416                UseTreeKind::Nested { .. } => {
417                    e.note("glob-like brace syntax must be last on the path");
418                }
419                _ => (),
420            }
421            return Err(e);
422        }
423        Ok(ItemKind::Use(tree))
424    }
425
426    /// When parsing a statement, would the start of a path be an item?
427    pub(super) fn is_path_start_item(&mut self) -> bool {
428        self.is_kw_followed_by_ident(kw::Union) // no: `union::b`, yes: `union U { .. }`
429        || self.is_reuse_item().is_some() // yes: `reuse impl Trait for Struct { self.0 }`, yes: `reuse some_path::foo;`
430        || self.check_trait_front_matter() // no: `auto::b`, yes: `auto trait X { .. }`
431        || self.is_async_fn() // no(2015): `async::b`, yes: `async fn`
432        || #[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`
433    }
434
435    fn is_reuse_item(&mut self) -> Option<ReuseKind> {
436        if !self.token.is_keyword(kw::Reuse) {
437            return None;
438        }
439
440        // no: `reuse ::path` for compatibility reasons with macro invocations
441        if self.look_ahead(1, |t| t.is_path_start() && *t != token::PathSep) {
442            Some(ReuseKind::Path)
443        } else if self.check_impl_frontmatter(1) {
444            Some(ReuseKind::Impl)
445        } else {
446            None
447        }
448    }
449
450    /// Are we sure this could not possibly be a macro invocation?
451    fn isnt_macro_invocation(&mut self) -> bool {
452        self.check_ident() && self.look_ahead(1, |t| *t != token::Bang && *t != token::PathSep)
453    }
454
455    /// Recover on encountering a struct, enum, or method definition where the user
456    /// forgot to add the `struct`, `enum`, or `fn` keyword
457    fn recover_missing_kw_before_item(&mut self) -> PResult<'a, ()> {
458        let is_pub = self.prev_token.is_keyword(kw::Pub);
459        let is_const = self.prev_token.is_keyword(kw::Const);
460        let ident_span = self.token.span;
461        let span = if is_pub { self.prev_token.span.to(ident_span) } else { ident_span };
462        let insert_span = ident_span.shrink_to_lo();
463
464        let ident = if self.token.is_ident()
465            && (!is_const || self.look_ahead(1, |t| *t == token::OpenParen))
466            && self.look_ahead(1, |t| {
467                #[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)
468            }) {
469            self.parse_ident_common(true).unwrap()
470        } else {
471            return Ok(());
472        };
473
474        let mut found_generics = false;
475        if self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Lt,
    token_type: crate::parser::token_type::TokenType::Lt,
}exp!(Lt)) {
476            found_generics = true;
477            self.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Gt,
    token_type: crate::parser::token_type::TokenType::Gt,
}exp!(Gt)]);
478            self.bump(); // `>`
479        }
480
481        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)) {
482            // possible struct or enum definition where `struct` or `enum` was forgotten
483            if self.look_ahead(1, |t| *t == token::CloseBrace) {
484                // `S {}` could be unit enum or struct
485                Some(errors::MissingKeywordForItemDefinition::EnumOrStruct { span })
486            } else if self.look_ahead(2, |t| *t == token::Colon)
487                || self.look_ahead(3, |t| *t == token::Colon)
488            {
489                // `S { f:` or `S { pub f:`
490                Some(errors::MissingKeywordForItemDefinition::Struct { span, insert_span, ident })
491            } else {
492                Some(errors::MissingKeywordForItemDefinition::Enum { span, insert_span, ident })
493            }
494        } else if self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenParen,
    token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
495            // possible function or tuple struct definition where `fn` or `struct` was forgotten
496            self.bump(); // `(`
497            let is_method = self.recover_self_param();
498
499            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);
500
501            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)) {
502                self.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenBrace,
    token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)]);
503                self.bump(); // `{`
504                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);
505                if is_method {
506                    errors::MissingKeywordForItemDefinition::Method { span, insert_span, ident }
507                } else {
508                    errors::MissingKeywordForItemDefinition::Function { span, insert_span, ident }
509                }
510            } 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)) {
511                errors::MissingKeywordForItemDefinition::Struct { span, insert_span, ident }
512            } else {
513                errors::MissingKeywordForItemDefinition::Ambiguous {
514                    span,
515                    subdiag: if found_generics {
516                        None
517                    } else if let Ok(snippet) = self.span_to_snippet(ident_span) {
518                        Some(errors::AmbiguousMissingKwForItemSub::SuggestMacro {
519                            span: ident_span,
520                            snippet,
521                        })
522                    } else {
523                        Some(errors::AmbiguousMissingKwForItemSub::HelpMacro)
524                    },
525                }
526            };
527            Some(err)
528        } else if found_generics {
529            Some(errors::MissingKeywordForItemDefinition::Ambiguous { span, subdiag: None })
530        } else {
531            None
532        };
533
534        if let Some(err) = err { Err(self.dcx().create_err(err)) } else { Ok(()) }
535    }
536
537    fn parse_item_builtin(&mut self) -> PResult<'a, Option<ItemKind>> {
538        // To be expanded
539        Ok(None)
540    }
541
542    /// Parses an item macro, e.g., `item!();`.
543    fn parse_item_macro(&mut self, vis: &Visibility) -> PResult<'a, MacCall> {
544        let path = self.parse_path(PathStyle::Mod)?; // `foo::bar`
545        self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Bang,
    token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang))?; // `!`
546        match self.parse_delim_args() {
547            // `( .. )` or `[ .. ]` (followed by `;`), or `{ .. }`.
548            Ok(args) => {
549                self.eat_semi_for_macro_if_needed(&args, Some(&path));
550                self.complain_if_pub_macro(vis, false);
551                Ok(MacCall { path, args })
552            }
553
554            Err(mut err) => {
555                // Maybe the user misspelled `macro_rules` (issue #91227)
556                if self.token.is_ident()
557                    && let [segment] = path.segments.as_slice()
558                    && edit_distance("macro_rules", &segment.ident.to_string(), 2).is_some()
559                {
560                    err.span_suggestion(
561                        path.span,
562                        "perhaps you meant to define a macro",
563                        "macro_rules",
564                        Applicability::MachineApplicable,
565                    );
566                }
567                Err(err)
568            }
569        }
570    }
571
572    /// Recover if we parsed attributes and expected an item but there was none.
573    fn recover_attrs_no_item(&mut self, attrs: &[Attribute]) -> PResult<'a, ()> {
574        let ([start @ end] | [start, .., end]) = attrs else {
575            return Ok(());
576        };
577        let msg = if end.is_doc_comment() {
578            "expected item after doc comment"
579        } else {
580            "expected item after attributes"
581        };
582        let mut err = self.dcx().struct_span_err(end.span, msg);
583        if end.is_doc_comment() {
584            err.span_label(end.span, "this doc comment doesn't document anything");
585        } else if self.token == TokenKind::Semi {
586            err.span_suggestion_verbose(
587                self.token.span,
588                "consider removing this semicolon",
589                "",
590                Applicability::MaybeIncorrect,
591            );
592        }
593        if let [.., penultimate, _] = attrs {
594            err.span_label(start.span.to(penultimate.span), "other attributes here");
595        }
596        Err(err)
597    }
598
599    fn is_async_fn(&self) -> bool {
600        self.token.is_keyword(kw::Async) && self.is_keyword_ahead(1, &[kw::Fn])
601    }
602
603    fn parse_polarity(&mut self) -> ast::ImplPolarity {
604        // Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type.
605        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()) {
606            self.bump(); // `!`
607            ast::ImplPolarity::Negative(self.prev_token.span)
608        } else {
609            ast::ImplPolarity::Positive
610        }
611    }
612
613    /// Parses an implementation item.
614    ///
615    /// ```ignore (illustrative)
616    /// impl<'a, T> TYPE { /* impl items */ }
617    /// impl<'a, T> TRAIT for TYPE { /* impl items */ }
618    /// impl<'a, T> !TRAIT for TYPE { /* impl items */ }
619    /// impl<'a, T> const TRAIT for TYPE { /* impl items */ }
620    /// ```
621    ///
622    /// We actually parse slightly more relaxed grammar for better error reporting and recovery.
623    /// ```ebnf
624    /// "impl" GENERICS "const"? "!"? TYPE "for"? (TYPE | "..") ("where" PREDICATES)? "{" BODY "}"
625    /// "impl" GENERICS "const"? "!"? TYPE ("where" PREDICATES)? "{" BODY "}"
626    /// ```
627    fn parse_item_impl(
628        &mut self,
629        attrs: &mut AttrVec,
630        defaultness: Defaultness,
631        is_reuse: bool,
632    ) -> PResult<'a, ItemKind> {
633        let mut constness = self.parse_constness(Case::Sensitive);
634        let safety = self.parse_safety(Case::Sensitive);
635        self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Impl,
    token_type: crate::parser::token_type::TokenType::KwImpl,
}exp!(Impl))?;
636
637        // First, parse generic parameters if necessary.
638        let mut generics = if self.choose_generics_over_qpath(0) {
639            self.parse_generics()?
640        } else {
641            let mut generics = Generics::default();
642            // impl A for B {}
643            //    /\ this is where `generics.span` should point when there are no type params.
644            generics.span = self.prev_token.span.shrink_to_hi();
645            generics
646        };
647
648        if let Const::No = constness {
649            // FIXME(const_trait_impl): disallow `impl const Trait`
650            constness = self.parse_constness(Case::Sensitive);
651        }
652
653        if let Const::Yes(span) = constness {
654            self.psess.gated_spans.gate(sym::const_trait_impl, span);
655        }
656
657        // Parse stray `impl async Trait`
658        if (self.token_uninterpolated_span().at_least_rust_2018()
659            && self.token.is_keyword(kw::Async))
660            || self.is_kw_followed_by_ident(kw::Async)
661        {
662            self.bump();
663            self.dcx().emit_err(errors::AsyncImpl { span: self.prev_token.span });
664        }
665
666        let polarity = self.parse_polarity();
667
668        // Parse both types and traits as a type, then reinterpret if necessary.
669        let ty_first = if self.token.is_keyword(kw::For) && self.look_ahead(1, |t| t != &token::Lt)
670        {
671            let span = self.prev_token.span.between(self.token.span);
672            return Err(self.dcx().create_err(errors::MissingTraitInTraitImpl {
673                span,
674                for_span: span.to(self.token.span),
675            }));
676        } else {
677            self.parse_ty_with_generics_recovery(&generics)?
678        };
679
680        // If `for` is missing we try to recover.
681        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));
682        let missing_for_span = self.prev_token.span.between(self.token.span);
683
684        let ty_second = if self.token == token::DotDot {
685            // We need to report this error after `cfg` expansion for compatibility reasons
686            self.bump(); // `..`, do not add it to expected tokens
687
688            // AST validation later detects this `TyKind::Dummy` and emits an
689            // error. (#121072 will hopefully remove all this special handling
690            // of the obsolete `impl Trait for ..` and then this can go away.)
691            Some(self.mk_ty(self.prev_token.span, TyKind::Dummy))
692        } else if has_for || self.token.can_begin_type() {
693            Some(self.parse_ty()?)
694        } else {
695            None
696        };
697
698        generics.where_clause = self.parse_where_clause()?;
699
700        let impl_items = if is_reuse {
701            Default::default()
702        } else {
703            self.parse_item_list(attrs, |p| p.parse_impl_item(ForceCollect::No))?
704        };
705
706        let (of_trait, self_ty) = match ty_second {
707            Some(ty_second) => {
708                // impl Trait for Type
709                if !has_for {
710                    self.dcx().emit_err(errors::MissingForInTraitImpl { span: missing_for_span });
711                }
712
713                let ty_first = *ty_first;
714                let path = match ty_first.kind {
715                    // This notably includes paths passed through `ty` macro fragments (#46438).
716                    TyKind::Path(None, path) => path,
717                    other => {
718                        if let TyKind::ImplTrait(_, bounds) = other
719                            && let [bound] = bounds.as_slice()
720                            && let GenericBound::Trait(poly_trait_ref) = bound
721                        {
722                            // Suggest removing extra `impl` keyword:
723                            // `impl<T: Default> impl Default for Wrapper<T>`
724                            //                   ^^^^^
725                            let extra_impl_kw = ty_first.span.until(bound.span());
726                            self.dcx().emit_err(errors::ExtraImplKeywordInTraitImpl {
727                                extra_impl_kw,
728                                impl_trait_span: ty_first.span,
729                            });
730                            poly_trait_ref.trait_ref.path.clone()
731                        } else {
732                            return Err(self.dcx().create_err(
733                                errors::ExpectedTraitInTraitImplFoundType { span: ty_first.span },
734                            ));
735                        }
736                    }
737                };
738                let trait_ref = TraitRef { path, ref_id: ty_first.id };
739
740                let of_trait =
741                    Some(Box::new(TraitImplHeader { defaultness, safety, polarity, trait_ref }));
742                (of_trait, ty_second)
743            }
744            None => {
745                let self_ty = ty_first;
746                let error = |modifier, modifier_name, modifier_span| {
747                    self.dcx().create_err(errors::TraitImplModifierInInherentImpl {
748                        span: self_ty.span,
749                        modifier,
750                        modifier_name,
751                        modifier_span,
752                        self_ty: self_ty.span,
753                    })
754                };
755
756                if let Safety::Unsafe(span) = safety {
757                    error("unsafe", "unsafe", span).with_code(E0197).emit();
758                }
759                if let ImplPolarity::Negative(span) = polarity {
760                    error("!", "negative", span).emit();
761                }
762                if let Defaultness::Default(def_span) = defaultness {
763                    error("default", "default", def_span).emit();
764                }
765                if let Const::Yes(span) = constness {
766                    self.psess.gated_spans.gate(sym::const_trait_impl, span);
767                }
768                (None, self_ty)
769            }
770        };
771
772        Ok(ItemKind::Impl(Impl { generics, of_trait, self_ty, items: impl_items, constness }))
773    }
774
775    fn parse_item_delegation(
776        &mut self,
777        attrs: &mut AttrVec,
778        defaultness: Defaultness,
779        kind: ReuseKind,
780    ) -> PResult<'a, ItemKind> {
781        let span = self.token.span;
782        self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Reuse,
    token_type: crate::parser::token_type::TokenType::KwReuse,
}exp!(Reuse))?;
783
784        let item_kind = match kind {
785            ReuseKind::Path => self.parse_path_like_delegation(),
786            ReuseKind::Impl => self.parse_impl_delegation(span, attrs, defaultness),
787        }?;
788
789        self.psess.gated_spans.gate(sym::fn_delegation, span.to(self.prev_token.span));
790
791        Ok(item_kind)
792    }
793
794    fn parse_delegation_body(&mut self) -> PResult<'a, Option<Box<Block>>> {
795        Ok(if self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenBrace,
    token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
796            Some(self.parse_block()?)
797        } else {
798            self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Semi,
    token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi))?;
799            None
800        })
801    }
802
803    fn parse_impl_delegation(
804        &mut self,
805        span: Span,
806        attrs: &mut AttrVec,
807        defaultness: Defaultness,
808    ) -> PResult<'a, ItemKind> {
809        let mut impl_item = self.parse_item_impl(attrs, defaultness, true)?;
810        let ItemKind::Impl(Impl { items, of_trait, .. }) = &mut impl_item else { ::core::panicking::panic("internal error: entered unreachable code")unreachable!() };
811
812        let until_expr_span = span.to(self.prev_token.span);
813
814        let Some(of_trait) = of_trait else {
815            return Err(self
816                .dcx()
817                .create_err(errors::ImplReuseInherentImpl { span: until_expr_span }));
818        };
819
820        let body = self.parse_delegation_body()?;
821        let whole_reuse_span = span.to(self.prev_token.span);
822
823        items.push(Box::new(AssocItem {
824            id: DUMMY_NODE_ID,
825            attrs: Default::default(),
826            span: whole_reuse_span,
827            tokens: None,
828            vis: Visibility {
829                kind: VisibilityKind::Inherited,
830                span: whole_reuse_span,
831                tokens: None,
832            },
833            kind: AssocItemKind::DelegationMac(Box::new(DelegationMac {
834                qself: None,
835                prefix: of_trait.trait_ref.path.clone(),
836                suffixes: None,
837                body,
838            })),
839        }));
840
841        Ok(impl_item)
842    }
843
844    fn parse_path_like_delegation(&mut self) -> PResult<'a, ItemKind> {
845        let (qself, path) = if self.eat_lt() {
846            let (qself, path) = self.parse_qpath(PathStyle::Expr)?;
847            (Some(qself), path)
848        } else {
849            (None, self.parse_path(PathStyle::Expr)?)
850        };
851
852        let rename = |this: &mut Self| {
853            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 })
854        };
855
856        Ok(if self.eat_path_sep() {
857            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)) {
858                None
859            } else {
860                let parse_suffix = |p: &mut Self| Ok((p.parse_path_segment_ident()?, rename(p)?));
861                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)
862            };
863
864            ItemKind::DelegationMac(Box::new(DelegationMac {
865                qself,
866                prefix: path,
867                suffixes,
868                body: self.parse_delegation_body()?,
869            }))
870        } else {
871            let rename = rename(self)?;
872            let ident = rename.unwrap_or_else(|| path.segments.last().unwrap().ident);
873
874            ItemKind::Delegation(Box::new(Delegation {
875                id: DUMMY_NODE_ID,
876                qself,
877                path,
878                ident,
879                rename,
880                body: self.parse_delegation_body()?,
881                from_glob: false,
882            }))
883        })
884    }
885
886    fn parse_item_list<T>(
887        &mut self,
888        attrs: &mut AttrVec,
889        mut parse_item: impl FnMut(&mut Parser<'a>) -> PResult<'a, Option<Option<T>>>,
890    ) -> PResult<'a, ThinVec<T>> {
891        let open_brace_span = self.token.span;
892
893        // Recover `impl Ty;` instead of `impl Ty {}`
894        if self.token == TokenKind::Semi {
895            self.dcx().emit_err(errors::UseEmptyBlockNotSemi { span: self.token.span });
896            self.bump();
897            return Ok(ThinVec::new());
898        }
899
900        self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenBrace,
    token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace))?;
901        attrs.extend(self.parse_inner_attributes()?);
902
903        let mut items = ThinVec::new();
904        while !self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseBrace,
    token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace)) {
905            if self.recover_doc_comment_before_brace() {
906                continue;
907            }
908            self.recover_vcs_conflict_marker();
909            match parse_item(self) {
910                Ok(None) => {
911                    let mut is_unnecessary_semicolon = !items.is_empty()
912                        // When the close delim is `)` in a case like the following, `token.kind`
913                        // is expected to be `token::CloseParen`, but the actual `token.kind` is
914                        // `token::CloseBrace`. This is because the `token.kind` of the close delim
915                        // is treated as the same as that of the open delim in
916                        // `TokenTreesReader::parse_token_tree`, even if the delimiters of them are
917                        // different. Therefore, `token.kind` should not be compared here.
918                        //
919                        // issue-60075.rs
920                        // ```
921                        // trait T {
922                        //     fn qux() -> Option<usize> {
923                        //         let _ = if true {
924                        //         });
925                        //          ^ this close delim
926                        //         Some(4)
927                        //     }
928                        // ```
929                        && self
930                            .span_to_snippet(self.prev_token.span)
931                            .is_ok_and(|snippet| snippet == "}")
932                        && self.token == token::Semi;
933                    let mut semicolon_span = self.token.span;
934                    if !is_unnecessary_semicolon {
935                        // #105369, Detect spurious `;` before assoc fn body
936                        is_unnecessary_semicolon =
937                            self.token == token::OpenBrace && self.prev_token == token::Semi;
938                        semicolon_span = self.prev_token.span;
939                    }
940                    // We have to bail or we'll potentially never make progress.
941                    let non_item_span = self.token.span;
942                    let is_let = self.token.is_keyword(kw::Let);
943
944                    let mut err =
945                        self.dcx().struct_span_err(non_item_span, "non-item in item list");
946                    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);
947                    if is_let {
948                        err.span_suggestion_verbose(
949                            non_item_span,
950                            "consider using `const` instead of `let` for associated const",
951                            "const",
952                            Applicability::MachineApplicable,
953                        );
954                    } else {
955                        err.span_label(open_brace_span, "item list starts here")
956                            .span_label(non_item_span, "non-item starts here")
957                            .span_label(self.prev_token.span, "item list ends here");
958                    }
959                    if is_unnecessary_semicolon {
960                        err.span_suggestion(
961                            semicolon_span,
962                            "consider removing this semicolon",
963                            "",
964                            Applicability::MaybeIncorrect,
965                        );
966                    }
967                    err.emit();
968                    break;
969                }
970                Ok(Some(item)) => items.extend(item),
971                Err(err) => {
972                    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);
973                    err.with_span_label(
974                        open_brace_span,
975                        "while parsing this item list starting here",
976                    )
977                    .with_span_label(self.prev_token.span, "the item list ends here")
978                    .emit();
979                    break;
980                }
981            }
982        }
983        Ok(items)
984    }
985
986    /// Recover on a doc comment before `}`.
987    fn recover_doc_comment_before_brace(&mut self) -> bool {
988        if let token::DocComment(..) = self.token.kind {
989            if self.look_ahead(1, |tok| tok == &token::CloseBrace) {
990                // FIXME: merge with `DocCommentDoesNotDocumentAnything` (E0585)
991                {
    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!(
992                    self.dcx(),
993                    self.token.span,
994                    E0584,
995                    "found a documentation comment that doesn't document anything",
996                )
997                .with_span_label(self.token.span, "this doc comment doesn't document anything")
998                .with_help(
999                    "doc comments must come before what they document, if a comment was \
1000                    intended use `//`",
1001                )
1002                .emit();
1003                self.bump();
1004                return true;
1005            }
1006        }
1007        false
1008    }
1009
1010    /// Parses defaultness (i.e., `default` or nothing).
1011    fn parse_defaultness(&mut self) -> Defaultness {
1012        // We are interested in `default` followed by another identifier.
1013        // However, we must avoid keywords that occur as binary operators.
1014        // Currently, the only applicable keyword is `as` (`default as Ty`).
1015        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))
1016            && self.look_ahead(1, |t| t.is_non_raw_ident_where(|i| i.name != kw::As))
1017        {
1018            self.bump(); // `default`
1019            Defaultness::Default(self.prev_token_uninterpolated_span())
1020        } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Final,
    token_type: crate::parser::token_type::TokenType::KwFinal,
}exp!(Final)) {
1021            self.psess.gated_spans.gate(sym::final_associated_functions, self.prev_token.span);
1022            Defaultness::Final(self.prev_token_uninterpolated_span())
1023        } else {
1024            Defaultness::Implicit
1025        }
1026    }
1027
1028    /// Is there an `[ impl(in? path) ]? trait` item `dist` tokens ahead?
1029    fn is_trait_with_maybe_impl_restriction_in_front(&self, dist: usize) -> bool {
1030        // `trait`
1031        if self.is_keyword_ahead(dist, &[kw::Trait]) {
1032            return true;
1033        }
1034        // `impl(`
1035        if !self.is_keyword_ahead(dist, &[kw::Impl])
1036            || !self.look_ahead(dist + 1, |t| t == &token::OpenParen)
1037        {
1038            return false;
1039        }
1040        // `crate | super | self) trait`
1041        if self.is_keyword_ahead(dist + 2, &[kw::Crate, kw::Super, kw::SelfLower])
1042            && self.look_ahead(dist + 3, |t| t == &token::CloseParen)
1043            && self.is_keyword_ahead(dist + 4, &[kw::Trait])
1044        {
1045            return true;
1046        }
1047        // `impl(in? something) trait`
1048        // We catch cases where the `in` keyword is missing to provide a
1049        // better error message. This is handled later in
1050        // `self.recover_incorrect_impl_restriction`.
1051        self.tree_look_ahead(dist + 2, |t| {
1052            if let TokenTree::Token(token, _) = t { token.is_keyword(kw::Trait) } else { false }
1053        })
1054        .unwrap_or(false)
1055    }
1056
1057    /// Is this an `(const unsafe? auto? [ impl(in? path) ]? | unsafe auto? [ impl(in? path) ]? | auto [ impl(in? path) ]? | [ impl(in? path) ]?) trait` item?
1058    fn check_trait_front_matter(&mut self) -> bool {
1059        // `[ impl(in? path) ]? trait`
1060        if self.is_trait_with_maybe_impl_restriction_in_front(0) {
1061            return true;
1062        }
1063        // `auto [ impl(in? path) ]? trait`
1064        if 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_trait_with_maybe_impl_restriction_in_front(1) {
1065            return true;
1066        }
1067        // `unsafe auto? [ impl(in? path) ]? trait`
1068        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))
1069            && (self.is_trait_with_maybe_impl_restriction_in_front(1)
1070                || self.is_keyword_ahead(1, &[kw::Auto])
1071                    && self.is_trait_with_maybe_impl_restriction_in_front(2))
1072        {
1073            return true;
1074        }
1075        // `const` ...
1076        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)) {
1077            return false;
1078        }
1079        // `const [ impl(in? path) ]? trait`
1080        if self.is_trait_with_maybe_impl_restriction_in_front(1) {
1081            return true;
1082        }
1083        // `const (unsafe | auto) [ impl(in? path) ]? trait`
1084        if self.is_keyword_ahead(1, &[kw::Unsafe, kw::Auto])
1085            && self.is_trait_with_maybe_impl_restriction_in_front(2)
1086        {
1087            return true;
1088        }
1089        // `const unsafe auto [ impl(in? path) ]? trait`
1090        self.is_keyword_ahead(1, &[kw::Unsafe])
1091            && self.is_keyword_ahead(2, &[kw::Auto])
1092            && self.is_trait_with_maybe_impl_restriction_in_front(3)
1093    }
1094
1095    /// Parses `const? unsafe? auto? [impl(in? path)]? trait Foo { ... }` or `trait Foo = Bar;`.
1096    ///
1097    /// FIXME(restrictions): The current keyword order follows the grammar specified in RFC 3323.
1098    /// However, whether the restriction should be grouped closer to the visibility modifier
1099    /// (e.g., `pub impl(crate) const unsafe auto trait`) remains an unresolved design question.
1100    /// This ordering must be kept in sync with the logic in `check_trait_front_matter`.
1101    fn parse_item_trait(&mut self, attrs: &mut AttrVec, lo: Span) -> PResult<'a, ItemKind> {
1102        let constness = self.parse_constness(Case::Sensitive);
1103        if let Const::Yes(span) = constness {
1104            self.psess.gated_spans.gate(sym::const_trait_impl, span);
1105        }
1106        let safety = self.parse_safety(Case::Sensitive);
1107        // Parse optional `auto` prefix.
1108        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)) {
1109            self.psess.gated_spans.gate(sym::auto_traits, self.prev_token.span);
1110            IsAuto::Yes
1111        } else {
1112            IsAuto::No
1113        };
1114
1115        let impl_restriction = self.parse_impl_restriction()?;
1116
1117        self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Trait,
    token_type: crate::parser::token_type::TokenType::KwTrait,
}exp!(Trait))?;
1118        let ident = self.parse_ident()?;
1119        let mut generics = self.parse_generics()?;
1120
1121        // Parse optional colon and supertrait bounds.
1122        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));
1123        let span_at_colon = self.prev_token.span;
1124        let bounds = if had_colon { self.parse_generic_bounds()? } else { Vec::new() };
1125
1126        let span_before_eq = self.prev_token.span;
1127        if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Eq,
    token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) {
1128            // It's a trait alias.
1129            if had_colon {
1130                let span = span_at_colon.to(span_before_eq);
1131                self.dcx().emit_err(errors::BoundsNotAllowedOnTraitAliases { span });
1132            }
1133
1134            let bounds = self.parse_generic_bounds()?;
1135            generics.where_clause = self.parse_where_clause()?;
1136            self.expect_semi()?;
1137
1138            let whole_span = lo.to(self.prev_token.span);
1139            if is_auto == IsAuto::Yes {
1140                self.dcx().emit_err(errors::TraitAliasCannotBeAuto { span: whole_span });
1141            }
1142            if let Safety::Unsafe(_) = safety {
1143                self.dcx().emit_err(errors::TraitAliasCannotBeUnsafe { span: whole_span });
1144            }
1145            if let RestrictionKind::Restricted { .. } = impl_restriction.kind {
1146                self.dcx().emit_err(errors::TraitAliasCannotBeImplRestricted { span: whole_span });
1147            }
1148
1149            self.psess.gated_spans.gate(sym::trait_alias, whole_span);
1150
1151            Ok(ItemKind::TraitAlias(Box::new(TraitAlias { constness, ident, generics, bounds })))
1152        } else {
1153            // It's a normal trait.
1154            generics.where_clause = self.parse_where_clause()?;
1155            let items = self.parse_item_list(attrs, |p| p.parse_trait_item(ForceCollect::No))?;
1156            Ok(ItemKind::Trait(Box::new(Trait {
1157                constness,
1158                is_auto,
1159                safety,
1160                impl_restriction,
1161                ident,
1162                generics,
1163                bounds,
1164                items,
1165            })))
1166        }
1167    }
1168
1169    pub fn parse_impl_item(
1170        &mut self,
1171        force_collect: ForceCollect,
1172    ) -> PResult<'a, Option<Option<Box<AssocItem>>>> {
1173        let fn_parse_mode =
1174            FnParseMode { req_name: |_, _| true, context: FnContext::Impl, req_body: true };
1175        self.parse_assoc_item(fn_parse_mode, force_collect)
1176    }
1177
1178    pub fn parse_trait_item(
1179        &mut self,
1180        force_collect: ForceCollect,
1181    ) -> PResult<'a, Option<Option<Box<AssocItem>>>> {
1182        let fn_parse_mode = FnParseMode {
1183            req_name: |edition, _| edition >= Edition::Edition2018,
1184            context: FnContext::Trait,
1185            req_body: false,
1186        };
1187        self.parse_assoc_item(fn_parse_mode, force_collect)
1188    }
1189
1190    /// Parses associated items.
1191    fn parse_assoc_item(
1192        &mut self,
1193        fn_parse_mode: FnParseMode,
1194        force_collect: ForceCollect,
1195    ) -> PResult<'a, Option<Option<Box<AssocItem>>>> {
1196        Ok(self
1197            .parse_item_(
1198                fn_parse_mode,
1199                force_collect,
1200                AllowConstBlockItems::DoesNotMatter, // due to `AssocItemKind::try_from` below
1201            )?
1202            .map(|Item { attrs, id, span, vis, kind, tokens }| {
1203                let kind = match AssocItemKind::try_from(kind) {
1204                    Ok(kind) => kind,
1205                    Err(kind) => match kind {
1206                        ItemKind::Static(box StaticItem {
1207                            ident,
1208                            ty,
1209                            safety: _,
1210                            mutability: _,
1211                            expr,
1212                            define_opaque,
1213                        }) => {
1214                            self.dcx().emit_err(errors::AssociatedStaticItemNotAllowed { span });
1215                            AssocItemKind::Const(Box::new(ConstItem {
1216                                defaultness: Defaultness::Implicit,
1217                                ident,
1218                                generics: Generics::default(),
1219                                ty,
1220                                rhs_kind: ConstItemRhsKind::Body { rhs: expr },
1221                                define_opaque,
1222                            }))
1223                        }
1224                        _ => return self.error_bad_item_kind(span, &kind, "`trait`s or `impl`s"),
1225                    },
1226                };
1227                Some(Box::new(Item { attrs, id, span, vis, kind, tokens }))
1228            }))
1229    }
1230
1231    /// Parses a `type` alias with the following grammar:
1232    /// ```ebnf
1233    /// TypeAlias = "type" Ident Generics (":" GenericBounds)? WhereClause ("=" Ty)? WhereClause ";" ;
1234    /// ```
1235    /// The `"type"` has already been eaten.
1236    fn parse_type_alias(&mut self, defaultness: Defaultness) -> PResult<'a, ItemKind> {
1237        let ident = self.parse_ident()?;
1238        let mut generics = self.parse_generics()?;
1239
1240        // Parse optional colon and param bounds.
1241        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() };
1242        generics.where_clause = self.parse_where_clause()?;
1243
1244        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 };
1245
1246        let after_where_clause = self.parse_where_clause()?;
1247
1248        self.expect_semi()?;
1249
1250        Ok(ItemKind::TyAlias(Box::new(TyAlias {
1251            defaultness,
1252            ident,
1253            generics,
1254            after_where_clause,
1255            bounds,
1256            ty,
1257        })))
1258    }
1259
1260    /// Parses a `UseTree`.
1261    ///
1262    /// ```text
1263    /// USE_TREE = [`::`] `*` |
1264    ///            [`::`] `{` USE_TREE_LIST `}` |
1265    ///            PATH `::` `*` |
1266    ///            PATH `::` `{` USE_TREE_LIST `}` |
1267    ///            PATH [`as` IDENT]
1268    /// ```
1269    fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
1270        let lo = self.token.span;
1271
1272        let mut prefix =
1273            ast::Path { segments: ThinVec::new(), span: lo.shrink_to_lo(), tokens: None };
1274        let kind =
1275            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() {
1276                // `use *;` or `use ::*;` or `use {...};` or `use ::{...};`
1277                let mod_sep_ctxt = self.token.span.ctxt();
1278                if self.eat_path_sep() {
1279                    prefix
1280                        .segments
1281                        .push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
1282                }
1283
1284                self.parse_use_tree_glob_or_nested()?
1285            } else {
1286                // `use path::*;` or `use path::{...};` or `use path;` or `use path as bar;`
1287                prefix = self.parse_path(PathStyle::Mod)?;
1288
1289                if self.eat_path_sep() {
1290                    self.parse_use_tree_glob_or_nested()?
1291                } else {
1292                    // Recover from using a colon as path separator.
1293                    while self.eat_noexpect(&token::Colon) {
1294                        self.dcx()
1295                            .emit_err(errors::SingleColonImportPath { span: self.prev_token.span });
1296
1297                        // We parse the rest of the path and append it to the original prefix.
1298                        self.parse_path_segments(&mut prefix.segments, PathStyle::Mod, None)?;
1299                        prefix.span = lo.to(self.prev_token.span);
1300                    }
1301
1302                    UseTreeKind::Simple(self.parse_rename()?)
1303                }
1304            };
1305
1306        Ok(UseTree { prefix, kind, span: lo.to(self.prev_token.span) })
1307    }
1308
1309    /// Parses `*` or `{...}`.
1310    fn parse_use_tree_glob_or_nested(&mut self) -> PResult<'a, UseTreeKind> {
1311        Ok(if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Star,
    token_type: crate::parser::token_type::TokenType::Star,
}exp!(Star)) {
1312            UseTreeKind::Glob
1313        } else {
1314            let lo = self.token.span;
1315            UseTreeKind::Nested {
1316                items: self.parse_use_tree_list()?,
1317                span: lo.to(self.prev_token.span),
1318            }
1319        })
1320    }
1321
1322    /// Parses a `UseTreeKind::Nested(list)`.
1323    ///
1324    /// ```text
1325    /// USE_TREE_LIST = ∅ | (USE_TREE `,`)* USE_TREE [`,`]
1326    /// ```
1327    fn parse_use_tree_list(&mut self) -> PResult<'a, ThinVec<(UseTree, ast::NodeId)>> {
1328        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| {
1329            p.recover_vcs_conflict_marker();
1330            Ok((p.parse_use_tree()?, DUMMY_NODE_ID))
1331        })
1332        .map(|(r, _)| r)
1333    }
1334
1335    fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
1336        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)) {
1337            self.parse_ident_or_underscore().map(Some)
1338        } else {
1339            Ok(None)
1340        }
1341    }
1342
1343    fn parse_ident_or_underscore(&mut self) -> PResult<'a, Ident> {
1344        match self.token.ident() {
1345            Some((ident @ Ident { name: kw::Underscore, .. }, IdentIsRaw::No)) => {
1346                self.bump();
1347                Ok(ident)
1348            }
1349            _ => self.parse_ident(),
1350        }
1351    }
1352
1353    /// Parses `extern crate` links.
1354    ///
1355    /// # Examples
1356    ///
1357    /// ```ignore (illustrative)
1358    /// extern crate foo;
1359    /// extern crate bar as foo;
1360    /// ```
1361    fn parse_item_extern_crate(&mut self) -> PResult<'a, ItemKind> {
1362        // Accept `extern crate name-like-this` for better diagnostics
1363        let orig_ident = self.parse_crate_name_with_dashes()?;
1364        let (orig_name, item_ident) = if let Some(rename) = self.parse_rename()? {
1365            (Some(orig_ident.name), rename)
1366        } else {
1367            (None, orig_ident)
1368        };
1369        self.expect_semi()?;
1370        Ok(ItemKind::ExternCrate(orig_name, item_ident))
1371    }
1372
1373    fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, Ident> {
1374        let ident = if self.token.is_keyword(kw::SelfLower) {
1375            self.parse_path_segment_ident()
1376        } else {
1377            self.parse_ident()
1378        }?;
1379
1380        let dash = crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Minus,
    token_type: crate::parser::token_type::TokenType::Minus,
}exp!(Minus);
1381        if self.token != dash.tok {
1382            return Ok(ident);
1383        }
1384
1385        // Accept `extern crate name-like-this` for better diagnostics.
1386        let mut dashes = ::alloc::vec::Vec::new()vec![];
1387        let mut idents = ::alloc::vec::Vec::new()vec![];
1388        while self.eat(dash) {
1389            dashes.push(self.prev_token.span);
1390            idents.push(self.parse_ident()?);
1391        }
1392
1393        let fixed_name_sp = ident.span.to(idents.last().unwrap().span);
1394        let mut fixed_name = ident.name.to_string();
1395        for part in idents {
1396            fixed_name.write_fmt(format_args!("_{0}", part.name))write!(fixed_name, "_{}", part.name).unwrap();
1397        }
1398
1399        self.dcx().emit_err(errors::ExternCrateNameWithDashes {
1400            span: fixed_name_sp,
1401            sugg: errors::ExternCrateNameWithDashesSugg { dashes },
1402        });
1403
1404        Ok(Ident::from_str_and_span(&fixed_name, fixed_name_sp))
1405    }
1406
1407    /// Parses `extern` for foreign ABIs modules.
1408    ///
1409    /// `extern` is expected to have been consumed before calling this method.
1410    ///
1411    /// # Examples
1412    ///
1413    /// ```ignore (only-for-syntax-highlight)
1414    /// extern "C" {}
1415    /// extern {}
1416    /// ```
1417    fn parse_item_foreign_mod(
1418        &mut self,
1419        attrs: &mut AttrVec,
1420        mut safety: Safety,
1421    ) -> PResult<'a, ItemKind> {
1422        let extern_span = self.prev_token_uninterpolated_span();
1423        let abi = self.parse_abi(); // ABI?
1424        // FIXME: This recovery should be tested better.
1425        if safety == Safety::Default
1426            && self.token.is_keyword(kw::Unsafe)
1427            && self.look_ahead(1, |t| *t == token::OpenBrace)
1428        {
1429            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();
1430            safety = Safety::Unsafe(self.token.span);
1431            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));
1432        }
1433        Ok(ItemKind::ForeignMod(ast::ForeignMod {
1434            extern_span,
1435            safety,
1436            abi,
1437            items: self.parse_item_list(attrs, |p| p.parse_foreign_item(ForceCollect::No))?,
1438        }))
1439    }
1440
1441    /// Parses a foreign item (one in an `extern { ... }` block).
1442    pub fn parse_foreign_item(
1443        &mut self,
1444        force_collect: ForceCollect,
1445    ) -> PResult<'a, Option<Option<Box<ForeignItem>>>> {
1446        let fn_parse_mode = FnParseMode {
1447            req_name: |_, is_dot_dot_dot| is_dot_dot_dot == IsDotDotDot::No,
1448            context: FnContext::Free,
1449            req_body: false,
1450        };
1451        Ok(self
1452            .parse_item_(
1453                fn_parse_mode,
1454                force_collect,
1455                AllowConstBlockItems::DoesNotMatter, // due to `ForeignItemKind::try_from` below
1456            )?
1457            .map(|Item { attrs, id, span, vis, kind, tokens }| {
1458                let kind = match ForeignItemKind::try_from(kind) {
1459                    Ok(kind) => kind,
1460                    Err(kind) => match kind {
1461                        ItemKind::Const(box ConstItem { ident, ty, rhs_kind, .. }) => {
1462                            let const_span = Some(span.with_hi(ident.span.lo()))
1463                                .filter(|span| span.can_be_used_for_suggestions());
1464                            self.dcx().emit_err(errors::ExternItemCannotBeConst {
1465                                ident_span: ident.span,
1466                                const_span,
1467                            });
1468                            ForeignItemKind::Static(Box::new(StaticItem {
1469                                ident,
1470                                ty,
1471                                mutability: Mutability::Not,
1472                                expr: match rhs_kind {
1473                                    ConstItemRhsKind::Body { rhs } => rhs,
1474                                    ConstItemRhsKind::TypeConst { rhs: Some(anon) } => {
1475                                        Some(anon.value)
1476                                    }
1477                                    ConstItemRhsKind::TypeConst { rhs: None } => None,
1478                                },
1479                                safety: Safety::Default,
1480                                define_opaque: None,
1481                            }))
1482                        }
1483                        _ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
1484                    },
1485                };
1486                Some(Box::new(Item { attrs, id, span, vis, kind, tokens }))
1487            }))
1488    }
1489
1490    fn error_bad_item_kind<T>(&self, span: Span, kind: &ItemKind, ctx: &'static str) -> Option<T> {
1491        // FIXME(#100717): needs variant for each `ItemKind` (instead of using `ItemKind::descr()`)
1492        let span = self.psess.source_map().guess_head_span(span);
1493        let descr = kind.descr();
1494        let help = match kind {
1495            ItemKind::DelegationMac(deleg) if deleg.suffixes.is_none() => false,
1496            _ => true,
1497        };
1498        self.dcx().emit_err(errors::BadItemKind { span, descr, ctx, help });
1499        None
1500    }
1501
1502    fn is_use_closure(&self) -> bool {
1503        if self.token.is_keyword(kw::Use) {
1504            // Check if this could be a closure.
1505            self.look_ahead(1, |token| {
1506                // Move or Async here would be an error but still we're parsing a closure
1507                let dist =
1508                    if token.is_keyword(kw::Move) || token.is_keyword(kw::Async) { 2 } else { 1 };
1509
1510                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))
1511            })
1512        } else {
1513            false
1514        }
1515    }
1516
1517    fn is_unsafe_foreign_mod(&self) -> bool {
1518        // Look for `unsafe`.
1519        if !self.token.is_keyword(kw::Unsafe) {
1520            return false;
1521        }
1522        // Look for `extern`.
1523        if !self.is_keyword_ahead(1, &[kw::Extern]) {
1524            return false;
1525        }
1526
1527        // Look for the optional ABI string literal.
1528        let n = if self.look_ahead(2, |t| t.can_begin_string_literal()) { 3 } else { 2 };
1529
1530        // Look for the `{`. Use `tree_look_ahead` because the ABI (if present)
1531        // might be a metavariable i.e. an invisible-delimited sequence, and
1532        // `tree_look_ahead` will consider that a single element when looking
1533        // ahead.
1534        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, _)))
1535            == Some(true)
1536    }
1537
1538    fn parse_global_static_front_matter(&mut self, case: Case) -> Option<Safety> {
1539        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) {
1540            // Check if this could be a closure.
1541            !self.look_ahead(1, |token| {
1542                if token.is_keyword_case(kw::Move, case) || token.is_keyword_case(kw::Use, case) {
1543                    return true;
1544                }
1545                #[allow(non_exhaustive_omitted_patterns)] match token.kind {
    token::Or | token::OrOr => true,
    _ => false,
}matches!(token.kind, token::Or | token::OrOr)
1546            })
1547        } else {
1548            // `$qual static`
1549            (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)
1550                || 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))
1551                && self.look_ahead(1, |t| t.is_keyword_case(kw::Static, case))
1552        };
1553
1554        if is_global_static {
1555            let safety = self.parse_safety(case);
1556            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);
1557            Some(safety)
1558        } else {
1559            None
1560        }
1561    }
1562
1563    /// Recover on `const mut` with `const` already eaten.
1564    fn recover_const_mut(&mut self, const_span: Span) {
1565        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)) {
1566            let span = self.prev_token.span;
1567            self.dcx()
1568                .emit_err(errors::ConstGlobalCannotBeMutable { ident_span: span, const_span });
1569        } 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)) {
1570            let span = self.prev_token.span;
1571            self.dcx().emit_err(errors::ConstLetMutuallyExclusive { span: const_span.to(span) });
1572        }
1573    }
1574
1575    fn parse_const_block_item(&mut self) -> PResult<'a, ConstBlockItem> {
1576        self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Const,
    token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const))?;
1577        let const_span = self.prev_token.span;
1578        self.psess.gated_spans.gate(sym::const_block_items, const_span);
1579        let block = self.parse_block()?;
1580        Ok(ConstBlockItem { id: DUMMY_NODE_ID, span: const_span.to(block.span), block })
1581    }
1582
1583    /// Parse a static item with the prefix `"static" "mut"?` already parsed and stored in
1584    /// `mutability`.
1585    ///
1586    /// ```ebnf
1587    /// Static = "static" "mut"? $ident ":" $ty (= $expr)? ";" ;
1588    /// ```
1589    fn parse_static_item(
1590        &mut self,
1591        safety: Safety,
1592        mutability: Mutability,
1593    ) -> PResult<'a, ItemKind> {
1594        let ident = self.parse_ident()?;
1595
1596        if self.token == TokenKind::Lt && self.may_recover() {
1597            let generics = self.parse_generics()?;
1598            self.dcx().emit_err(errors::StaticWithGenerics { span: generics.span });
1599        }
1600
1601        // Parse the type of a static item. That is, the `":" $ty` fragment.
1602        // FIXME: This could maybe benefit from `.may_recover()`?
1603        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))) {
1604            (true, false) => self.parse_ty()?,
1605            // If there wasn't a `:` or the colon was followed by a `=` or `;`, recover a missing
1606            // type.
1607            (colon, _) => self.recover_missing_global_item_type(colon, Some(mutability)),
1608        };
1609
1610        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 };
1611
1612        self.expect_semi()?;
1613
1614        let item = StaticItem { ident, ty, safety, mutability, expr, define_opaque: None };
1615        Ok(ItemKind::Static(Box::new(item)))
1616    }
1617
1618    /// Parse a constant item with the prefix `"const"` already parsed.
1619    ///
1620    /// If `const_arg` is true, any expression assigned to the const will be parsed
1621    /// as a const_arg instead of a body expression.
1622    ///
1623    /// ```ebnf
1624    /// Const = "const" ($ident | "_") Generics ":" $ty (= $expr)? WhereClause ";" ;
1625    /// ```
1626    fn parse_const_item(
1627        &mut self,
1628        const_arg: bool,
1629        const_span: Span,
1630    ) -> PResult<'a, (Ident, Generics, Box<Ty>, ConstItemRhsKind)> {
1631        let ident = self.parse_ident_or_underscore()?;
1632
1633        let mut generics = self.parse_generics()?;
1634
1635        // Check the span for emptiness instead of the list of parameters in order to correctly
1636        // recognize and subsequently flag empty parameter lists (`<>`) as unstable.
1637        if !generics.span.is_empty() {
1638            self.psess.gated_spans.gate(sym::generic_const_items, generics.span);
1639        }
1640
1641        // Parse the type of a constant item. That is, the `":" $ty` fragment.
1642        // FIXME: This could maybe benefit from `.may_recover()`?
1643        let ty = match (
1644            self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Colon,
    token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)),
1645            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)),
1646        ) {
1647            (true, false) => self.parse_ty()?,
1648            // If there wasn't a `:` or the colon was followed by a `=`, `;` or `where`, recover a missing type.
1649            (colon, _) => self.recover_missing_global_item_type(colon, None),
1650        };
1651
1652        // Proactively parse a where-clause to be able to provide a good error message in case we
1653        // encounter the item body following it.
1654        let before_where_clause =
1655            if self.may_recover() { self.parse_where_clause()? } else { WhereClause::default() };
1656
1657        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) {
1658            (true, true) => ConstItemRhsKind::TypeConst {
1659                rhs: Some(self.parse_expr_anon_const(|_, _| MgcaDisambiguation::Direct)?),
1660            },
1661            (true, false) => ConstItemRhsKind::Body { rhs: Some(self.parse_expr()?) },
1662            (false, true) => ConstItemRhsKind::TypeConst { rhs: None },
1663            (false, false) => ConstItemRhsKind::Body { rhs: None },
1664        };
1665
1666        let after_where_clause = self.parse_where_clause()?;
1667
1668        // Provide a nice error message if the user placed a where-clause before the item body.
1669        // Users may be tempted to write such code if they are still used to the deprecated
1670        // where-clause location on type aliases and associated types. See also #89122.
1671        if before_where_clause.has_where_token
1672            && let Some(rhs_span) = rhs.span()
1673        {
1674            self.dcx().emit_err(errors::WhereClauseBeforeConstBody {
1675                span: before_where_clause.span,
1676                name: ident.span,
1677                body: rhs_span,
1678                sugg: if !after_where_clause.has_where_token {
1679                    self.psess.source_map().span_to_snippet(rhs_span).ok().map(|body_s| {
1680                        errors::WhereClauseBeforeConstBodySugg {
1681                            left: before_where_clause.span.shrink_to_lo(),
1682                            snippet: body_s,
1683                            right: before_where_clause.span.shrink_to_hi().to(rhs_span),
1684                        }
1685                    })
1686                } else {
1687                    // FIXME(generic_const_items): Provide a structured suggestion to merge the first
1688                    // where-clause into the second one.
1689                    None
1690                },
1691            });
1692        }
1693
1694        // Merge the predicates of both where-clauses since either one can be relevant.
1695        // If we didn't parse a body (which is valid for associated consts in traits) and we were
1696        // allowed to recover, `before_where_clause` contains the predicates, otherwise they are
1697        // in `after_where_clause`. Further, both of them might contain predicates iff two
1698        // where-clauses were provided which is syntactically ill-formed but we want to recover from
1699        // it and treat them as one large where-clause.
1700        let mut predicates = before_where_clause.predicates;
1701        predicates.extend(after_where_clause.predicates);
1702        let where_clause = WhereClause {
1703            has_where_token: before_where_clause.has_where_token
1704                || after_where_clause.has_where_token,
1705            predicates,
1706            span: if after_where_clause.has_where_token {
1707                after_where_clause.span
1708            } else {
1709                before_where_clause.span
1710            },
1711        };
1712
1713        if where_clause.has_where_token {
1714            self.psess.gated_spans.gate(sym::generic_const_items, where_clause.span);
1715        }
1716
1717        generics.where_clause = where_clause;
1718
1719        if let Some(rhs) = self.try_recover_const_missing_semi(&rhs, const_span) {
1720            return Ok((ident, generics, ty, ConstItemRhsKind::Body { rhs: Some(rhs) }));
1721        }
1722        self.expect_semi()?;
1723
1724        Ok((ident, generics, ty, rhs))
1725    }
1726
1727    /// We were supposed to parse `":" $ty` but the `:` or the type was missing.
1728    /// This means that the type is missing.
1729    fn recover_missing_global_item_type(
1730        &mut self,
1731        colon_present: bool,
1732        m: Option<Mutability>,
1733    ) -> Box<Ty> {
1734        // Construct the error and stash it away with the hope
1735        // that typeck will later enrich the error with a type.
1736        let kind = match m {
1737            Some(Mutability::Mut) => "static mut",
1738            Some(Mutability::Not) => "static",
1739            None => "const",
1740        };
1741
1742        let colon = match colon_present {
1743            true => "",
1744            false => ":",
1745        };
1746
1747        let span = self.prev_token.span.shrink_to_hi();
1748        let err = self.dcx().create_err(errors::MissingConstType { span, colon, kind });
1749        err.stash(span, StashKey::ItemNoType);
1750
1751        // The user intended that the type be inferred,
1752        // so treat this as if the user wrote e.g. `const A: _ = expr;`.
1753        Box::new(Ty { kind: TyKind::Infer, span, id: ast::DUMMY_NODE_ID, tokens: None })
1754    }
1755
1756    /// Parses an enum declaration.
1757    fn parse_item_enum(&mut self) -> PResult<'a, ItemKind> {
1758        if self.token.is_keyword(kw::Struct) {
1759            let span = self.prev_token.span.to(self.token.span);
1760            let err = errors::EnumStructMutuallyExclusive { span };
1761            if self.look_ahead(1, |t| t.is_ident()) {
1762                self.bump();
1763                self.dcx().emit_err(err);
1764            } else {
1765                return Err(self.dcx().create_err(err));
1766            }
1767        }
1768
1769        let prev_span = self.prev_token.span;
1770        let ident = self.parse_ident()?;
1771        let mut generics = self.parse_generics()?;
1772        generics.where_clause = self.parse_where_clause()?;
1773
1774        // Possibly recover `enum Foo;` instead of `enum Foo {}`
1775        let (variants, _) = if self.token == TokenKind::Semi {
1776            self.dcx().emit_err(errors::UseEmptyBlockNotSemi { span: self.token.span });
1777            self.bump();
1778            (::thin_vec::ThinVec::new()thin_vec![], Trailing::No)
1779        } else {
1780            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| {
1781                p.parse_enum_variant(ident.span)
1782            })
1783            .map_err(|mut err| {
1784                err.span_label(ident.span, "while parsing this enum");
1785                // Try to recover `enum Foo { ident : Ty }`.
1786                if self.prev_token.is_non_reserved_ident() && self.token == token::Colon {
1787                    let snapshot = self.create_snapshot_for_diagnostic();
1788                    self.bump();
1789                    match self.parse_ty() {
1790                        Ok(_) => {
1791                            err.span_suggestion_verbose(
1792                                prev_span,
1793                                "perhaps you meant to use `struct` here",
1794                                "struct",
1795                                Applicability::MaybeIncorrect,
1796                            );
1797                        }
1798                        Err(e) => {
1799                            e.cancel();
1800                        }
1801                    }
1802                    self.restore_snapshot(snapshot);
1803                }
1804                self.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseBrace,
    token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace)]);
1805                self.bump(); // }
1806                err
1807            })?
1808        };
1809
1810        let enum_definition = EnumDef { variants: variants.into_iter().flatten().collect() };
1811        Ok(ItemKind::Enum(ident, generics, enum_definition))
1812    }
1813
1814    fn parse_enum_variant(&mut self, span: Span) -> PResult<'a, Option<Variant>> {
1815        self.recover_vcs_conflict_marker();
1816        let variant_attrs = self.parse_outer_attributes()?;
1817        self.recover_vcs_conflict_marker();
1818        let help = "enum variants can be `Variant`, `Variant = <integer>`, \
1819                    `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`";
1820        self.collect_tokens(None, variant_attrs, ForceCollect::No, |this, variant_attrs| {
1821            let vlo = this.token.span;
1822
1823            let vis = this.parse_visibility(FollowedByType::No)?;
1824            if !this.recover_nested_adt_item(kw::Enum)? {
1825                return Ok((None, Trailing::No, UsePreAttrPos::No));
1826            }
1827            let ident = this.parse_field_ident("enum", vlo)?;
1828
1829            if this.token == token::Bang {
1830                if let Err(err) = this.unexpected() {
1831                    err.with_note(rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("macros cannot expand to enum variants"))msg!("macros cannot expand to enum variants")).emit();
1832                }
1833
1834                this.bump();
1835                this.parse_delim_args()?;
1836
1837                return Ok((None, Trailing::from(this.token == token::Comma), UsePreAttrPos::No));
1838            }
1839
1840            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)) {
1841                // Parse a struct variant.
1842                let (fields, recovered) =
1843                    match this.parse_record_struct_body("struct", ident.span, false) {
1844                        Ok((fields, recovered)) => (fields, recovered),
1845                        Err(mut err) => {
1846                            if this.token == token::Colon {
1847                                // We handle `enum` to `struct` suggestion in the caller.
1848                                return Err(err);
1849                            }
1850                            this.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseBrace,
    token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace)]);
1851                            this.bump(); // }
1852                            err.span_label(span, "while parsing this enum");
1853                            err.help(help);
1854                            let guar = err.emit();
1855                            (::thin_vec::ThinVec::new()thin_vec![], Recovered::Yes(guar))
1856                        }
1857                    };
1858                VariantData::Struct { fields, recovered }
1859            } else if this.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenParen,
    token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
1860                let body = match this.parse_tuple_struct_body() {
1861                    Ok(body) => body,
1862                    Err(mut err) => {
1863                        if this.token == token::Colon {
1864                            // We handle `enum` to `struct` suggestion in the caller.
1865                            return Err(err);
1866                        }
1867                        this.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseParen,
    token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen)]);
1868                        this.bump(); // )
1869                        err.span_label(span, "while parsing this enum");
1870                        err.help(help);
1871                        err.emit();
1872                        ::thin_vec::ThinVec::new()thin_vec![]
1873                    }
1874                };
1875                VariantData::Tuple(body, DUMMY_NODE_ID)
1876            } else {
1877                VariantData::Unit(DUMMY_NODE_ID)
1878            };
1879
1880            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)) {
1881                Some(this.parse_expr_anon_const(|_, _| MgcaDisambiguation::AnonConst)?)
1882            } else {
1883                None
1884            };
1885
1886            let vr = ast::Variant {
1887                ident,
1888                vis,
1889                id: DUMMY_NODE_ID,
1890                attrs: variant_attrs,
1891                data: struct_def,
1892                disr_expr,
1893                span: vlo.to(this.prev_token.span),
1894                is_placeholder: false,
1895            };
1896
1897            Ok((Some(vr), Trailing::from(this.token == token::Comma), UsePreAttrPos::No))
1898        })
1899        .map_err(|mut err| {
1900            err.help(help);
1901            err
1902        })
1903    }
1904
1905    /// Parses `struct Foo { ... }`.
1906    fn parse_item_struct(&mut self) -> PResult<'a, ItemKind> {
1907        let ident = self.parse_ident()?;
1908
1909        let mut generics = self.parse_generics()?;
1910
1911        // There is a special case worth noting here, as reported in issue #17904.
1912        // If we are parsing a tuple struct it is the case that the where clause
1913        // should follow the field list. Like so:
1914        //
1915        // struct Foo<T>(T) where T: Copy;
1916        //
1917        // If we are parsing a normal record-style struct it is the case
1918        // that the where clause comes before the body, and after the generics.
1919        // So if we look ahead and see a brace or a where-clause we begin
1920        // parsing a record style struct.
1921        //
1922        // Otherwise if we look ahead and see a paren we parse a tuple-style
1923        // struct.
1924
1925        let vdata = if self.token.is_keyword(kw::Where) {
1926            let tuple_struct_body;
1927            (generics.where_clause, tuple_struct_body) =
1928                self.parse_struct_where_clause(ident, generics.span)?;
1929
1930            if let Some(body) = tuple_struct_body {
1931                // If we see a misplaced tuple struct body: `struct Foo<T> where T: Copy, (T);`
1932                let body = VariantData::Tuple(body, DUMMY_NODE_ID);
1933                self.expect_semi()?;
1934                body
1935            } else if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Semi,
    token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
1936                // If we see a: `struct Foo<T> where T: Copy;` style decl.
1937                VariantData::Unit(DUMMY_NODE_ID)
1938            } else {
1939                // If we see: `struct Foo<T> where T: Copy { ... }`
1940                let (fields, recovered) = self.parse_record_struct_body(
1941                    "struct",
1942                    ident.span,
1943                    generics.where_clause.has_where_token,
1944                )?;
1945                VariantData::Struct { fields, recovered }
1946            }
1947        // No `where` so: `struct Foo<T>;`
1948        } else if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Semi,
    token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
1949            VariantData::Unit(DUMMY_NODE_ID)
1950        // Record-style struct definition
1951        } else if self.token == token::OpenBrace {
1952            let (fields, recovered) = self.parse_record_struct_body(
1953                "struct",
1954                ident.span,
1955                generics.where_clause.has_where_token,
1956            )?;
1957            VariantData::Struct { fields, recovered }
1958        // Tuple-style struct definition with optional where-clause.
1959        } else if self.token == token::OpenParen {
1960            let body = VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID);
1961            generics.where_clause = self.parse_where_clause()?;
1962            self.expect_semi()?;
1963            body
1964        } else {
1965            let err = errors::UnexpectedTokenAfterStructName::new(self.token.span, self.token);
1966            return Err(self.dcx().create_err(err));
1967        };
1968
1969        Ok(ItemKind::Struct(ident, generics, vdata))
1970    }
1971
1972    /// Parses `union Foo { ... }`.
1973    fn parse_item_union(&mut self) -> PResult<'a, ItemKind> {
1974        let ident = self.parse_ident()?;
1975
1976        let mut generics = self.parse_generics()?;
1977
1978        let vdata = if self.token.is_keyword(kw::Where) {
1979            generics.where_clause = self.parse_where_clause()?;
1980            let (fields, recovered) = self.parse_record_struct_body(
1981                "union",
1982                ident.span,
1983                generics.where_clause.has_where_token,
1984            )?;
1985            VariantData::Struct { fields, recovered }
1986        } else if self.token == token::OpenBrace {
1987            let (fields, recovered) = self.parse_record_struct_body(
1988                "union",
1989                ident.span,
1990                generics.where_clause.has_where_token,
1991            )?;
1992            VariantData::Struct { fields, recovered }
1993        } else {
1994            let token_str = super::token_descr(&self.token);
1995            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}");
1996            let mut err = self.dcx().struct_span_err(self.token.span, msg);
1997            err.span_label(self.token.span, "expected `where` or `{` after union name");
1998            return Err(err);
1999        };
2000
2001        Ok(ItemKind::Union(ident, generics, vdata))
2002    }
2003
2004    /// This function parses the fields of record structs:
2005    ///
2006    ///   - `struct S { ... }`
2007    ///   - `enum E { Variant { ... } }`
2008    pub(crate) fn parse_record_struct_body(
2009        &mut self,
2010        adt_ty: &str,
2011        ident_span: Span,
2012        parsed_where: bool,
2013    ) -> PResult<'a, (ThinVec<FieldDef>, Recovered)> {
2014        let mut fields = ThinVec::new();
2015        let mut recovered = Recovered::No;
2016        if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenBrace,
    token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
2017            while self.token != token::CloseBrace {
2018                match self.parse_field_def(adt_ty, ident_span) {
2019                    Ok(field) => {
2020                        fields.push(field);
2021                    }
2022                    Err(mut err) => {
2023                        self.consume_block(
2024                            crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenBrace,
    token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace),
2025                            crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseBrace,
    token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace),
2026                            ConsumeClosingDelim::No,
2027                        );
2028                        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}"));
2029                        let guar = err.emit();
2030                        recovered = Recovered::Yes(guar);
2031                        break;
2032                    }
2033                }
2034            }
2035            self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseBrace,
    token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace))?;
2036        } else {
2037            let token_str = super::token_descr(&self.token);
2038            let where_str = if parsed_where { "" } else { "`where`, or " };
2039            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}");
2040            let mut err = self.dcx().struct_span_err(self.token.span, msg);
2041            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",));
2042            return Err(err);
2043        }
2044
2045        Ok((fields, recovered))
2046    }
2047
2048    fn parse_unsafe_field(&mut self) -> Safety {
2049        // not using parse_safety as that also accepts `safe`.
2050        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)) {
2051            let span = self.prev_token.span;
2052            self.psess.gated_spans.gate(sym::unsafe_fields, span);
2053            Safety::Unsafe(span)
2054        } else {
2055            Safety::Default
2056        }
2057    }
2058
2059    pub(super) fn parse_tuple_struct_body(&mut self) -> PResult<'a, ThinVec<FieldDef>> {
2060        // This is the case where we find `struct Foo<T>(T) where T: Copy;`
2061        // Unit like structs are handled in parse_item_struct function
2062        self.parse_paren_comma_seq(|p| {
2063            let attrs = p.parse_outer_attributes()?;
2064            p.collect_tokens(None, attrs, ForceCollect::No, |p, attrs| {
2065                let mut snapshot = None;
2066                if p.is_vcs_conflict_marker(&TokenKind::Shl, &TokenKind::Lt) {
2067                    // Account for `<<<<<<<` diff markers. We can't proactively error here because
2068                    // that can be a valid type start, so we snapshot and reparse only we've
2069                    // encountered another parse error.
2070                    snapshot = Some(p.create_snapshot_for_diagnostic());
2071                }
2072                let lo = p.token.span;
2073                let vis = match p.parse_visibility(FollowedByType::Yes) {
2074                    Ok(vis) => vis,
2075                    Err(err) => {
2076                        if let Some(ref mut snapshot) = snapshot {
2077                            snapshot.recover_vcs_conflict_marker();
2078                        }
2079                        return Err(err);
2080                    }
2081                };
2082                // Unsafe fields are not supported in tuple structs, as doing so would result in a
2083                // parsing ambiguity for `struct X(unsafe fn())`.
2084                let ty = match p.parse_ty() {
2085                    Ok(ty) => ty,
2086                    Err(err) => {
2087                        if let Some(ref mut snapshot) = snapshot {
2088                            snapshot.recover_vcs_conflict_marker();
2089                        }
2090                        return Err(err);
2091                    }
2092                };
2093                let mut default = None;
2094                if p.token == token::Eq {
2095                    let mut snapshot = p.create_snapshot_for_diagnostic();
2096                    snapshot.bump();
2097                    match snapshot.parse_expr_anon_const(|_, _| MgcaDisambiguation::AnonConst) {
2098                        Ok(const_expr) => {
2099                            let sp = ty.span.shrink_to_hi().to(const_expr.value.span);
2100                            p.psess.gated_spans.gate(sym::default_field_values, sp);
2101                            p.restore_snapshot(snapshot);
2102                            default = Some(const_expr);
2103                        }
2104                        Err(err) => {
2105                            err.cancel();
2106                        }
2107                    }
2108                }
2109
2110                Ok((
2111                    FieldDef {
2112                        span: lo.to(ty.span),
2113                        vis,
2114                        safety: Safety::Default,
2115                        ident: None,
2116                        id: DUMMY_NODE_ID,
2117                        ty,
2118                        default,
2119                        attrs,
2120                        is_placeholder: false,
2121                    },
2122                    Trailing::from(p.token == token::Comma),
2123                    UsePreAttrPos::No,
2124                ))
2125            })
2126        })
2127        .map(|(r, _)| r)
2128    }
2129
2130    /// Parses an element of a struct declaration.
2131    fn parse_field_def(&mut self, adt_ty: &str, ident_span: Span) -> PResult<'a, FieldDef> {
2132        self.recover_vcs_conflict_marker();
2133        let attrs = self.parse_outer_attributes()?;
2134        self.recover_vcs_conflict_marker();
2135        self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
2136            let lo = this.token.span;
2137            let vis = this.parse_visibility(FollowedByType::No)?;
2138            let safety = this.parse_unsafe_field();
2139            this.parse_single_struct_field(adt_ty, lo, vis, safety, attrs, ident_span)
2140                .map(|field| (field, Trailing::No, UsePreAttrPos::No))
2141        })
2142    }
2143
2144    /// Parses a structure field declaration.
2145    fn parse_single_struct_field(
2146        &mut self,
2147        adt_ty: &str,
2148        lo: Span,
2149        vis: Visibility,
2150        safety: Safety,
2151        attrs: AttrVec,
2152        ident_span: Span,
2153    ) -> PResult<'a, FieldDef> {
2154        let a_var = self.parse_name_and_ty(adt_ty, lo, vis, safety, attrs)?;
2155        match self.token.kind {
2156            token::Comma => {
2157                self.bump();
2158            }
2159            token::Semi => {
2160                self.bump();
2161                let sp = self.prev_token.span;
2162                let mut err =
2163                    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 `,`"));
2164                err.span_suggestion_short(
2165                    sp,
2166                    "replace `;` with `,`",
2167                    ",",
2168                    Applicability::MachineApplicable,
2169                );
2170                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}"));
2171                err.emit();
2172            }
2173            token::CloseBrace => {}
2174            token::DocComment(..) => {
2175                let previous_span = self.prev_token.span;
2176                let mut err = errors::DocCommentDoesNotDocumentAnything {
2177                    span: self.token.span,
2178                    missing_comma: None,
2179                };
2180                self.bump(); // consume the doc comment
2181                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 {
2182                    self.dcx().emit_err(err);
2183                } else {
2184                    let sp = previous_span.shrink_to_hi();
2185                    err.missing_comma = Some(sp);
2186                    return Err(self.dcx().create_err(err));
2187                }
2188            }
2189            _ => {
2190                let sp = self.prev_token.span.shrink_to_hi();
2191                let msg =
2192                    ::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));
2193
2194                // Try to recover extra trailing angle brackets
2195                if let TyKind::Path(_, Path { segments, .. }) = &a_var.ty.kind
2196                    && let Some(last_segment) = segments.last()
2197                {
2198                    let guar = self.check_trailing_angle_brackets(
2199                        last_segment,
2200                        &[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)],
2201                    );
2202                    if let Some(_guar) = guar {
2203                        // Handle a case like `Vec<u8>>,` where we can continue parsing fields
2204                        // after the comma
2205                        let _ = self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma));
2206
2207                        // `check_trailing_angle_brackets` already emitted a nicer error, as
2208                        // proven by the presence of `_guar`. We can continue parsing.
2209                        return Ok(a_var);
2210                    }
2211                }
2212
2213                let mut err = self.dcx().struct_span_err(sp, msg);
2214
2215                if self.token.is_ident()
2216                    || (self.token == TokenKind::Pound
2217                        && (self.look_ahead(1, |t| t == &token::OpenBracket)))
2218                {
2219                    // This is likely another field, TokenKind::Pound is used for `#[..]`
2220                    // attribute for next field. Emit the diagnostic and continue parsing.
2221                    err.span_suggestion(
2222                        sp,
2223                        "try adding a comma",
2224                        ",",
2225                        Applicability::MachineApplicable,
2226                    );
2227                    err.emit();
2228                } else {
2229                    return Err(err);
2230                }
2231            }
2232        }
2233        Ok(a_var)
2234    }
2235
2236    fn expect_field_ty_separator(&mut self) -> PResult<'a, ()> {
2237        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)) {
2238            let sm = self.psess.source_map();
2239            let eq_typo = self.token == token::Eq && self.look_ahead(1, |t| t.is_path_start());
2240            let semi_typo = self.token == token::Semi
2241                && self.look_ahead(1, |t| {
2242                    t.is_path_start()
2243                    // We check that we are in a situation like `foo; bar` to avoid bad suggestions
2244                    // when there's no type and `;` was used instead of a comma.
2245                    && match (sm.lookup_line(self.token.span.hi()), sm.lookup_line(t.span.lo())) {
2246                        (Ok(l), Ok(r)) => l.line == r.line,
2247                        _ => true,
2248                    }
2249                });
2250            if eq_typo || semi_typo {
2251                self.bump();
2252                // Gracefully handle small typos.
2253                err.with_span_suggestion_short(
2254                    self.prev_token.span,
2255                    "field names and their types are separated with `:`",
2256                    ":",
2257                    Applicability::MachineApplicable,
2258                )
2259                .emit();
2260            } else {
2261                return Err(err);
2262            }
2263        }
2264        Ok(())
2265    }
2266
2267    /// Parses a structure field.
2268    fn parse_name_and_ty(
2269        &mut self,
2270        adt_ty: &str,
2271        lo: Span,
2272        vis: Visibility,
2273        safety: Safety,
2274        attrs: AttrVec,
2275    ) -> PResult<'a, FieldDef> {
2276        let name = self.parse_field_ident(adt_ty, lo)?;
2277        if self.token == token::Bang {
2278            if let Err(mut err) = self.unexpected() {
2279                // Encounter the macro invocation
2280                err.subdiagnostic(MacroExpandsToAdtField { adt_ty });
2281                return Err(err);
2282            }
2283        }
2284        self.expect_field_ty_separator()?;
2285        let ty = self.parse_ty()?;
2286        if self.token == token::Colon && self.look_ahead(1, |&t| t != token::Colon) {
2287            self.dcx()
2288                .struct_span_err(self.token.span, "found single colon in a struct field type path")
2289                .with_span_suggestion_verbose(
2290                    self.token.span,
2291                    "write a path separator here",
2292                    "::",
2293                    Applicability::MaybeIncorrect,
2294                )
2295                .emit();
2296        }
2297        let default = if self.token == token::Eq {
2298            self.bump();
2299            let const_expr = self.parse_expr_anon_const(|_, _| MgcaDisambiguation::AnonConst)?;
2300            let sp = ty.span.shrink_to_hi().to(const_expr.value.span);
2301            self.psess.gated_spans.gate(sym::default_field_values, sp);
2302            Some(const_expr)
2303        } else {
2304            None
2305        };
2306        Ok(FieldDef {
2307            span: lo.to(self.prev_token.span),
2308            ident: Some(name),
2309            vis,
2310            safety,
2311            id: DUMMY_NODE_ID,
2312            ty,
2313            default,
2314            attrs,
2315            is_placeholder: false,
2316        })
2317    }
2318
2319    /// Parses a field identifier. Specialized version of `parse_ident_common`
2320    /// for better diagnostics and suggestions.
2321    fn parse_field_ident(&mut self, adt_ty: &str, lo: Span) -> PResult<'a, Ident> {
2322        let (ident, is_raw) = self.ident_or_err(true)?;
2323        if is_raw == IdentIsRaw::No && ident.is_reserved() {
2324            let snapshot = self.create_snapshot_for_diagnostic();
2325            let err = if self.check_fn_front_matter(false, Case::Sensitive) {
2326                let inherited_vis =
2327                    Visibility { span: DUMMY_SP, kind: VisibilityKind::Inherited, tokens: None };
2328                // We use `parse_fn` to get a span for the function
2329                let fn_parse_mode =
2330                    FnParseMode { req_name: |_, _| true, context: FnContext::Free, req_body: true };
2331                match self.parse_fn(
2332                    &mut AttrVec::new(),
2333                    fn_parse_mode,
2334                    lo,
2335                    &inherited_vis,
2336                    Case::Insensitive,
2337                ) {
2338                    Ok(_) => {
2339                        self.dcx().struct_span_err(
2340                            lo.to(self.prev_token.span),
2341                            ::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"),
2342                        )
2343                        .with_help(
2344                            "unlike in C++, Java, and C#, functions are declared in `impl` blocks",
2345                        )
2346                        .with_help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information")
2347                    }
2348                    Err(err) => {
2349                        err.cancel();
2350                        self.restore_snapshot(snapshot);
2351                        self.expected_ident_found_err()
2352                    }
2353                }
2354            } 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)) {
2355                match self.parse_item_struct() {
2356                    Ok(item) => {
2357                        let ItemKind::Struct(ident, ..) = item else { ::core::panicking::panic("internal error: entered unreachable code")unreachable!() };
2358                        self.dcx()
2359                            .struct_span_err(
2360                                lo.with_hi(ident.span.hi()),
2361                                ::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"),
2362                            )
2363                            .with_help(
2364                                "consider creating a new `struct` definition instead of nesting",
2365                            )
2366                    }
2367                    Err(err) => {
2368                        err.cancel();
2369                        self.restore_snapshot(snapshot);
2370                        self.expected_ident_found_err()
2371                    }
2372                }
2373            } else {
2374                let mut err = self.expected_ident_found_err();
2375                if self.eat_keyword_noexpect(kw::Let)
2376                    && let removal_span = self.prev_token.span.until(self.token.span)
2377                    && let Ok(ident) = self
2378                        .parse_ident_common(false)
2379                        // Cancel this error, we don't need it.
2380                        .map_err(|err| err.cancel())
2381                    && self.token == TokenKind::Colon
2382                {
2383                    err.span_suggestion(
2384                        removal_span,
2385                        "remove this `let` keyword",
2386                        String::new(),
2387                        Applicability::MachineApplicable,
2388                    );
2389                    err.note("the `let` keyword is not allowed in `struct` fields");
2390                    err.note("see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information");
2391                    err.emit();
2392                    return Ok(ident);
2393                } else {
2394                    self.restore_snapshot(snapshot);
2395                }
2396                err
2397            };
2398            return Err(err);
2399        }
2400        self.bump();
2401        Ok(ident)
2402    }
2403
2404    /// Parses a declarative macro 2.0 definition.
2405    /// The `macro` keyword has already been parsed.
2406    /// ```ebnf
2407    /// MacBody = "{" TOKEN_STREAM "}" ;
2408    /// MacParams = "(" TOKEN_STREAM ")" ;
2409    /// DeclMac = "macro" Ident MacParams? MacBody ;
2410    /// ```
2411    fn parse_item_decl_macro(&mut self, lo: Span) -> PResult<'a, ItemKind> {
2412        let ident = self.parse_ident()?;
2413        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)) {
2414            self.parse_delim_args()? // `MacBody`
2415        } else if self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenParen,
    token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
2416            let params = self.parse_token_tree(); // `MacParams`
2417            let pspan = params.span();
2418            if !self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenBrace,
    token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
2419                self.unexpected()?;
2420            }
2421            let body = self.parse_token_tree(); // `MacBody`
2422            // Convert `MacParams MacBody` into `{ MacParams => MacBody }`.
2423            let bspan = body.span();
2424            let arrow = TokenTree::token_alone(token::FatArrow, pspan.between(bspan)); // `=>`
2425            let tokens = TokenStream::new(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [params, arrow, body]))vec![params, arrow, body]);
2426            let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
2427            Box::new(DelimArgs { dspan, delim: Delimiter::Brace, tokens })
2428        } else {
2429            self.unexpected_any()?
2430        };
2431
2432        self.psess.gated_spans.gate(sym::decl_macro, lo.to(self.prev_token.span));
2433        Ok(ItemKind::MacroDef(
2434            ident,
2435            ast::MacroDef { body, macro_rules: false, eii_declaration: None },
2436        ))
2437    }
2438
2439    /// Is this a possibly malformed start of a `macro_rules! foo` item definition?
2440    fn is_macro_rules_item(&mut self) -> IsMacroRulesItem {
2441        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)) {
2442            let macro_rules_span = self.token.span;
2443
2444            if self.look_ahead(1, |t| *t == token::Bang) && self.look_ahead(2, |t| t.is_ident()) {
2445                return IsMacroRulesItem::Yes { has_bang: true };
2446            } else if self.look_ahead(1, |t| t.is_ident()) {
2447                // macro_rules foo
2448                self.dcx().emit_err(errors::MacroRulesMissingBang {
2449                    span: macro_rules_span,
2450                    hi: macro_rules_span.shrink_to_hi(),
2451                });
2452
2453                return IsMacroRulesItem::Yes { has_bang: false };
2454            }
2455        }
2456
2457        IsMacroRulesItem::No
2458    }
2459
2460    /// Parses a `macro_rules! foo { ... }` declarative macro.
2461    fn parse_item_macro_rules(
2462        &mut self,
2463        vis: &Visibility,
2464        has_bang: bool,
2465    ) -> PResult<'a, ItemKind> {
2466        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`
2467
2468        if has_bang {
2469            self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Bang,
    token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang))?; // `!`
2470        }
2471        let ident = self.parse_ident()?;
2472
2473        if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Bang,
    token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang)) {
2474            // Handle macro_rules! foo!
2475            let span = self.prev_token.span;
2476            self.dcx().emit_err(errors::MacroNameRemoveBang { span });
2477        }
2478
2479        let body = self.parse_delim_args()?;
2480        self.eat_semi_for_macro_if_needed(&body, None);
2481        self.complain_if_pub_macro(vis, true);
2482
2483        Ok(ItemKind::MacroDef(
2484            ident,
2485            ast::MacroDef { body, macro_rules: true, eii_declaration: None },
2486        ))
2487    }
2488
2489    /// Item macro invocations or `macro_rules!` definitions need inherited visibility.
2490    /// If that's not the case, emit an error.
2491    fn complain_if_pub_macro(&self, vis: &Visibility, macro_rules: bool) {
2492        if let VisibilityKind::Inherited = vis.kind {
2493            return;
2494        }
2495
2496        let vstr = pprust::vis_to_string(vis);
2497        let vstr = vstr.trim_end();
2498        if macro_rules {
2499            self.dcx().emit_err(errors::MacroRulesVisibility { span: vis.span, vis: vstr });
2500        } else {
2501            self.dcx().emit_err(errors::MacroInvocationVisibility { span: vis.span, vis: vstr });
2502        }
2503    }
2504
2505    fn eat_semi_for_macro_if_needed(&mut self, args: &DelimArgs, path: Option<&Path>) {
2506        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)) {
2507            self.report_invalid_macro_expansion_item(args, path);
2508        }
2509    }
2510
2511    fn report_invalid_macro_expansion_item(&self, args: &DelimArgs, path: Option<&Path>) {
2512        let span = args.dspan.entire();
2513        let mut err = self.dcx().struct_span_err(
2514            span,
2515            "macros that expand to items must be delimited with braces or followed by a semicolon",
2516        );
2517        // FIXME: This will make us not emit the help even for declarative
2518        // macros within the same crate (that we can fix), which is sad.
2519        if !span.from_expansion() {
2520            let DelimSpan { open, close } = args.dspan;
2521            // Check if this looks like `macro_rules!(name) { ... }`
2522            // a common mistake when trying to define a macro.
2523            if let Some(path) = path
2524                && path.segments.first().is_some_and(|seg| seg.ident.name == sym::macro_rules)
2525                && args.delim == Delimiter::Parenthesis
2526            {
2527                let replace =
2528                    if path.span.hi() + rustc_span::BytePos(1) < open.lo() { "" } else { " " };
2529                err.multipart_suggestion(
2530                    "to define a macro, remove the parentheses around the macro name",
2531                    ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(open, replace.to_string()), (close, String::new())]))vec![(open, replace.to_string()), (close, String::new())],
2532                    Applicability::MachineApplicable,
2533                );
2534            } else {
2535                err.multipart_suggestion(
2536                    "change the delimiters to curly braces",
2537                    ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(open, "{".to_string()), (close, '}'.to_string())]))vec![(open, "{".to_string()), (close, '}'.to_string())],
2538                    Applicability::MaybeIncorrect,
2539                );
2540                err.span_suggestion(
2541                    span.with_neighbor(self.token.span).shrink_to_hi(),
2542                    "add a semicolon",
2543                    ';',
2544                    Applicability::MaybeIncorrect,
2545                );
2546            }
2547        }
2548        err.emit();
2549    }
2550
2551    /// Checks if current token is one of tokens which cannot be nested like `kw::Enum`. In case
2552    /// it is, we try to parse the item and report error about nested types.
2553    fn recover_nested_adt_item(&mut self, keyword: Symbol) -> PResult<'a, bool> {
2554        if (self.token.is_keyword(kw::Enum)
2555            || self.token.is_keyword(kw::Struct)
2556            || self.token.is_keyword(kw::Union))
2557            && self.look_ahead(1, |t| t.is_ident())
2558        {
2559            let kw_token = self.token;
2560            let kw_str = pprust::token_to_string(&kw_token);
2561            let item = self.parse_item(
2562                ForceCollect::No,
2563                AllowConstBlockItems::DoesNotMatter, // self.token != kw::Const
2564            )?;
2565            let mut item = item.unwrap().span;
2566            if self.token == token::Comma {
2567                item = item.to(self.token.span);
2568            }
2569            self.dcx().emit_err(errors::NestedAdt {
2570                span: kw_token.span,
2571                item,
2572                kw_str,
2573                keyword: keyword.as_str(),
2574            });
2575            // We successfully parsed the item but we must inform the caller about nested problem.
2576            return Ok(false);
2577        }
2578        Ok(true)
2579    }
2580}
2581
2582/// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
2583///
2584/// The function decides if, per-parameter `p`, `p` must have a pattern or just a type.
2585///
2586/// This function pointer accepts an edition, because in edition 2015, trait declarations
2587/// were allowed to omit parameter names. In 2018, they became required. It also accepts an
2588/// `IsDotDotDot` parameter, as `extern` function declarations and function pointer types are
2589/// allowed to omit the name of the `...` but regular function items are not.
2590type ReqName = fn(Edition, IsDotDotDot) -> bool;
2591
2592#[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)]
2593pub(crate) enum IsDotDotDot {
2594    Yes,
2595    No,
2596}
2597
2598/// Parsing configuration for functions.
2599///
2600/// The syntax of function items is slightly different within trait definitions,
2601/// impl blocks, and modules. It is still parsed using the same code, just with
2602/// different flags set, so that even when the input is wrong and produces a parse
2603/// error, it still gets into the AST and the rest of the parser and
2604/// type checker can run.
2605#[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)]
2606pub(crate) struct FnParseMode {
2607    /// A function pointer that decides if, per-parameter `p`, `p` must have a
2608    /// pattern or just a type. This field affects parsing of the parameters list.
2609    ///
2610    /// ```text
2611    /// fn foo(alef: A) -> X { X::new() }
2612    ///        -----^^ affects parsing this part of the function signature
2613    ///        |
2614    ///        if req_name returns false, then this name is optional
2615    ///
2616    /// fn bar(A) -> X;
2617    ///        ^
2618    ///        |
2619    ///        if req_name returns true, this is an error
2620    /// ```
2621    ///
2622    /// Calling this function pointer should only return false if:
2623    ///
2624    ///   * The item is being parsed inside of a trait definition.
2625    ///     Within an impl block or a module, it should always evaluate
2626    ///     to true.
2627    ///   * The span is from Edition 2015. In particular, you can get a
2628    ///     2015 span inside a 2021 crate using macros.
2629    ///
2630    /// Or if `IsDotDotDot::Yes`, this function will also return `false` if the item being parsed
2631    /// is inside an `extern` block.
2632    pub(super) req_name: ReqName,
2633    /// The context in which this function is parsed, used for diagnostics.
2634    /// This indicates the fn is a free function or method and so on.
2635    pub(super) context: FnContext,
2636    /// If this flag is set to `true`, then plain, semicolon-terminated function
2637    /// prototypes are not allowed here.
2638    ///
2639    /// ```text
2640    /// fn foo(alef: A) -> X { X::new() }
2641    ///                      ^^^^^^^^^^^^
2642    ///                      |
2643    ///                      this is always allowed
2644    ///
2645    /// fn bar(alef: A, bet: B) -> X;
2646    ///                             ^
2647    ///                             |
2648    ///                             if req_body is set to true, this is an error
2649    /// ```
2650    ///
2651    /// This field should only be set to false if the item is inside of a trait
2652    /// definition or extern block. Within an impl block or a module, it should
2653    /// always be set to true.
2654    pub(super) req_body: bool,
2655}
2656
2657/// The context in which a function is parsed.
2658/// FIXME(estebank, xizheyin): Use more variants.
2659#[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_fields_are_eq(&self) {}
}Eq)]
2660pub(crate) enum FnContext {
2661    /// Free context.
2662    Free,
2663    /// A Trait context.
2664    Trait,
2665    /// An Impl block.
2666    Impl,
2667}
2668
2669/// Parsing of functions and methods.
2670impl<'a> Parser<'a> {
2671    /// Parse a function starting from the front matter (`const ...`) to the body `{ ... }` or `;`.
2672    fn parse_fn(
2673        &mut self,
2674        attrs: &mut AttrVec,
2675        fn_parse_mode: FnParseMode,
2676        sig_lo: Span,
2677        vis: &Visibility,
2678        case: Case,
2679    ) -> PResult<'a, (Ident, FnSig, Generics, Option<Box<FnContract>>, Option<Box<Block>>)> {
2680        let fn_span = self.token.span;
2681        let header = self.parse_fn_front_matter(vis, case, FrontMatterParsingMode::Function)?; // `const ... fn`
2682        let ident = self.parse_ident()?; // `foo`
2683        let mut generics = self.parse_generics()?; // `<'a, T, ...>`
2684        let decl = match self.parse_fn_decl(&fn_parse_mode, AllowPlus::Yes, RecoverReturnSign::Yes)
2685        {
2686            Ok(decl) => decl,
2687            Err(old_err) => {
2688                // If we see `for Ty ...` then user probably meant `impl` item.
2689                if self.token.is_keyword(kw::For) {
2690                    old_err.cancel();
2691                    return Err(self.dcx().create_err(errors::FnTypoWithImpl { fn_span }));
2692                } else {
2693                    return Err(old_err);
2694                }
2695            }
2696        };
2697
2698        // Store the end of function parameters to give better diagnostics
2699        // inside `parse_fn_body()`.
2700        let fn_params_end = self.prev_token.span.shrink_to_hi();
2701
2702        let contract = self.parse_contract()?;
2703
2704        generics.where_clause = self.parse_where_clause()?; // `where T: Ord`
2705
2706        // `fn_params_end` is needed only when it's followed by a where clause.
2707        let fn_params_end =
2708            if generics.where_clause.has_where_token { Some(fn_params_end) } else { None };
2709
2710        let mut sig_hi = self.prev_token.span;
2711        // Either `;` or `{ ... }`.
2712        let body =
2713            self.parse_fn_body(attrs, &ident, &mut sig_hi, fn_parse_mode.req_body, fn_params_end)?;
2714        let fn_sig_span = sig_lo.to(sig_hi);
2715        Ok((ident, FnSig { header, decl, span: fn_sig_span }, generics, contract, body))
2716    }
2717
2718    /// Provide diagnostics when function body is not found
2719    fn error_fn_body_not_found(
2720        &mut self,
2721        ident_span: Span,
2722        req_body: bool,
2723        fn_params_end: Option<Span>,
2724    ) -> PResult<'a, ErrorGuaranteed> {
2725        let expected: &[_] =
2726            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)] };
2727        match self.expected_one_of_not_found(&[], expected) {
2728            Ok(error_guaranteed) => Ok(error_guaranteed),
2729            Err(mut err) => {
2730                if self.token == token::CloseBrace {
2731                    // The enclosing `mod`, `trait` or `impl` is being closed, so keep the `fn` in
2732                    // the AST for typechecking.
2733                    err.span_label(ident_span, "while parsing this `fn`");
2734                    Ok(err.emit())
2735                } else if self.token == token::RArrow
2736                    && let Some(fn_params_end) = fn_params_end
2737                {
2738                    // Instead of a function body, the parser has encountered a right arrow
2739                    // preceded by a where clause.
2740
2741                    // Find whether token behind the right arrow is a function trait and
2742                    // store its span.
2743                    let fn_trait_span =
2744                        [sym::FnOnce, sym::FnMut, sym::Fn].into_iter().find_map(|symbol| {
2745                            if self.prev_token.is_ident_named(symbol) {
2746                                Some(self.prev_token.span)
2747                            } else {
2748                                None
2749                            }
2750                        });
2751
2752                    // Parse the return type (along with the right arrow) and store its span.
2753                    // If there's a parse error, cancel it and return the existing error
2754                    // as we are primarily concerned with the
2755                    // expected-function-body-but-found-something-else error here.
2756                    let arrow_span = self.token.span;
2757                    let ty_span = match self.parse_ret_ty(
2758                        AllowPlus::Yes,
2759                        RecoverQPath::Yes,
2760                        RecoverReturnSign::Yes,
2761                    ) {
2762                        Ok(ty_span) => ty_span.span().shrink_to_hi(),
2763                        Err(parse_error) => {
2764                            parse_error.cancel();
2765                            return Err(err);
2766                        }
2767                    };
2768                    let ret_ty_span = arrow_span.to(ty_span);
2769
2770                    if let Some(fn_trait_span) = fn_trait_span {
2771                        // Typo'd Fn* trait bounds such as
2772                        // fn foo<F>() where F: FnOnce -> () {}
2773                        err.subdiagnostic(errors::FnTraitMissingParen { span: fn_trait_span });
2774                    } else if let Ok(snippet) = self.psess.source_map().span_to_snippet(ret_ty_span)
2775                    {
2776                        // If token behind right arrow is not a Fn* trait, the programmer
2777                        // probably misplaced the return type after the where clause like
2778                        // `fn foo<T>() where T: Default -> u8 {}`
2779                        err.primary_message(
2780                            "return type should be specified after the function parameters",
2781                        );
2782                        err.subdiagnostic(errors::MisplacedReturnType {
2783                            fn_params_end,
2784                            snippet,
2785                            ret_ty_span,
2786                        });
2787                    }
2788                    Err(err)
2789                } else {
2790                    Err(err)
2791                }
2792            }
2793        }
2794    }
2795
2796    /// Parse the "body" of a function.
2797    /// This can either be `;` when there's no body,
2798    /// or e.g. a block when the function is a provided one.
2799    fn parse_fn_body(
2800        &mut self,
2801        attrs: &mut AttrVec,
2802        ident: &Ident,
2803        sig_hi: &mut Span,
2804        req_body: bool,
2805        fn_params_end: Option<Span>,
2806    ) -> PResult<'a, Option<Box<Block>>> {
2807        let has_semi = if req_body {
2808            self.token == TokenKind::Semi
2809        } else {
2810            // Only include `;` in list of expected tokens if body is not required
2811            self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Semi,
    token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi))
2812        };
2813        let (inner_attrs, body) = if has_semi {
2814            // Include the trailing semicolon in the span of the signature
2815            self.expect_semi()?;
2816            *sig_hi = self.prev_token.span;
2817            (AttrVec::new(), None)
2818        } 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() {
2819            let prev_in_fn_body = self.in_fn_body;
2820            self.in_fn_body = true;
2821            let res = self.parse_block_common(self.token.span, BlockCheckMode::Default, None).map(
2822                |(attrs, mut body)| {
2823                    if let Some(guar) = self.fn_body_missing_semi_guar.take() {
2824                        body.stmts.push(self.mk_stmt(
2825                            body.span,
2826                            StmtKind::Expr(self.mk_expr(body.span, ExprKind::Err(guar))),
2827                        ));
2828                    }
2829                    (attrs, Some(body))
2830                },
2831            );
2832            self.in_fn_body = prev_in_fn_body;
2833            res?
2834        } else if self.token == token::Eq {
2835            // Recover `fn foo() = $expr;`.
2836            self.bump(); // `=`
2837            let eq_sp = self.prev_token.span;
2838            let _ = self.parse_expr()?;
2839            self.expect_semi()?; // `;`
2840            let span = eq_sp.to(self.prev_token.span);
2841            let guar = self.dcx().emit_err(errors::FunctionBodyEqualsExpr {
2842                span,
2843                sugg: errors::FunctionBodyEqualsExprSugg { eq: eq_sp, semi: self.prev_token.span },
2844            });
2845            (AttrVec::new(), Some(self.mk_block_err(span, guar)))
2846        } else {
2847            self.error_fn_body_not_found(ident.span, req_body, fn_params_end)?;
2848            (AttrVec::new(), None)
2849        };
2850        attrs.extend(inner_attrs);
2851        Ok(body)
2852    }
2853
2854    fn check_impl_frontmatter(&mut self, look_ahead: usize) -> bool {
2855        const ALL_QUALS: &[Symbol] = &[kw::Const, kw::Unsafe];
2856        // In contrast to the loop below, this call inserts `impl` into the
2857        // list of expected tokens shown in diagnostics.
2858        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)) {
2859            return true;
2860        }
2861        let mut i = 0;
2862        while i < ALL_QUALS.len() {
2863            let action = self.look_ahead(i + look_ahead, |token| {
2864                if token.is_keyword(kw::Impl) {
2865                    return Some(true);
2866                }
2867                if ALL_QUALS.iter().any(|&qual| token.is_keyword(qual)) {
2868                    // Ok, we found a legal keyword, keep looking for `impl`
2869                    return None;
2870                }
2871                Some(false)
2872            });
2873            if let Some(ret) = action {
2874                return ret;
2875            }
2876            i += 1;
2877        }
2878
2879        self.is_keyword_ahead(i, &[kw::Impl])
2880    }
2881
2882    /// Is the current token the start of an `FnHeader` / not a valid parse?
2883    ///
2884    /// `check_pub` adds additional `pub` to the checks in case users place it
2885    /// wrongly, can be used to ensure `pub` never comes after `default`.
2886    pub(super) fn check_fn_front_matter(&mut self, check_pub: bool, case: Case) -> bool {
2887        const ALL_QUALS: &[ExpKeywordPair] = &[
2888            crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Pub,
    token_type: crate::parser::token_type::TokenType::KwPub,
}exp!(Pub),
2889            crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Gen,
    token_type: crate::parser::token_type::TokenType::KwGen,
}exp!(Gen),
2890            crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Const,
    token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const),
2891            crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Async,
    token_type: crate::parser::token_type::TokenType::KwAsync,
}exp!(Async),
2892            crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Unsafe,
    token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe),
2893            crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Safe,
    token_type: crate::parser::token_type::TokenType::KwSafe,
}exp!(Safe),
2894            crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Extern,
    token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern),
2895        ];
2896
2897        // We use an over-approximation here.
2898        // `const const`, `fn const` won't parse, but we're not stepping over other syntax either.
2899        // `pub` is added in case users got confused with the ordering like `async pub fn`,
2900        // only if it wasn't preceded by `default` as `default pub` is invalid.
2901        let quals: &[_] = if check_pub {
2902            ALL_QUALS
2903        } else {
2904            &[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)]
2905        };
2906        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`.
2907            // `$qual fn` or `$qual $qual`:
2908            || quals.iter().any(|&exp| self.check_keyword_case(exp, case))
2909                && self.look_ahead(1, |t| {
2910                    // `$qual fn`, e.g. `const fn` or `async fn`.
2911                    t.is_keyword_case(kw::Fn, case)
2912                    // Two qualifiers `$qual $qual` is enough, e.g. `async unsafe`.
2913                    || (
2914                        (
2915                            t.is_non_raw_ident_where(|i|
2916                                quals.iter().any(|exp| exp.kw == i.name)
2917                                    // Rule out 2015 `const async: T = val`.
2918                                    && i.is_reserved()
2919                            )
2920                            || case == Case::Insensitive
2921                                && t.is_non_raw_ident_where(|i| quals.iter().any(|exp| {
2922                                    exp.kw.as_str() == i.name.as_str().to_lowercase()
2923                                }))
2924                        )
2925                        // Rule out `unsafe extern {`.
2926                        && !self.is_unsafe_foreign_mod()
2927                        // Rule out `async gen {` and `async gen move {`
2928                        && !self.is_async_gen_block()
2929                        // Rule out `const unsafe auto` and `const unsafe trait` and `const unsafe impl`.
2930                        && !self.is_keyword_ahead(2, &[kw::Auto, kw::Trait, kw::Impl])
2931                    )
2932                })
2933            // `extern ABI fn`
2934            || 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)
2935                // Use `tree_look_ahead` because `ABI` might be a metavariable,
2936                // i.e. an invisible-delimited sequence, and `tree_look_ahead`
2937                // will consider that a single element when looking ahead.
2938                && self.look_ahead(1, |t| t.can_begin_string_literal())
2939                && (self.tree_look_ahead(2, |tt| {
2940                    match tt {
2941                        TokenTree::Token(t, _) => t.is_keyword_case(kw::Fn, case),
2942                        TokenTree::Delimited(..) => false,
2943                    }
2944                }) == Some(true) ||
2945                    // This branch is only for better diagnostics; `pub`, `unsafe`, etc. are not
2946                    // allowed here.
2947                    (self.may_recover()
2948                        && self.tree_look_ahead(2, |tt| {
2949                            match tt {
2950                                TokenTree::Token(t, _) =>
2951                                    ALL_QUALS.iter().any(|exp| {
2952                                        t.is_keyword(exp.kw)
2953                                    }),
2954                                TokenTree::Delimited(..) => false,
2955                            }
2956                        }) == Some(true)
2957                        && self.tree_look_ahead(3, |tt| {
2958                            match tt {
2959                                TokenTree::Token(t, _) => t.is_keyword_case(kw::Fn, case),
2960                                TokenTree::Delimited(..) => false,
2961                            }
2962                        }) == Some(true)
2963                    )
2964                )
2965    }
2966
2967    /// Parses all the "front matter" (or "qualifiers") for a `fn` declaration,
2968    /// up to and including the `fn` keyword. The formal grammar is:
2969    ///
2970    /// ```text
2971    /// Extern = "extern" StringLit? ;
2972    /// FnQual = "const"? "async"? "unsafe"? Extern? ;
2973    /// FnFrontMatter = FnQual "fn" ;
2974    /// ```
2975    ///
2976    /// `vis` represents the visibility that was already parsed, if any. Use
2977    /// `Visibility::Inherited` when no visibility is known.
2978    ///
2979    /// If `parsing_mode` is `FrontMatterParsingMode::FunctionPtrType`, we error on `const` and `async` qualifiers,
2980    /// which are not allowed in function pointer types.
2981    pub(super) fn parse_fn_front_matter(
2982        &mut self,
2983        orig_vis: &Visibility,
2984        case: Case,
2985        parsing_mode: FrontMatterParsingMode,
2986    ) -> PResult<'a, FnHeader> {
2987        let sp_start = self.token.span;
2988        let constness = self.parse_constness(case);
2989        if parsing_mode == FrontMatterParsingMode::FunctionPtrType
2990            && let Const::Yes(const_span) = constness
2991        {
2992            self.dcx().emit_err(FnPointerCannotBeConst {
2993                span: const_span,
2994                suggestion: const_span.until(self.token.span),
2995            });
2996        }
2997
2998        let async_start_sp = self.token.span;
2999        let coroutine_kind = self.parse_coroutine_kind(case);
3000        if parsing_mode == FrontMatterParsingMode::FunctionPtrType
3001            && let Some(ast::CoroutineKind::Async { span: async_span, .. }) = coroutine_kind
3002        {
3003            self.dcx().emit_err(FnPointerCannotBeAsync {
3004                span: async_span,
3005                suggestion: async_span.until(self.token.span),
3006            });
3007        }
3008        // FIXME(gen_blocks): emit a similar error for `gen fn()`
3009
3010        let unsafe_start_sp = self.token.span;
3011        let safety = self.parse_safety(case);
3012
3013        let ext_start_sp = self.token.span;
3014        let ext = self.parse_extern(case);
3015
3016        if let Some(CoroutineKind::Async { span, .. }) = coroutine_kind {
3017            if span.is_rust_2015() {
3018                self.dcx().emit_err(errors::AsyncFnIn2015 {
3019                    span,
3020                    help: errors::HelpUseLatestEdition::new(),
3021                });
3022            }
3023        }
3024
3025        match coroutine_kind {
3026            Some(CoroutineKind::Gen { span, .. }) | Some(CoroutineKind::AsyncGen { span, .. }) => {
3027                self.psess.gated_spans.gate(sym::gen_blocks, span);
3028            }
3029            Some(CoroutineKind::Async { .. }) | None => {}
3030        }
3031
3032        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) {
3033            // It is possible for `expect_one_of` to recover given the contents of
3034            // `self.expected_token_types`, therefore, do not use `self.unexpected()` which doesn't
3035            // account for this.
3036            match self.expect_one_of(&[], &[]) {
3037                Ok(Recovered::Yes(_)) => {}
3038                Ok(Recovered::No) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
3039                Err(mut err) => {
3040                    // Qualifier keywords ordering check
3041                    enum WrongKw {
3042                        Duplicated(Span),
3043                        Misplaced(Span),
3044                        /// `MisplacedDisallowedQualifier` is only used instead of `Misplaced`,
3045                        /// when the misplaced keyword is disallowed by the current `FrontMatterParsingMode`.
3046                        /// In this case, we avoid generating the suggestion to swap around the keywords,
3047                        /// as we already generated a suggestion to remove the keyword earlier.
3048                        MisplacedDisallowedQualifier,
3049                    }
3050
3051                    // We may be able to recover
3052                    let mut recover_constness = constness;
3053                    let mut recover_coroutine_kind = coroutine_kind;
3054                    let mut recover_safety = safety;
3055                    // This will allow the machine fix to directly place the keyword in the correct place or to indicate
3056                    // that the keyword is already present and the second instance should be removed.
3057                    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)) {
3058                        match constness {
3059                            Const::Yes(sp) => Some(WrongKw::Duplicated(sp)),
3060                            Const::No => {
3061                                recover_constness = Const::Yes(self.token.span);
3062                                match parsing_mode {
3063                                    FrontMatterParsingMode::Function => {
3064                                        Some(WrongKw::Misplaced(async_start_sp))
3065                                    }
3066                                    FrontMatterParsingMode::FunctionPtrType => {
3067                                        self.dcx().emit_err(FnPointerCannotBeConst {
3068                                            span: self.token.span,
3069                                            suggestion: self
3070                                                .token
3071                                                .span
3072                                                .with_lo(self.prev_token.span.hi()),
3073                                        });
3074                                        Some(WrongKw::MisplacedDisallowedQualifier)
3075                                    }
3076                                }
3077                            }
3078                        }
3079                    } 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)) {
3080                        match coroutine_kind {
3081                            Some(CoroutineKind::Async { span, .. }) => {
3082                                Some(WrongKw::Duplicated(span))
3083                            }
3084                            Some(CoroutineKind::AsyncGen { span, .. }) => {
3085                                Some(WrongKw::Duplicated(span))
3086                            }
3087                            Some(CoroutineKind::Gen { .. }) => {
3088                                recover_coroutine_kind = Some(CoroutineKind::AsyncGen {
3089                                    span: self.token.span,
3090                                    closure_id: DUMMY_NODE_ID,
3091                                    return_impl_trait_id: DUMMY_NODE_ID,
3092                                });
3093                                // FIXME(gen_blocks): This span is wrong, didn't want to think about it.
3094                                Some(WrongKw::Misplaced(unsafe_start_sp))
3095                            }
3096                            None => {
3097                                recover_coroutine_kind = Some(CoroutineKind::Async {
3098                                    span: self.token.span,
3099                                    closure_id: DUMMY_NODE_ID,
3100                                    return_impl_trait_id: DUMMY_NODE_ID,
3101                                });
3102                                match parsing_mode {
3103                                    FrontMatterParsingMode::Function => {
3104                                        Some(WrongKw::Misplaced(async_start_sp))
3105                                    }
3106                                    FrontMatterParsingMode::FunctionPtrType => {
3107                                        self.dcx().emit_err(FnPointerCannotBeAsync {
3108                                            span: self.token.span,
3109                                            suggestion: self
3110                                                .token
3111                                                .span
3112                                                .with_lo(self.prev_token.span.hi()),
3113                                        });
3114                                        Some(WrongKw::MisplacedDisallowedQualifier)
3115                                    }
3116                                }
3117                            }
3118                        }
3119                    } 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)) {
3120                        match safety {
3121                            Safety::Unsafe(sp) => Some(WrongKw::Duplicated(sp)),
3122                            Safety::Safe(sp) => {
3123                                recover_safety = Safety::Unsafe(self.token.span);
3124                                Some(WrongKw::Misplaced(sp))
3125                            }
3126                            Safety::Default => {
3127                                recover_safety = Safety::Unsafe(self.token.span);
3128                                Some(WrongKw::Misplaced(ext_start_sp))
3129                            }
3130                        }
3131                    } 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)) {
3132                        match safety {
3133                            Safety::Safe(sp) => Some(WrongKw::Duplicated(sp)),
3134                            Safety::Unsafe(sp) => {
3135                                recover_safety = Safety::Safe(self.token.span);
3136                                Some(WrongKw::Misplaced(sp))
3137                            }
3138                            Safety::Default => {
3139                                recover_safety = Safety::Safe(self.token.span);
3140                                Some(WrongKw::Misplaced(ext_start_sp))
3141                            }
3142                        }
3143                    } else {
3144                        None
3145                    };
3146
3147                    // The keyword is already present, suggest removal of the second instance
3148                    if let Some(WrongKw::Duplicated(original_sp)) = wrong_kw {
3149                        let original_kw = self
3150                            .span_to_snippet(original_sp)
3151                            .expect("Span extracted directly from keyword should always work");
3152
3153                        err.span_suggestion(
3154                            self.token_uninterpolated_span(),
3155                            ::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"),
3156                            "",
3157                            Applicability::MachineApplicable,
3158                        )
3159                        .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"));
3160                    }
3161                    // The keyword has not been seen yet, suggest correct placement in the function front matter
3162                    else if let Some(WrongKw::Misplaced(correct_pos_sp)) = wrong_kw {
3163                        let correct_pos_sp = correct_pos_sp.to(self.prev_token.span);
3164                        if let Ok(current_qual) = self.span_to_snippet(correct_pos_sp) {
3165                            let misplaced_qual_sp = self.token_uninterpolated_span();
3166                            let misplaced_qual = self.span_to_snippet(misplaced_qual_sp).unwrap();
3167
3168                            err.span_suggestion(
3169                                    correct_pos_sp.to(misplaced_qual_sp),
3170                                    ::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}`"),
3171                                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} {1}", misplaced_qual,
                current_qual))
    })format!("{misplaced_qual} {current_qual}"),
3172                                    Applicability::MachineApplicable,
3173                                ).note("keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern`");
3174                        }
3175                    }
3176                    // Recover incorrect visibility order such as `async pub`
3177                    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)) {
3178                        let sp = sp_start.to(self.prev_token.span);
3179                        if let Ok(snippet) = self.span_to_snippet(sp) {
3180                            let current_vis = match self.parse_visibility(FollowedByType::No) {
3181                                Ok(v) => v,
3182                                Err(d) => {
3183                                    d.cancel();
3184                                    return Err(err);
3185                                }
3186                            };
3187                            let vs = pprust::vis_to_string(&current_vis);
3188                            let vs = vs.trim_end();
3189
3190                            // There was no explicit visibility
3191                            if #[allow(non_exhaustive_omitted_patterns)] match orig_vis.kind {
    VisibilityKind::Inherited => true,
    _ => false,
}matches!(orig_vis.kind, VisibilityKind::Inherited) {
3192                                err.span_suggestion(
3193                                    sp_start.to(self.prev_token.span),
3194                                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("visibility `{0}` must come before `{1}`",
                vs, snippet))
    })format!("visibility `{vs}` must come before `{snippet}`"),
3195                                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} {1}", vs, snippet))
    })format!("{vs} {snippet}"),
3196                                    Applicability::MachineApplicable,
3197                                );
3198                            }
3199                            // There was an explicit visibility
3200                            else {
3201                                err.span_suggestion(
3202                                    current_vis.span,
3203                                    "there is already a visibility modifier, remove one",
3204                                    "",
3205                                    Applicability::MachineApplicable,
3206                                )
3207                                .span_note(orig_vis.span, "explicit visibility first seen here");
3208                            }
3209                        }
3210                    }
3211
3212                    // FIXME(gen_blocks): add keyword recovery logic for genness
3213
3214                    if let Some(wrong_kw) = wrong_kw
3215                        && self.may_recover()
3216                        && self.look_ahead(1, |tok| tok.is_keyword_case(kw::Fn, case))
3217                    {
3218                        // Advance past the misplaced keyword and `fn`
3219                        self.bump();
3220                        self.bump();
3221                        // When we recover from a `MisplacedDisallowedQualifier`, we already emitted an error for the disallowed qualifier
3222                        // So we don't emit another error that the qualifier is unexpected.
3223                        if #[allow(non_exhaustive_omitted_patterns)] match wrong_kw {
    WrongKw::MisplacedDisallowedQualifier => true,
    _ => false,
}matches!(wrong_kw, WrongKw::MisplacedDisallowedQualifier) {
3224                            err.cancel();
3225                        } else {
3226                            err.emit();
3227                        }
3228                        return Ok(FnHeader {
3229                            constness: recover_constness,
3230                            safety: recover_safety,
3231                            coroutine_kind: recover_coroutine_kind,
3232                            ext,
3233                        });
3234                    }
3235
3236                    return Err(err);
3237                }
3238            }
3239        }
3240
3241        Ok(FnHeader { constness, safety, coroutine_kind, ext })
3242    }
3243
3244    /// Parses the parameter list and result type of a function declaration.
3245    pub(super) fn parse_fn_decl(
3246        &mut self,
3247        fn_parse_mode: &FnParseMode,
3248        ret_allow_plus: AllowPlus,
3249        recover_return_sign: RecoverReturnSign,
3250    ) -> PResult<'a, Box<FnDecl>> {
3251        Ok(Box::new(FnDecl {
3252            inputs: self.parse_fn_params(fn_parse_mode)?,
3253            output: self.parse_ret_ty(ret_allow_plus, RecoverQPath::Yes, recover_return_sign)?,
3254        }))
3255    }
3256
3257    /// Parses the parameter list of a function, including the `(` and `)` delimiters.
3258    pub(super) fn parse_fn_params(
3259        &mut self,
3260        fn_parse_mode: &FnParseMode,
3261    ) -> PResult<'a, ThinVec<Param>> {
3262        let mut first_param = true;
3263        // Parse the arguments, starting out with `self` being allowed...
3264        if self.token != TokenKind::OpenParen
3265        // might be typo'd trait impl, handled elsewhere
3266        && !self.token.is_keyword(kw::For)
3267        {
3268            // recover from missing argument list, e.g. `fn main -> () {}`
3269            self.dcx()
3270                .emit_err(errors::MissingFnParams { span: self.prev_token.span.shrink_to_hi() });
3271            return Ok(ThinVec::new());
3272        }
3273
3274        let (mut params, _) = self.parse_paren_comma_seq(|p| {
3275            p.recover_vcs_conflict_marker();
3276            let snapshot = p.create_snapshot_for_diagnostic();
3277            let param = p.parse_param_general(fn_parse_mode, first_param, true).or_else(|e| {
3278                let guar = e.emit();
3279                // When parsing a param failed, we should check to make the span of the param
3280                // not contain '(' before it.
3281                // For example when parsing `*mut Self` in function `fn oof(*mut Self)`.
3282                let lo = if let TokenKind::OpenParen = p.prev_token.kind {
3283                    p.prev_token.span.shrink_to_hi()
3284                } else {
3285                    p.prev_token.span
3286                };
3287                p.restore_snapshot(snapshot);
3288                // Skip every token until next possible arg or end.
3289                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)]);
3290                // Create a placeholder argument for proper arg count (issue #34264).
3291                Ok(dummy_arg(Ident::new(sym::dummy, lo.to(p.prev_token.span)), guar))
3292            });
3293            // ...now that we've parsed the first argument, `self` is no longer allowed.
3294            first_param = false;
3295            param
3296        })?;
3297        // Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
3298        self.deduplicate_recovered_params_names(&mut params);
3299        Ok(params)
3300    }
3301
3302    /// Parses a single function parameter.
3303    ///
3304    /// - `self` is syntactically allowed when `first_param` holds.
3305    /// - `recover_arg_parse` is used to recover from a failed argument parse.
3306    pub(super) fn parse_param_general(
3307        &mut self,
3308        fn_parse_mode: &FnParseMode,
3309        first_param: bool,
3310        recover_arg_parse: bool,
3311    ) -> PResult<'a, Param> {
3312        let lo = self.token.span;
3313        let attrs = self.parse_outer_attributes()?;
3314        self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
3315            // Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
3316            if let Some(mut param) = this.parse_self_param()? {
3317                param.attrs = attrs;
3318                let res = if first_param { Ok(param) } else { this.recover_bad_self_param(param) };
3319                return Ok((res?, Trailing::No, UsePreAttrPos::No));
3320            }
3321
3322            let is_dot_dot_dot = if this.token.kind == token::DotDotDot {
3323                IsDotDotDot::Yes
3324            } else {
3325                IsDotDotDot::No
3326            };
3327            let is_name_required = (fn_parse_mode.req_name)(
3328                this.token.span.with_neighbor(this.prev_token.span).edition(),
3329                is_dot_dot_dot,
3330            );
3331            let is_name_required = if is_name_required && is_dot_dot_dot == IsDotDotDot::Yes {
3332                this.psess.buffer_lint(
3333                    VARARGS_WITHOUT_PATTERN,
3334                    this.token.span,
3335                    ast::CRATE_NODE_ID,
3336                    errors::VarargsWithoutPattern { span: this.token.span },
3337                );
3338                false
3339            } else {
3340                is_name_required
3341            };
3342            let (pat, ty) = if is_name_required || this.is_named_param() {
3343                {
    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:3343",
                        "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(3343u32),
                        ::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);
3344                let (pat, colon) = this.parse_fn_param_pat_colon()?;
3345                if !colon {
3346                    let mut err = this.unexpected().unwrap_err();
3347                    return if let Some(ident) = this.parameter_without_type(
3348                        &mut err,
3349                        pat,
3350                        is_name_required,
3351                        first_param,
3352                        fn_parse_mode,
3353                    ) {
3354                        let guar = err.emit();
3355                        Ok((dummy_arg(ident, guar), Trailing::No, UsePreAttrPos::No))
3356                    } else {
3357                        Err(err)
3358                    };
3359                }
3360
3361                this.eat_incorrect_doc_comment_for_param_type();
3362                (pat, this.parse_ty_for_param()?)
3363            } else {
3364                {
    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:3364",
                        "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(3364u32),
                        ::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");
3365                let parser_snapshot_before_ty = this.create_snapshot_for_diagnostic();
3366                this.eat_incorrect_doc_comment_for_param_type();
3367                let mut ty = this.parse_ty_for_param();
3368
3369                if let Ok(t) = &ty {
3370                    // Check for trailing angle brackets
3371                    if let TyKind::Path(_, Path { segments, .. }) = &t.kind
3372                        && let Some(segment) = segments.last()
3373                        && let Some(guar) =
3374                            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)])
3375                    {
3376                        return Ok((
3377                            dummy_arg(segment.ident, guar),
3378                            Trailing::No,
3379                            UsePreAttrPos::No,
3380                        ));
3381                    }
3382
3383                    if this.token != token::Comma && this.token != token::CloseParen {
3384                        // This wasn't actually a type, but a pattern looking like a type,
3385                        // so we are going to rollback and re-parse for recovery.
3386                        ty = this.unexpected_any();
3387                    }
3388                }
3389                match ty {
3390                    Ok(ty) => {
3391                        let pat = this.mk_pat(ty.span, PatKind::Missing);
3392                        (Box::new(pat), ty)
3393                    }
3394                    // If this is a C-variadic argument and we hit an error, return the error.
3395                    Err(err) if this.token == token::DotDotDot => return Err(err),
3396                    Err(err) if this.unmatched_angle_bracket_count > 0 => return Err(err),
3397                    Err(err) if recover_arg_parse => {
3398                        // Recover from attempting to parse the argument as a type without pattern.
3399                        err.cancel();
3400                        this.restore_snapshot(parser_snapshot_before_ty);
3401                        this.recover_arg_parse()?
3402                    }
3403                    Err(err) => return Err(err),
3404                }
3405            };
3406
3407            let span = lo.to(this.prev_token.span);
3408
3409            Ok((
3410                Param { attrs, id: ast::DUMMY_NODE_ID, is_placeholder: false, pat, span, ty },
3411                Trailing::No,
3412                UsePreAttrPos::No,
3413            ))
3414        })
3415    }
3416
3417    /// Returns the parsed optional self parameter and whether a self shortcut was used.
3418    fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
3419        // Extract an identifier *after* having confirmed that the token is one.
3420        let expect_self_ident = |this: &mut Self| match this.token.ident() {
3421            Some((ident, IdentIsRaw::No)) => {
3422                this.bump();
3423                ident
3424            }
3425            _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
3426        };
3427        // is lifetime `n` tokens ahead?
3428        let is_lifetime = |this: &Self, n| this.look_ahead(n, |t| t.is_lifetime());
3429        // Is `self` `n` tokens ahead?
3430        let is_isolated_self = |this: &Self, n| {
3431            this.is_keyword_ahead(n, &[kw::SelfLower])
3432                && this.look_ahead(n + 1, |t| t != &token::PathSep)
3433        };
3434        // Is `pin const self` `n` tokens ahead?
3435        let is_isolated_pin_const_self = |this: &Self, n| {
3436            this.look_ahead(n, |token| token.is_ident_named(sym::pin))
3437                && this.is_keyword_ahead(n + 1, &[kw::Const])
3438                && is_isolated_self(this, n + 2)
3439        };
3440        // Is `mut self` `n` tokens ahead?
3441        let is_isolated_mut_self =
3442            |this: &Self, n| this.is_keyword_ahead(n, &[kw::Mut]) && is_isolated_self(this, n + 1);
3443        // Is `pin mut self` `n` tokens ahead?
3444        let is_isolated_pin_mut_self = |this: &Self, n| {
3445            this.look_ahead(n, |token| token.is_ident_named(sym::pin))
3446                && is_isolated_mut_self(this, n + 1)
3447        };
3448        // Parse `self` or `self: TYPE`. We already know the current token is `self`.
3449        let parse_self_possibly_typed = |this: &mut Self, m| {
3450            let eself_ident = expect_self_ident(this);
3451            let eself_hi = this.prev_token.span;
3452            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)) {
3453                SelfKind::Explicit(this.parse_ty()?, m)
3454            } else {
3455                SelfKind::Value(m)
3456            };
3457            Ok((eself, eself_ident, eself_hi))
3458        };
3459        let expect_self_ident_not_typed =
3460            |this: &mut Self, modifier: &SelfKind, modifier_span: Span| {
3461                let eself_ident = expect_self_ident(this);
3462
3463                // Recover `: Type` after a qualified self
3464                if this.may_recover() && this.eat_noexpect(&token::Colon) {
3465                    let snap = this.create_snapshot_for_diagnostic();
3466                    match this.parse_ty() {
3467                        Ok(ty) => {
3468                            this.dcx().emit_err(errors::IncorrectTypeOnSelf {
3469                                span: ty.span,
3470                                move_self_modifier: errors::MoveSelfModifier {
3471                                    removal_span: modifier_span,
3472                                    insertion_span: ty.span.shrink_to_lo(),
3473                                    modifier: modifier.to_ref_suggestion(),
3474                                },
3475                            });
3476                        }
3477                        Err(diag) => {
3478                            diag.cancel();
3479                            this.restore_snapshot(snap);
3480                        }
3481                    }
3482                }
3483                eself_ident
3484            };
3485        // Recover for the grammar `*self`, `*const self`, and `*mut self`.
3486        let recover_self_ptr = |this: &mut Self| {
3487            this.dcx().emit_err(errors::SelfArgumentPointer { span: this.token.span });
3488
3489            Ok((SelfKind::Value(Mutability::Not), expect_self_ident(this), this.prev_token.span))
3490        };
3491
3492        // Parse optional `self` parameter of a method.
3493        // Only a limited set of initial token sequences is considered `self` parameters; anything
3494        // else is parsed as a normal function parameter list, so some lookahead is required.
3495        let eself_lo = self.token.span;
3496        let (eself, eself_ident, eself_hi) = match self.token.uninterpolate().kind {
3497            token::And => {
3498                let has_lifetime = is_lifetime(self, 1);
3499                let skip_lifetime_count = has_lifetime as usize;
3500                let eself = if is_isolated_self(self, skip_lifetime_count + 1) {
3501                    // `&{'lt} self`
3502                    self.bump(); // &
3503                    let lifetime = has_lifetime.then(|| self.expect_lifetime());
3504                    SelfKind::Region(lifetime, Mutability::Not)
3505                } else if is_isolated_mut_self(self, skip_lifetime_count + 1) {
3506                    // `&{'lt} mut self`
3507                    self.bump(); // &
3508                    let lifetime = has_lifetime.then(|| self.expect_lifetime());
3509                    self.bump(); // mut
3510                    SelfKind::Region(lifetime, Mutability::Mut)
3511                } else if is_isolated_pin_const_self(self, skip_lifetime_count + 1) {
3512                    // `&{'lt} pin const self`
3513                    self.bump(); // &
3514                    let lifetime = has_lifetime.then(|| self.expect_lifetime());
3515                    self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
3516                    self.bump(); // pin
3517                    self.bump(); // const
3518                    SelfKind::Pinned(lifetime, Mutability::Not)
3519                } else if is_isolated_pin_mut_self(self, skip_lifetime_count + 1) {
3520                    // `&{'lt} pin mut self`
3521                    self.bump(); // &
3522                    let lifetime = has_lifetime.then(|| self.expect_lifetime());
3523                    self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
3524                    self.bump(); // pin
3525                    self.bump(); // mut
3526                    SelfKind::Pinned(lifetime, Mutability::Mut)
3527                } else {
3528                    // `&not_self`
3529                    return Ok(None);
3530                };
3531                let hi = self.token.span;
3532                let self_ident = expect_self_ident_not_typed(self, &eself, eself_lo.until(hi));
3533                (eself, self_ident, hi)
3534            }
3535            // `*self`
3536            token::Star if is_isolated_self(self, 1) => {
3537                self.bump();
3538                recover_self_ptr(self)?
3539            }
3540            // `*mut self` and `*const self`
3541            token::Star
3542                if self.look_ahead(1, |t| t.is_mutability()) && is_isolated_self(self, 2) =>
3543            {
3544                self.bump();
3545                self.bump();
3546                recover_self_ptr(self)?
3547            }
3548            // `self` and `self: TYPE`
3549            token::Ident(..) if is_isolated_self(self, 0) => {
3550                parse_self_possibly_typed(self, Mutability::Not)?
3551            }
3552            // `mut self` and `mut self: TYPE`
3553            token::Ident(..) if is_isolated_mut_self(self, 0) => {
3554                self.bump();
3555                parse_self_possibly_typed(self, Mutability::Mut)?
3556            }
3557            _ => return Ok(None),
3558        };
3559
3560        let eself = source_map::respan(eself_lo.to(eself_hi), eself);
3561        Ok(Some(Param::from_self(AttrVec::default(), eself, eself_ident)))
3562    }
3563
3564    fn is_named_param(&self) -> bool {
3565        let offset = match &self.token.kind {
3566            token::OpenInvisible(origin) => match origin {
3567                InvisibleOrigin::MetaVar(MetaVarKind::Pat(_)) => {
3568                    return self.check_noexpect_past_close_delim(&token::Colon);
3569                }
3570                _ => 0,
3571            },
3572            token::And | token::AndAnd => 1,
3573            _ if self.token.is_keyword(kw::Mut) => 1,
3574            _ => 0,
3575        };
3576
3577        self.look_ahead(offset, |t| t.is_ident())
3578            && self.look_ahead(offset + 1, |t| t == &token::Colon)
3579    }
3580
3581    fn recover_self_param(&mut self) -> bool {
3582        #[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!(
3583            self.parse_outer_attributes()
3584                .and_then(|_| self.parse_self_param())
3585                .map_err(|e| e.cancel()),
3586            Ok(Some(_))
3587        )
3588    }
3589
3590    /// Try to recover from over-parsing in const item when a semicolon is missing.
3591    ///
3592    /// This detects cases where we parsed too much because a semicolon was missing
3593    /// and the next line started an expression that the parser treated as a continuation
3594    /// (e.g., `foo() \n &bar` was parsed as `foo() & bar`).
3595    ///
3596    /// Returns a corrected expression if recovery is successful.
3597    fn try_recover_const_missing_semi(
3598        &mut self,
3599        rhs: &ConstItemRhsKind,
3600        const_span: Span,
3601    ) -> Option<Box<Expr>> {
3602        if self.token == TokenKind::Semi {
3603            return None;
3604        }
3605        let ConstItemRhsKind::Body { rhs: Some(rhs) } = rhs else {
3606            return None;
3607        };
3608        if !self.in_fn_body || !self.may_recover() || rhs.span.from_expansion() {
3609            return None;
3610        }
3611        if let Some((span, guar)) =
3612            self.missing_semi_from_binop("const", rhs, Some(const_span.shrink_to_lo()))
3613        {
3614            self.fn_body_missing_semi_guar = Some(guar);
3615            Some(self.mk_expr(span, ExprKind::Err(guar)))
3616        } else {
3617            None
3618        }
3619    }
3620}
3621
3622enum IsMacroRulesItem {
3623    Yes { has_bang: bool },
3624    No,
3625}
3626
3627#[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_fields_are_eq(&self) {}
}Eq)]
3628pub(super) enum FrontMatterParsingMode {
3629    /// Parse the front matter of a function declaration
3630    Function,
3631    /// Parse the front matter of a function pointet type.
3632    /// For function pointer types, the `const` and `async` keywords are not permitted.
3633    FunctionPtrType,
3634}