rustc_parse/parser/
item.rs

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