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