rustc_parse/parser/
item.rs

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