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