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        generics.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        self.expect_semi()?;
1051
1052        Ok(ItemKind::TyAlias(Box::new(TyAlias {
1053            defaultness,
1054            ident,
1055            generics,
1056            after_where_clause,
1057            bounds,
1058            ty,
1059        })))
1060    }
1061
1062    /// Parses a `UseTree`.
1063    ///
1064    /// ```text
1065    /// USE_TREE = [`::`] `*` |
1066    ///            [`::`] `{` USE_TREE_LIST `}` |
1067    ///            PATH `::` `*` |
1068    ///            PATH `::` `{` USE_TREE_LIST `}` |
1069    ///            PATH [`as` IDENT]
1070    /// ```
1071    fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
1072        let lo = self.token.span;
1073
1074        let mut prefix =
1075            ast::Path { segments: ThinVec::new(), span: lo.shrink_to_lo(), tokens: None };
1076        let kind =
1077            if self.check(exp!(OpenBrace)) || self.check(exp!(Star)) || self.is_import_coupler() {
1078                // `use *;` or `use ::*;` or `use {...};` or `use ::{...};`
1079                let mod_sep_ctxt = self.token.span.ctxt();
1080                if self.eat_path_sep() {
1081                    prefix
1082                        .segments
1083                        .push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
1084                }
1085
1086                self.parse_use_tree_glob_or_nested()?
1087            } else {
1088                // `use path::*;` or `use path::{...};` or `use path;` or `use path as bar;`
1089                prefix = self.parse_path(PathStyle::Mod)?;
1090
1091                if self.eat_path_sep() {
1092                    self.parse_use_tree_glob_or_nested()?
1093                } else {
1094                    // Recover from using a colon as path separator.
1095                    while self.eat_noexpect(&token::Colon) {
1096                        self.dcx()
1097                            .emit_err(errors::SingleColonImportPath { span: self.prev_token.span });
1098
1099                        // We parse the rest of the path and append it to the original prefix.
1100                        self.parse_path_segments(&mut prefix.segments, PathStyle::Mod, None)?;
1101                        prefix.span = lo.to(self.prev_token.span);
1102                    }
1103
1104                    UseTreeKind::Simple(self.parse_rename()?)
1105                }
1106            };
1107
1108        Ok(UseTree { prefix, kind, span: lo.to(self.prev_token.span) })
1109    }
1110
1111    /// Parses `*` or `{...}`.
1112    fn parse_use_tree_glob_or_nested(&mut self) -> PResult<'a, UseTreeKind> {
1113        Ok(if self.eat(exp!(Star)) {
1114            UseTreeKind::Glob
1115        } else {
1116            let lo = self.token.span;
1117            UseTreeKind::Nested {
1118                items: self.parse_use_tree_list()?,
1119                span: lo.to(self.prev_token.span),
1120            }
1121        })
1122    }
1123
1124    /// Parses a `UseTreeKind::Nested(list)`.
1125    ///
1126    /// ```text
1127    /// USE_TREE_LIST = ∅ | (USE_TREE `,`)* USE_TREE [`,`]
1128    /// ```
1129    fn parse_use_tree_list(&mut self) -> PResult<'a, ThinVec<(UseTree, ast::NodeId)>> {
1130        self.parse_delim_comma_seq(exp!(OpenBrace), exp!(CloseBrace), |p| {
1131            p.recover_vcs_conflict_marker();
1132            Ok((p.parse_use_tree()?, DUMMY_NODE_ID))
1133        })
1134        .map(|(r, _)| r)
1135    }
1136
1137    fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
1138        if self.eat_keyword(exp!(As)) {
1139            self.parse_ident_or_underscore().map(Some)
1140        } else {
1141            Ok(None)
1142        }
1143    }
1144
1145    fn parse_ident_or_underscore(&mut self) -> PResult<'a, Ident> {
1146        match self.token.ident() {
1147            Some((ident @ Ident { name: kw::Underscore, .. }, IdentIsRaw::No)) => {
1148                self.bump();
1149                Ok(ident)
1150            }
1151            _ => self.parse_ident(),
1152        }
1153    }
1154
1155    /// Parses `extern crate` links.
1156    ///
1157    /// # Examples
1158    ///
1159    /// ```ignore (illustrative)
1160    /// extern crate foo;
1161    /// extern crate bar as foo;
1162    /// ```
1163    fn parse_item_extern_crate(&mut self) -> PResult<'a, ItemKind> {
1164        // Accept `extern crate name-like-this` for better diagnostics
1165        let orig_ident = self.parse_crate_name_with_dashes()?;
1166        let (orig_name, item_ident) = if let Some(rename) = self.parse_rename()? {
1167            (Some(orig_ident.name), rename)
1168        } else {
1169            (None, orig_ident)
1170        };
1171        self.expect_semi()?;
1172        Ok(ItemKind::ExternCrate(orig_name, item_ident))
1173    }
1174
1175    fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, Ident> {
1176        let ident = if self.token.is_keyword(kw::SelfLower) {
1177            self.parse_path_segment_ident()
1178        } else {
1179            self.parse_ident()
1180        }?;
1181
1182        let dash = exp!(Minus);
1183        if self.token != dash.tok {
1184            return Ok(ident);
1185        }
1186
1187        // Accept `extern crate name-like-this` for better diagnostics.
1188        let mut dashes = vec![];
1189        let mut idents = vec![];
1190        while self.eat(dash) {
1191            dashes.push(self.prev_token.span);
1192            idents.push(self.parse_ident()?);
1193        }
1194
1195        let fixed_name_sp = ident.span.to(idents.last().unwrap().span);
1196        let mut fixed_name = ident.name.to_string();
1197        for part in idents {
1198            write!(fixed_name, "_{}", part.name).unwrap();
1199        }
1200
1201        self.dcx().emit_err(errors::ExternCrateNameWithDashes {
1202            span: fixed_name_sp,
1203            sugg: errors::ExternCrateNameWithDashesSugg { dashes },
1204        });
1205
1206        Ok(Ident::from_str_and_span(&fixed_name, fixed_name_sp))
1207    }
1208
1209    /// Parses `extern` for foreign ABIs modules.
1210    ///
1211    /// `extern` is expected to have been consumed before calling this method.
1212    ///
1213    /// # Examples
1214    ///
1215    /// ```ignore (only-for-syntax-highlight)
1216    /// extern "C" {}
1217    /// extern {}
1218    /// ```
1219    fn parse_item_foreign_mod(
1220        &mut self,
1221        attrs: &mut AttrVec,
1222        mut safety: Safety,
1223    ) -> PResult<'a, ItemKind> {
1224        let extern_span = self.prev_token_uninterpolated_span();
1225        let abi = self.parse_abi(); // ABI?
1226        // FIXME: This recovery should be tested better.
1227        if safety == Safety::Default
1228            && self.token.is_keyword(kw::Unsafe)
1229            && self.look_ahead(1, |t| *t == token::OpenBrace)
1230        {
1231            self.expect(exp!(OpenBrace)).unwrap_err().emit();
1232            safety = Safety::Unsafe(self.token.span);
1233            let _ = self.eat_keyword(exp!(Unsafe));
1234        }
1235        Ok(ItemKind::ForeignMod(ast::ForeignMod {
1236            extern_span,
1237            safety,
1238            abi,
1239            items: self.parse_item_list(attrs, |p| p.parse_foreign_item(ForceCollect::No))?,
1240        }))
1241    }
1242
1243    /// Parses a foreign item (one in an `extern { ... }` block).
1244    pub fn parse_foreign_item(
1245        &mut self,
1246        force_collect: ForceCollect,
1247    ) -> PResult<'a, Option<Option<Box<ForeignItem>>>> {
1248        let fn_parse_mode =
1249            FnParseMode { req_name: |_| true, context: FnContext::Free, req_body: false };
1250        Ok(self.parse_item_(fn_parse_mode, force_collect)?.map(
1251            |Item { attrs, id, span, vis, kind, tokens }| {
1252                let kind = match ForeignItemKind::try_from(kind) {
1253                    Ok(kind) => kind,
1254                    Err(kind) => match kind {
1255                        ItemKind::Const(box ConstItem { ident, ty, expr, .. }) => {
1256                            let const_span = Some(span.with_hi(ident.span.lo()))
1257                                .filter(|span| span.can_be_used_for_suggestions());
1258                            self.dcx().emit_err(errors::ExternItemCannotBeConst {
1259                                ident_span: ident.span,
1260                                const_span,
1261                            });
1262                            ForeignItemKind::Static(Box::new(StaticItem {
1263                                ident,
1264                                ty,
1265                                mutability: Mutability::Not,
1266                                expr,
1267                                safety: Safety::Default,
1268                                define_opaque: None,
1269                            }))
1270                        }
1271                        _ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
1272                    },
1273                };
1274                Some(Box::new(Item { attrs, id, span, vis, kind, tokens }))
1275            },
1276        ))
1277    }
1278
1279    fn error_bad_item_kind<T>(&self, span: Span, kind: &ItemKind, ctx: &'static str) -> Option<T> {
1280        // FIXME(#100717): needs variant for each `ItemKind` (instead of using `ItemKind::descr()`)
1281        let span = self.psess.source_map().guess_head_span(span);
1282        let descr = kind.descr();
1283        let help = match kind {
1284            ItemKind::DelegationMac(deleg) if deleg.suffixes.is_none() => false,
1285            _ => true,
1286        };
1287        self.dcx().emit_err(errors::BadItemKind { span, descr, ctx, help });
1288        None
1289    }
1290
1291    fn is_use_closure(&self) -> bool {
1292        if self.token.is_keyword(kw::Use) {
1293            // Check if this could be a closure.
1294            self.look_ahead(1, |token| {
1295                // Move or Async here would be an error but still we're parsing a closure
1296                let dist =
1297                    if token.is_keyword(kw::Move) || token.is_keyword(kw::Async) { 2 } else { 1 };
1298
1299                self.look_ahead(dist, |token| matches!(token.kind, token::Or | token::OrOr))
1300            })
1301        } else {
1302            false
1303        }
1304    }
1305
1306    fn is_unsafe_foreign_mod(&self) -> bool {
1307        // Look for `unsafe`.
1308        if !self.token.is_keyword(kw::Unsafe) {
1309            return false;
1310        }
1311        // Look for `extern`.
1312        if !self.is_keyword_ahead(1, &[kw::Extern]) {
1313            return false;
1314        }
1315
1316        // Look for the optional ABI string literal.
1317        let n = if self.look_ahead(2, |t| t.can_begin_string_literal()) { 3 } else { 2 };
1318
1319        // Look for the `{`. Use `tree_look_ahead` because the ABI (if present)
1320        // might be a metavariable i.e. an invisible-delimited sequence, and
1321        // `tree_look_ahead` will consider that a single element when looking
1322        // ahead.
1323        self.tree_look_ahead(n, |t| matches!(t, TokenTree::Delimited(_, _, Delimiter::Brace, _)))
1324            == Some(true)
1325    }
1326
1327    fn is_static_global(&mut self) -> bool {
1328        if self.check_keyword(exp!(Static)) {
1329            // Check if this could be a closure.
1330            !self.look_ahead(1, |token| {
1331                if token.is_keyword(kw::Move) || token.is_keyword(kw::Use) {
1332                    return true;
1333                }
1334                matches!(token.kind, token::Or | token::OrOr)
1335            })
1336        } else {
1337            // `$qual static`
1338            (self.check_keyword(exp!(Unsafe)) || self.check_keyword(exp!(Safe)))
1339                && self.look_ahead(1, |t| t.is_keyword(kw::Static))
1340        }
1341    }
1342
1343    /// Recover on `const mut` with `const` already eaten.
1344    fn recover_const_mut(&mut self, const_span: Span) {
1345        if self.eat_keyword(exp!(Mut)) {
1346            let span = self.prev_token.span;
1347            self.dcx()
1348                .emit_err(errors::ConstGlobalCannotBeMutable { ident_span: span, const_span });
1349        } else if self.eat_keyword(exp!(Let)) {
1350            let span = self.prev_token.span;
1351            self.dcx().emit_err(errors::ConstLetMutuallyExclusive { span: const_span.to(span) });
1352        }
1353    }
1354
1355    /// Recover on `const impl` with `const` already eaten.
1356    fn recover_const_impl(
1357        &mut self,
1358        const_span: Span,
1359        attrs: &mut AttrVec,
1360        defaultness: Defaultness,
1361    ) -> PResult<'a, ItemKind> {
1362        let impl_span = self.token.span;
1363        let err = self.expected_ident_found_err();
1364
1365        // Only try to recover if this is implementing a trait for a type
1366        let mut item_kind = match self.parse_item_impl(attrs, defaultness) {
1367            Ok(item_kind) => item_kind,
1368            Err(recovery_error) => {
1369                // Recovery failed, raise the "expected identifier" error
1370                recovery_error.cancel();
1371                return Err(err);
1372            }
1373        };
1374
1375        match &mut item_kind {
1376            ItemKind::Impl(Impl { of_trait: Some(of_trait), .. }) => {
1377                of_trait.constness = Const::Yes(const_span);
1378
1379                let before_trait = of_trait.trait_ref.path.span.shrink_to_lo();
1380                let const_up_to_impl = const_span.with_hi(impl_span.lo());
1381                err.with_multipart_suggestion(
1382                    "you might have meant to write a const trait impl",
1383                    vec![(const_up_to_impl, "".to_owned()), (before_trait, "const ".to_owned())],
1384                    Applicability::MaybeIncorrect,
1385                )
1386                .emit();
1387            }
1388            ItemKind::Impl { .. } => return Err(err),
1389            _ => unreachable!(),
1390        }
1391
1392        Ok(item_kind)
1393    }
1394
1395    /// Parse a static item with the prefix `"static" "mut"?` already parsed and stored in
1396    /// `mutability`.
1397    ///
1398    /// ```ebnf
1399    /// Static = "static" "mut"? $ident ":" $ty (= $expr)? ";" ;
1400    /// ```
1401    fn parse_static_item(
1402        &mut self,
1403        safety: Safety,
1404        mutability: Mutability,
1405    ) -> PResult<'a, ItemKind> {
1406        let ident = self.parse_ident()?;
1407
1408        if self.token == TokenKind::Lt && self.may_recover() {
1409            let generics = self.parse_generics()?;
1410            self.dcx().emit_err(errors::StaticWithGenerics { span: generics.span });
1411        }
1412
1413        // Parse the type of a static item. That is, the `":" $ty` fragment.
1414        // FIXME: This could maybe benefit from `.may_recover()`?
1415        let ty = match (self.eat(exp!(Colon)), self.check(exp!(Eq)) | self.check(exp!(Semi))) {
1416            (true, false) => self.parse_ty()?,
1417            // If there wasn't a `:` or the colon was followed by a `=` or `;`, recover a missing
1418            // type.
1419            (colon, _) => self.recover_missing_global_item_type(colon, Some(mutability)),
1420        };
1421
1422        let expr = if self.eat(exp!(Eq)) { Some(self.parse_expr()?) } else { None };
1423
1424        self.expect_semi()?;
1425
1426        let item = StaticItem { ident, ty, safety, mutability, expr, define_opaque: None };
1427        Ok(ItemKind::Static(Box::new(item)))
1428    }
1429
1430    /// Parse a constant item with the prefix `"const"` already parsed.
1431    ///
1432    /// ```ebnf
1433    /// Const = "const" ($ident | "_") Generics ":" $ty (= $expr)? WhereClause ";" ;
1434    /// ```
1435    fn parse_const_item(
1436        &mut self,
1437    ) -> PResult<'a, (Ident, Generics, Box<Ty>, Option<Box<ast::Expr>>)> {
1438        let ident = self.parse_ident_or_underscore()?;
1439
1440        let mut generics = self.parse_generics()?;
1441
1442        // Check the span for emptiness instead of the list of parameters in order to correctly
1443        // recognize and subsequently flag empty parameter lists (`<>`) as unstable.
1444        if !generics.span.is_empty() {
1445            self.psess.gated_spans.gate(sym::generic_const_items, generics.span);
1446        }
1447
1448        // Parse the type of a constant item. That is, the `":" $ty` fragment.
1449        // FIXME: This could maybe benefit from `.may_recover()`?
1450        let ty = match (
1451            self.eat(exp!(Colon)),
1452            self.check(exp!(Eq)) | self.check(exp!(Semi)) | self.check_keyword(exp!(Where)),
1453        ) {
1454            (true, false) => self.parse_ty()?,
1455            // If there wasn't a `:` or the colon was followed by a `=`, `;` or `where`, recover a missing type.
1456            (colon, _) => self.recover_missing_global_item_type(colon, None),
1457        };
1458
1459        // Proactively parse a where-clause to be able to provide a good error message in case we
1460        // encounter the item body following it.
1461        let before_where_clause =
1462            if self.may_recover() { self.parse_where_clause()? } else { WhereClause::default() };
1463
1464        let expr = if self.eat(exp!(Eq)) { Some(self.parse_expr()?) } else { None };
1465
1466        let after_where_clause = self.parse_where_clause()?;
1467
1468        // Provide a nice error message if the user placed a where-clause before the item body.
1469        // Users may be tempted to write such code if they are still used to the deprecated
1470        // where-clause location on type aliases and associated types. See also #89122.
1471        if before_where_clause.has_where_token
1472            && let Some(expr) = &expr
1473        {
1474            self.dcx().emit_err(errors::WhereClauseBeforeConstBody {
1475                span: before_where_clause.span,
1476                name: ident.span,
1477                body: expr.span,
1478                sugg: if !after_where_clause.has_where_token {
1479                    self.psess.source_map().span_to_snippet(expr.span).ok().map(|body| {
1480                        errors::WhereClauseBeforeConstBodySugg {
1481                            left: before_where_clause.span.shrink_to_lo(),
1482                            snippet: body,
1483                            right: before_where_clause.span.shrink_to_hi().to(expr.span),
1484                        }
1485                    })
1486                } else {
1487                    // FIXME(generic_const_items): Provide a structured suggestion to merge the first
1488                    // where-clause into the second one.
1489                    None
1490                },
1491            });
1492        }
1493
1494        // Merge the predicates of both where-clauses since either one can be relevant.
1495        // If we didn't parse a body (which is valid for associated consts in traits) and we were
1496        // allowed to recover, `before_where_clause` contains the predicates, otherwise they are
1497        // in `after_where_clause`. Further, both of them might contain predicates iff two
1498        // where-clauses were provided which is syntactically ill-formed but we want to recover from
1499        // it and treat them as one large where-clause.
1500        let mut predicates = before_where_clause.predicates;
1501        predicates.extend(after_where_clause.predicates);
1502        let where_clause = WhereClause {
1503            has_where_token: before_where_clause.has_where_token
1504                || after_where_clause.has_where_token,
1505            predicates,
1506            span: if after_where_clause.has_where_token {
1507                after_where_clause.span
1508            } else {
1509                before_where_clause.span
1510            },
1511        };
1512
1513        if where_clause.has_where_token {
1514            self.psess.gated_spans.gate(sym::generic_const_items, where_clause.span);
1515        }
1516
1517        generics.where_clause = where_clause;
1518
1519        self.expect_semi()?;
1520
1521        Ok((ident, generics, ty, expr))
1522    }
1523
1524    /// We were supposed to parse `":" $ty` but the `:` or the type was missing.
1525    /// This means that the type is missing.
1526    fn recover_missing_global_item_type(
1527        &mut self,
1528        colon_present: bool,
1529        m: Option<Mutability>,
1530    ) -> Box<Ty> {
1531        // Construct the error and stash it away with the hope
1532        // that typeck will later enrich the error with a type.
1533        let kind = match m {
1534            Some(Mutability::Mut) => "static mut",
1535            Some(Mutability::Not) => "static",
1536            None => "const",
1537        };
1538
1539        let colon = match colon_present {
1540            true => "",
1541            false => ":",
1542        };
1543
1544        let span = self.prev_token.span.shrink_to_hi();
1545        let err = self.dcx().create_err(errors::MissingConstType { span, colon, kind });
1546        err.stash(span, StashKey::ItemNoType);
1547
1548        // The user intended that the type be inferred,
1549        // so treat this as if the user wrote e.g. `const A: _ = expr;`.
1550        Box::new(Ty { kind: TyKind::Infer, span, id: ast::DUMMY_NODE_ID, tokens: None })
1551    }
1552
1553    /// Parses an enum declaration.
1554    fn parse_item_enum(&mut self) -> PResult<'a, ItemKind> {
1555        if self.token.is_keyword(kw::Struct) {
1556            let span = self.prev_token.span.to(self.token.span);
1557            let err = errors::EnumStructMutuallyExclusive { span };
1558            if self.look_ahead(1, |t| t.is_ident()) {
1559                self.bump();
1560                self.dcx().emit_err(err);
1561            } else {
1562                return Err(self.dcx().create_err(err));
1563            }
1564        }
1565
1566        let prev_span = self.prev_token.span;
1567        let ident = self.parse_ident()?;
1568        let mut generics = self.parse_generics()?;
1569        generics.where_clause = self.parse_where_clause()?;
1570
1571        // Possibly recover `enum Foo;` instead of `enum Foo {}`
1572        let (variants, _) = if self.token == TokenKind::Semi {
1573            self.dcx().emit_err(errors::UseEmptyBlockNotSemi { span: self.token.span });
1574            self.bump();
1575            (thin_vec![], Trailing::No)
1576        } else {
1577            self.parse_delim_comma_seq(exp!(OpenBrace), exp!(CloseBrace), |p| {
1578                p.parse_enum_variant(ident.span)
1579            })
1580            .map_err(|mut err| {
1581                err.span_label(ident.span, "while parsing this enum");
1582                // Try to recover `enum Foo { ident : Ty }`.
1583                if self.prev_token.is_non_reserved_ident() && self.token == token::Colon {
1584                    let snapshot = self.create_snapshot_for_diagnostic();
1585                    self.bump();
1586                    match self.parse_ty() {
1587                        Ok(_) => {
1588                            err.span_suggestion_verbose(
1589                                prev_span,
1590                                "perhaps you meant to use `struct` here",
1591                                "struct",
1592                                Applicability::MaybeIncorrect,
1593                            );
1594                        }
1595                        Err(e) => {
1596                            e.cancel();
1597                        }
1598                    }
1599                    self.restore_snapshot(snapshot);
1600                }
1601                self.eat_to_tokens(&[exp!(CloseBrace)]);
1602                self.bump(); // }
1603                err
1604            })?
1605        };
1606
1607        let enum_definition = EnumDef { variants: variants.into_iter().flatten().collect() };
1608        Ok(ItemKind::Enum(ident, generics, enum_definition))
1609    }
1610
1611    fn parse_enum_variant(&mut self, span: Span) -> PResult<'a, Option<Variant>> {
1612        self.recover_vcs_conflict_marker();
1613        let variant_attrs = self.parse_outer_attributes()?;
1614        self.recover_vcs_conflict_marker();
1615        let help = "enum variants can be `Variant`, `Variant = <integer>`, \
1616                    `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`";
1617        self.collect_tokens(None, variant_attrs, ForceCollect::No, |this, variant_attrs| {
1618            let vlo = this.token.span;
1619
1620            let vis = this.parse_visibility(FollowedByType::No)?;
1621            if !this.recover_nested_adt_item(kw::Enum)? {
1622                return Ok((None, Trailing::No, UsePreAttrPos::No));
1623            }
1624            let ident = this.parse_field_ident("enum", vlo)?;
1625
1626            if this.token == token::Bang {
1627                if let Err(err) = this.unexpected() {
1628                    err.with_note(fluent::parse_macro_expands_to_enum_variant).emit();
1629                }
1630
1631                this.bump();
1632                this.parse_delim_args()?;
1633
1634                return Ok((None, Trailing::from(this.token == token::Comma), UsePreAttrPos::No));
1635            }
1636
1637            let struct_def = if this.check(exp!(OpenBrace)) {
1638                // Parse a struct variant.
1639                let (fields, recovered) =
1640                    match this.parse_record_struct_body("struct", ident.span, false) {
1641                        Ok((fields, recovered)) => (fields, recovered),
1642                        Err(mut err) => {
1643                            if this.token == token::Colon {
1644                                // We handle `enum` to `struct` suggestion in the caller.
1645                                return Err(err);
1646                            }
1647                            this.eat_to_tokens(&[exp!(CloseBrace)]);
1648                            this.bump(); // }
1649                            err.span_label(span, "while parsing this enum");
1650                            err.help(help);
1651                            let guar = err.emit();
1652                            (thin_vec![], Recovered::Yes(guar))
1653                        }
1654                    };
1655                VariantData::Struct { fields, recovered }
1656            } else if this.check(exp!(OpenParen)) {
1657                let body = match this.parse_tuple_struct_body() {
1658                    Ok(body) => body,
1659                    Err(mut err) => {
1660                        if this.token == token::Colon {
1661                            // We handle `enum` to `struct` suggestion in the caller.
1662                            return Err(err);
1663                        }
1664                        this.eat_to_tokens(&[exp!(CloseParen)]);
1665                        this.bump(); // )
1666                        err.span_label(span, "while parsing this enum");
1667                        err.help(help);
1668                        err.emit();
1669                        thin_vec![]
1670                    }
1671                };
1672                VariantData::Tuple(body, DUMMY_NODE_ID)
1673            } else {
1674                VariantData::Unit(DUMMY_NODE_ID)
1675            };
1676
1677            let disr_expr =
1678                if this.eat(exp!(Eq)) { Some(this.parse_expr_anon_const()?) } else { None };
1679
1680            let vr = ast::Variant {
1681                ident,
1682                vis,
1683                id: DUMMY_NODE_ID,
1684                attrs: variant_attrs,
1685                data: struct_def,
1686                disr_expr,
1687                span: vlo.to(this.prev_token.span),
1688                is_placeholder: false,
1689            };
1690
1691            Ok((Some(vr), Trailing::from(this.token == token::Comma), UsePreAttrPos::No))
1692        })
1693        .map_err(|mut err| {
1694            err.help(help);
1695            err
1696        })
1697    }
1698
1699    /// Parses `struct Foo { ... }`.
1700    fn parse_item_struct(&mut self) -> PResult<'a, ItemKind> {
1701        let ident = self.parse_ident()?;
1702
1703        let mut generics = self.parse_generics()?;
1704
1705        // There is a special case worth noting here, as reported in issue #17904.
1706        // If we are parsing a tuple struct it is the case that the where clause
1707        // should follow the field list. Like so:
1708        //
1709        // struct Foo<T>(T) where T: Copy;
1710        //
1711        // If we are parsing a normal record-style struct it is the case
1712        // that the where clause comes before the body, and after the generics.
1713        // So if we look ahead and see a brace or a where-clause we begin
1714        // parsing a record style struct.
1715        //
1716        // Otherwise if we look ahead and see a paren we parse a tuple-style
1717        // struct.
1718
1719        let vdata = if self.token.is_keyword(kw::Where) {
1720            let tuple_struct_body;
1721            (generics.where_clause, tuple_struct_body) =
1722                self.parse_struct_where_clause(ident, generics.span)?;
1723
1724            if let Some(body) = tuple_struct_body {
1725                // If we see a misplaced tuple struct body: `struct Foo<T> where T: Copy, (T);`
1726                let body = VariantData::Tuple(body, DUMMY_NODE_ID);
1727                self.expect_semi()?;
1728                body
1729            } else if self.eat(exp!(Semi)) {
1730                // If we see a: `struct Foo<T> where T: Copy;` style decl.
1731                VariantData::Unit(DUMMY_NODE_ID)
1732            } else {
1733                // If we see: `struct Foo<T> where T: Copy { ... }`
1734                let (fields, recovered) = self.parse_record_struct_body(
1735                    "struct",
1736                    ident.span,
1737                    generics.where_clause.has_where_token,
1738                )?;
1739                VariantData::Struct { fields, recovered }
1740            }
1741        // No `where` so: `struct Foo<T>;`
1742        } else if self.eat(exp!(Semi)) {
1743            VariantData::Unit(DUMMY_NODE_ID)
1744        // Record-style struct definition
1745        } else if self.token == token::OpenBrace {
1746            let (fields, recovered) = self.parse_record_struct_body(
1747                "struct",
1748                ident.span,
1749                generics.where_clause.has_where_token,
1750            )?;
1751            VariantData::Struct { fields, recovered }
1752        // Tuple-style struct definition with optional where-clause.
1753        } else if self.token == token::OpenParen {
1754            let body = VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID);
1755            generics.where_clause = self.parse_where_clause()?;
1756            self.expect_semi()?;
1757            body
1758        } else {
1759            let err = errors::UnexpectedTokenAfterStructName::new(self.token.span, self.token);
1760            return Err(self.dcx().create_err(err));
1761        };
1762
1763        Ok(ItemKind::Struct(ident, generics, vdata))
1764    }
1765
1766    /// Parses `union Foo { ... }`.
1767    fn parse_item_union(&mut self) -> PResult<'a, ItemKind> {
1768        let ident = self.parse_ident()?;
1769
1770        let mut generics = self.parse_generics()?;
1771
1772        let vdata = if self.token.is_keyword(kw::Where) {
1773            generics.where_clause = self.parse_where_clause()?;
1774            let (fields, recovered) = self.parse_record_struct_body(
1775                "union",
1776                ident.span,
1777                generics.where_clause.has_where_token,
1778            )?;
1779            VariantData::Struct { fields, recovered }
1780        } else if self.token == token::OpenBrace {
1781            let (fields, recovered) = self.parse_record_struct_body(
1782                "union",
1783                ident.span,
1784                generics.where_clause.has_where_token,
1785            )?;
1786            VariantData::Struct { fields, recovered }
1787        } else {
1788            let token_str = super::token_descr(&self.token);
1789            let msg = format!("expected `where` or `{{` after union name, found {token_str}");
1790            let mut err = self.dcx().struct_span_err(self.token.span, msg);
1791            err.span_label(self.token.span, "expected `where` or `{` after union name");
1792            return Err(err);
1793        };
1794
1795        Ok(ItemKind::Union(ident, generics, vdata))
1796    }
1797
1798    /// This function parses the fields of record structs:
1799    ///
1800    ///   - `struct S { ... }`
1801    ///   - `enum E { Variant { ... } }`
1802    pub(crate) fn parse_record_struct_body(
1803        &mut self,
1804        adt_ty: &str,
1805        ident_span: Span,
1806        parsed_where: bool,
1807    ) -> PResult<'a, (ThinVec<FieldDef>, Recovered)> {
1808        let mut fields = ThinVec::new();
1809        let mut recovered = Recovered::No;
1810        if self.eat(exp!(OpenBrace)) {
1811            while self.token != token::CloseBrace {
1812                match self.parse_field_def(adt_ty, ident_span) {
1813                    Ok(field) => {
1814                        fields.push(field);
1815                    }
1816                    Err(mut err) => {
1817                        self.consume_block(
1818                            exp!(OpenBrace),
1819                            exp!(CloseBrace),
1820                            ConsumeClosingDelim::No,
1821                        );
1822                        err.span_label(ident_span, format!("while parsing this {adt_ty}"));
1823                        let guar = err.emit();
1824                        recovered = Recovered::Yes(guar);
1825                        break;
1826                    }
1827                }
1828            }
1829            self.expect(exp!(CloseBrace))?;
1830        } else {
1831            let token_str = super::token_descr(&self.token);
1832            let where_str = if parsed_where { "" } else { "`where`, or " };
1833            let msg = format!("expected {where_str}`{{` after struct name, found {token_str}");
1834            let mut err = self.dcx().struct_span_err(self.token.span, msg);
1835            err.span_label(self.token.span, format!("expected {where_str}`{{` after struct name",));
1836            return Err(err);
1837        }
1838
1839        Ok((fields, recovered))
1840    }
1841
1842    fn parse_unsafe_field(&mut self) -> Safety {
1843        // not using parse_safety as that also accepts `safe`.
1844        if self.eat_keyword(exp!(Unsafe)) {
1845            let span = self.prev_token.span;
1846            self.psess.gated_spans.gate(sym::unsafe_fields, span);
1847            Safety::Unsafe(span)
1848        } else {
1849            Safety::Default
1850        }
1851    }
1852
1853    pub(super) fn parse_tuple_struct_body(&mut self) -> PResult<'a, ThinVec<FieldDef>> {
1854        // This is the case where we find `struct Foo<T>(T) where T: Copy;`
1855        // Unit like structs are handled in parse_item_struct function
1856        self.parse_paren_comma_seq(|p| {
1857            let attrs = p.parse_outer_attributes()?;
1858            p.collect_tokens(None, attrs, ForceCollect::No, |p, attrs| {
1859                let mut snapshot = None;
1860                if p.is_vcs_conflict_marker(&TokenKind::Shl, &TokenKind::Lt) {
1861                    // Account for `<<<<<<<` diff markers. We can't proactively error here because
1862                    // that can be a valid type start, so we snapshot and reparse only we've
1863                    // encountered another parse error.
1864                    snapshot = Some(p.create_snapshot_for_diagnostic());
1865                }
1866                let lo = p.token.span;
1867                let vis = match p.parse_visibility(FollowedByType::Yes) {
1868                    Ok(vis) => vis,
1869                    Err(err) => {
1870                        if let Some(ref mut snapshot) = snapshot {
1871                            snapshot.recover_vcs_conflict_marker();
1872                        }
1873                        return Err(err);
1874                    }
1875                };
1876                // Unsafe fields are not supported in tuple structs, as doing so would result in a
1877                // parsing ambiguity for `struct X(unsafe fn())`.
1878                let ty = match p.parse_ty() {
1879                    Ok(ty) => ty,
1880                    Err(err) => {
1881                        if let Some(ref mut snapshot) = snapshot {
1882                            snapshot.recover_vcs_conflict_marker();
1883                        }
1884                        return Err(err);
1885                    }
1886                };
1887                let mut default = None;
1888                if p.token == token::Eq {
1889                    let mut snapshot = p.create_snapshot_for_diagnostic();
1890                    snapshot.bump();
1891                    match snapshot.parse_expr_anon_const() {
1892                        Ok(const_expr) => {
1893                            let sp = ty.span.shrink_to_hi().to(const_expr.value.span);
1894                            p.psess.gated_spans.gate(sym::default_field_values, sp);
1895                            p.restore_snapshot(snapshot);
1896                            default = Some(const_expr);
1897                        }
1898                        Err(err) => {
1899                            err.cancel();
1900                        }
1901                    }
1902                }
1903
1904                Ok((
1905                    FieldDef {
1906                        span: lo.to(ty.span),
1907                        vis,
1908                        safety: Safety::Default,
1909                        ident: None,
1910                        id: DUMMY_NODE_ID,
1911                        ty,
1912                        default,
1913                        attrs,
1914                        is_placeholder: false,
1915                    },
1916                    Trailing::from(p.token == token::Comma),
1917                    UsePreAttrPos::No,
1918                ))
1919            })
1920        })
1921        .map(|(r, _)| r)
1922    }
1923
1924    /// Parses an element of a struct declaration.
1925    fn parse_field_def(&mut self, adt_ty: &str, ident_span: Span) -> PResult<'a, FieldDef> {
1926        self.recover_vcs_conflict_marker();
1927        let attrs = self.parse_outer_attributes()?;
1928        self.recover_vcs_conflict_marker();
1929        self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
1930            let lo = this.token.span;
1931            let vis = this.parse_visibility(FollowedByType::No)?;
1932            let safety = this.parse_unsafe_field();
1933            this.parse_single_struct_field(adt_ty, lo, vis, safety, attrs, ident_span)
1934                .map(|field| (field, Trailing::No, UsePreAttrPos::No))
1935        })
1936    }
1937
1938    /// Parses a structure field declaration.
1939    fn parse_single_struct_field(
1940        &mut self,
1941        adt_ty: &str,
1942        lo: Span,
1943        vis: Visibility,
1944        safety: Safety,
1945        attrs: AttrVec,
1946        ident_span: Span,
1947    ) -> PResult<'a, FieldDef> {
1948        let a_var = self.parse_name_and_ty(adt_ty, lo, vis, safety, attrs)?;
1949        match self.token.kind {
1950            token::Comma => {
1951                self.bump();
1952            }
1953            token::Semi => {
1954                self.bump();
1955                let sp = self.prev_token.span;
1956                let mut err =
1957                    self.dcx().struct_span_err(sp, format!("{adt_ty} fields are separated by `,`"));
1958                err.span_suggestion_short(
1959                    sp,
1960                    "replace `;` with `,`",
1961                    ",",
1962                    Applicability::MachineApplicable,
1963                );
1964                err.span_label(ident_span, format!("while parsing this {adt_ty}"));
1965                err.emit();
1966            }
1967            token::CloseBrace => {}
1968            token::DocComment(..) => {
1969                let previous_span = self.prev_token.span;
1970                let mut err = errors::DocCommentDoesNotDocumentAnything {
1971                    span: self.token.span,
1972                    missing_comma: None,
1973                };
1974                self.bump(); // consume the doc comment
1975                if self.eat(exp!(Comma)) || self.token == token::CloseBrace {
1976                    self.dcx().emit_err(err);
1977                } else {
1978                    let sp = previous_span.shrink_to_hi();
1979                    err.missing_comma = Some(sp);
1980                    return Err(self.dcx().create_err(err));
1981                }
1982            }
1983            _ => {
1984                let sp = self.prev_token.span.shrink_to_hi();
1985                let msg =
1986                    format!("expected `,`, or `}}`, found {}", super::token_descr(&self.token));
1987
1988                // Try to recover extra trailing angle brackets
1989                if let TyKind::Path(_, Path { segments, .. }) = &a_var.ty.kind
1990                    && let Some(last_segment) = segments.last()
1991                {
1992                    let guar = self.check_trailing_angle_brackets(
1993                        last_segment,
1994                        &[exp!(Comma), exp!(CloseBrace)],
1995                    );
1996                    if let Some(_guar) = guar {
1997                        // Handle a case like `Vec<u8>>,` where we can continue parsing fields
1998                        // after the comma
1999                        let _ = self.eat(exp!(Comma));
2000
2001                        // `check_trailing_angle_brackets` already emitted a nicer error, as
2002                        // proven by the presence of `_guar`. We can continue parsing.
2003                        return Ok(a_var);
2004                    }
2005                }
2006
2007                let mut err = self.dcx().struct_span_err(sp, msg);
2008
2009                if self.token.is_ident()
2010                    || (self.token == TokenKind::Pound
2011                        && (self.look_ahead(1, |t| t == &token::OpenBracket)))
2012                {
2013                    // This is likely another field, TokenKind::Pound is used for `#[..]`
2014                    // attribute for next field. Emit the diagnostic and continue parsing.
2015                    err.span_suggestion(
2016                        sp,
2017                        "try adding a comma",
2018                        ",",
2019                        Applicability::MachineApplicable,
2020                    );
2021                    err.emit();
2022                } else {
2023                    return Err(err);
2024                }
2025            }
2026        }
2027        Ok(a_var)
2028    }
2029
2030    fn expect_field_ty_separator(&mut self) -> PResult<'a, ()> {
2031        if let Err(err) = self.expect(exp!(Colon)) {
2032            let sm = self.psess.source_map();
2033            let eq_typo = self.token == token::Eq && self.look_ahead(1, |t| t.is_path_start());
2034            let semi_typo = self.token == token::Semi
2035                && self.look_ahead(1, |t| {
2036                    t.is_path_start()
2037                    // We check that we are in a situation like `foo; bar` to avoid bad suggestions
2038                    // when there's no type and `;` was used instead of a comma.
2039                    && match (sm.lookup_line(self.token.span.hi()), sm.lookup_line(t.span.lo())) {
2040                        (Ok(l), Ok(r)) => l.line == r.line,
2041                        _ => true,
2042                    }
2043                });
2044            if eq_typo || semi_typo {
2045                self.bump();
2046                // Gracefully handle small typos.
2047                err.with_span_suggestion_short(
2048                    self.prev_token.span,
2049                    "field names and their types are separated with `:`",
2050                    ":",
2051                    Applicability::MachineApplicable,
2052                )
2053                .emit();
2054            } else {
2055                return Err(err);
2056            }
2057        }
2058        Ok(())
2059    }
2060
2061    /// Parses a structure field.
2062    fn parse_name_and_ty(
2063        &mut self,
2064        adt_ty: &str,
2065        lo: Span,
2066        vis: Visibility,
2067        safety: Safety,
2068        attrs: AttrVec,
2069    ) -> PResult<'a, FieldDef> {
2070        let name = self.parse_field_ident(adt_ty, lo)?;
2071        if self.token == token::Bang {
2072            if let Err(mut err) = self.unexpected() {
2073                // Encounter the macro invocation
2074                err.subdiagnostic(MacroExpandsToAdtField { adt_ty });
2075                return Err(err);
2076            }
2077        }
2078        self.expect_field_ty_separator()?;
2079        let ty = self.parse_ty()?;
2080        if self.token == token::Colon && self.look_ahead(1, |&t| t != token::Colon) {
2081            self.dcx()
2082                .struct_span_err(self.token.span, "found single colon in a struct field type path")
2083                .with_span_suggestion_verbose(
2084                    self.token.span,
2085                    "write a path separator here",
2086                    "::",
2087                    Applicability::MaybeIncorrect,
2088                )
2089                .emit();
2090        }
2091        let default = if self.token == token::Eq {
2092            self.bump();
2093            let const_expr = self.parse_expr_anon_const()?;
2094            let sp = ty.span.shrink_to_hi().to(const_expr.value.span);
2095            self.psess.gated_spans.gate(sym::default_field_values, sp);
2096            Some(const_expr)
2097        } else {
2098            None
2099        };
2100        Ok(FieldDef {
2101            span: lo.to(self.prev_token.span),
2102            ident: Some(name),
2103            vis,
2104            safety,
2105            id: DUMMY_NODE_ID,
2106            ty,
2107            default,
2108            attrs,
2109            is_placeholder: false,
2110        })
2111    }
2112
2113    /// Parses a field identifier. Specialized version of `parse_ident_common`
2114    /// for better diagnostics and suggestions.
2115    fn parse_field_ident(&mut self, adt_ty: &str, lo: Span) -> PResult<'a, Ident> {
2116        let (ident, is_raw) = self.ident_or_err(true)?;
2117        if matches!(is_raw, IdentIsRaw::No) && ident.is_reserved() {
2118            let snapshot = self.create_snapshot_for_diagnostic();
2119            let err = if self.check_fn_front_matter(false, Case::Sensitive) {
2120                let inherited_vis =
2121                    Visibility { span: DUMMY_SP, kind: VisibilityKind::Inherited, tokens: None };
2122                // We use `parse_fn` to get a span for the function
2123                let fn_parse_mode =
2124                    FnParseMode { req_name: |_| true, context: FnContext::Free, req_body: true };
2125                match self.parse_fn(
2126                    &mut AttrVec::new(),
2127                    fn_parse_mode,
2128                    lo,
2129                    &inherited_vis,
2130                    Case::Insensitive,
2131                ) {
2132                    Ok(_) => {
2133                        self.dcx().struct_span_err(
2134                            lo.to(self.prev_token.span),
2135                            format!("functions are not allowed in {adt_ty} definitions"),
2136                        )
2137                        .with_help(
2138                            "unlike in C++, Java, and C#, functions are declared in `impl` blocks",
2139                        )
2140                        .with_help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information")
2141                    }
2142                    Err(err) => {
2143                        err.cancel();
2144                        self.restore_snapshot(snapshot);
2145                        self.expected_ident_found_err()
2146                    }
2147                }
2148            } else if self.eat_keyword(exp!(Struct)) {
2149                match self.parse_item_struct() {
2150                    Ok(item) => {
2151                        let ItemKind::Struct(ident, ..) = item else { unreachable!() };
2152                        self.dcx()
2153                            .struct_span_err(
2154                                lo.with_hi(ident.span.hi()),
2155                                format!("structs are not allowed in {adt_ty} definitions"),
2156                            )
2157                            .with_help(
2158                                "consider creating a new `struct` definition instead of nesting",
2159                            )
2160                    }
2161                    Err(err) => {
2162                        err.cancel();
2163                        self.restore_snapshot(snapshot);
2164                        self.expected_ident_found_err()
2165                    }
2166                }
2167            } else {
2168                let mut err = self.expected_ident_found_err();
2169                if self.eat_keyword_noexpect(kw::Let)
2170                    && let removal_span = self.prev_token.span.until(self.token.span)
2171                    && let Ok(ident) = self
2172                        .parse_ident_common(false)
2173                        // Cancel this error, we don't need it.
2174                        .map_err(|err| err.cancel())
2175                    && self.token == TokenKind::Colon
2176                {
2177                    err.span_suggestion(
2178                        removal_span,
2179                        "remove this `let` keyword",
2180                        String::new(),
2181                        Applicability::MachineApplicable,
2182                    );
2183                    err.note("the `let` keyword is not allowed in `struct` fields");
2184                    err.note("see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information");
2185                    err.emit();
2186                    return Ok(ident);
2187                } else {
2188                    self.restore_snapshot(snapshot);
2189                }
2190                err
2191            };
2192            return Err(err);
2193        }
2194        self.bump();
2195        Ok(ident)
2196    }
2197
2198    /// Parses a declarative macro 2.0 definition.
2199    /// The `macro` keyword has already been parsed.
2200    /// ```ebnf
2201    /// MacBody = "{" TOKEN_STREAM "}" ;
2202    /// MacParams = "(" TOKEN_STREAM ")" ;
2203    /// DeclMac = "macro" Ident MacParams? MacBody ;
2204    /// ```
2205    fn parse_item_decl_macro(&mut self, lo: Span) -> PResult<'a, ItemKind> {
2206        let ident = self.parse_ident()?;
2207        let body = if self.check(exp!(OpenBrace)) {
2208            self.parse_delim_args()? // `MacBody`
2209        } else if self.check(exp!(OpenParen)) {
2210            let params = self.parse_token_tree(); // `MacParams`
2211            let pspan = params.span();
2212            if !self.check(exp!(OpenBrace)) {
2213                self.unexpected()?;
2214            }
2215            let body = self.parse_token_tree(); // `MacBody`
2216            // Convert `MacParams MacBody` into `{ MacParams => MacBody }`.
2217            let bspan = body.span();
2218            let arrow = TokenTree::token_alone(token::FatArrow, pspan.between(bspan)); // `=>`
2219            let tokens = TokenStream::new(vec![params, arrow, body]);
2220            let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
2221            Box::new(DelimArgs { dspan, delim: Delimiter::Brace, tokens })
2222        } else {
2223            self.unexpected_any()?
2224        };
2225
2226        self.psess.gated_spans.gate(sym::decl_macro, lo.to(self.prev_token.span));
2227        Ok(ItemKind::MacroDef(ident, ast::MacroDef { body, macro_rules: false }))
2228    }
2229
2230    /// Is this a possibly malformed start of a `macro_rules! foo` item definition?
2231    fn is_macro_rules_item(&mut self) -> IsMacroRulesItem {
2232        if self.check_keyword(exp!(MacroRules)) {
2233            let macro_rules_span = self.token.span;
2234
2235            if self.look_ahead(1, |t| *t == token::Bang) && self.look_ahead(2, |t| t.is_ident()) {
2236                return IsMacroRulesItem::Yes { has_bang: true };
2237            } else if self.look_ahead(1, |t| t.is_ident()) {
2238                // macro_rules foo
2239                self.dcx().emit_err(errors::MacroRulesMissingBang {
2240                    span: macro_rules_span,
2241                    hi: macro_rules_span.shrink_to_hi(),
2242                });
2243
2244                return IsMacroRulesItem::Yes { has_bang: false };
2245            }
2246        }
2247
2248        IsMacroRulesItem::No
2249    }
2250
2251    /// Parses a `macro_rules! foo { ... }` declarative macro.
2252    fn parse_item_macro_rules(
2253        &mut self,
2254        vis: &Visibility,
2255        has_bang: bool,
2256    ) -> PResult<'a, ItemKind> {
2257        self.expect_keyword(exp!(MacroRules))?; // `macro_rules`
2258
2259        if has_bang {
2260            self.expect(exp!(Bang))?; // `!`
2261        }
2262        let ident = self.parse_ident()?;
2263
2264        if self.eat(exp!(Bang)) {
2265            // Handle macro_rules! foo!
2266            let span = self.prev_token.span;
2267            self.dcx().emit_err(errors::MacroNameRemoveBang { span });
2268        }
2269
2270        let body = self.parse_delim_args()?;
2271        self.eat_semi_for_macro_if_needed(&body);
2272        self.complain_if_pub_macro(vis, true);
2273
2274        Ok(ItemKind::MacroDef(ident, ast::MacroDef { body, macro_rules: true }))
2275    }
2276
2277    /// Item macro invocations or `macro_rules!` definitions need inherited visibility.
2278    /// If that's not the case, emit an error.
2279    fn complain_if_pub_macro(&self, vis: &Visibility, macro_rules: bool) {
2280        if let VisibilityKind::Inherited = vis.kind {
2281            return;
2282        }
2283
2284        let vstr = pprust::vis_to_string(vis);
2285        let vstr = vstr.trim_end();
2286        if macro_rules {
2287            self.dcx().emit_err(errors::MacroRulesVisibility { span: vis.span, vis: vstr });
2288        } else {
2289            self.dcx().emit_err(errors::MacroInvocationVisibility { span: vis.span, vis: vstr });
2290        }
2291    }
2292
2293    fn eat_semi_for_macro_if_needed(&mut self, args: &DelimArgs) {
2294        if args.need_semicolon() && !self.eat(exp!(Semi)) {
2295            self.report_invalid_macro_expansion_item(args);
2296        }
2297    }
2298
2299    fn report_invalid_macro_expansion_item(&self, args: &DelimArgs) {
2300        let span = args.dspan.entire();
2301        let mut err = self.dcx().struct_span_err(
2302            span,
2303            "macros that expand to items must be delimited with braces or followed by a semicolon",
2304        );
2305        // FIXME: This will make us not emit the help even for declarative
2306        // macros within the same crate (that we can fix), which is sad.
2307        if !span.from_expansion() {
2308            let DelimSpan { open, close } = args.dspan;
2309            err.multipart_suggestion(
2310                "change the delimiters to curly braces",
2311                vec![(open, "{".to_string()), (close, '}'.to_string())],
2312                Applicability::MaybeIncorrect,
2313            );
2314            err.span_suggestion(
2315                span.with_neighbor(self.token.span).shrink_to_hi(),
2316                "add a semicolon",
2317                ';',
2318                Applicability::MaybeIncorrect,
2319            );
2320        }
2321        err.emit();
2322    }
2323
2324    /// Checks if current token is one of tokens which cannot be nested like `kw::Enum`. In case
2325    /// it is, we try to parse the item and report error about nested types.
2326    fn recover_nested_adt_item(&mut self, keyword: Symbol) -> PResult<'a, bool> {
2327        if (self.token.is_keyword(kw::Enum)
2328            || self.token.is_keyword(kw::Struct)
2329            || self.token.is_keyword(kw::Union))
2330            && self.look_ahead(1, |t| t.is_ident())
2331        {
2332            let kw_token = self.token;
2333            let kw_str = pprust::token_to_string(&kw_token);
2334            let item = self.parse_item(ForceCollect::No)?;
2335            let mut item = item.unwrap().span;
2336            if self.token == token::Comma {
2337                item = item.to(self.token.span);
2338            }
2339            self.dcx().emit_err(errors::NestedAdt {
2340                span: kw_token.span,
2341                item,
2342                kw_str,
2343                keyword: keyword.as_str(),
2344            });
2345            // We successfully parsed the item but we must inform the caller about nested problem.
2346            return Ok(false);
2347        }
2348        Ok(true)
2349    }
2350}
2351
2352/// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
2353///
2354/// The function decides if, per-parameter `p`, `p` must have a pattern or just a type.
2355///
2356/// This function pointer accepts an edition, because in edition 2015, trait declarations
2357/// were allowed to omit parameter names. In 2018, they became required.
2358type ReqName = fn(Edition) -> bool;
2359
2360/// Parsing configuration for functions.
2361///
2362/// The syntax of function items is slightly different within trait definitions,
2363/// impl blocks, and modules. It is still parsed using the same code, just with
2364/// different flags set, so that even when the input is wrong and produces a parse
2365/// error, it still gets into the AST and the rest of the parser and
2366/// type checker can run.
2367#[derive(Clone, Copy)]
2368pub(crate) struct FnParseMode {
2369    /// A function pointer that decides if, per-parameter `p`, `p` must have a
2370    /// pattern or just a type. This field affects parsing of the parameters list.
2371    ///
2372    /// ```text
2373    /// fn foo(alef: A) -> X { X::new() }
2374    ///        -----^^ affects parsing this part of the function signature
2375    ///        |
2376    ///        if req_name returns false, then this name is optional
2377    ///
2378    /// fn bar(A) -> X;
2379    ///        ^
2380    ///        |
2381    ///        if req_name returns true, this is an error
2382    /// ```
2383    ///
2384    /// Calling this function pointer should only return false if:
2385    ///
2386    ///   * The item is being parsed inside of a trait definition.
2387    ///     Within an impl block or a module, it should always evaluate
2388    ///     to true.
2389    ///   * The span is from Edition 2015. In particular, you can get a
2390    ///     2015 span inside a 2021 crate using macros.
2391    pub(super) req_name: ReqName,
2392    /// The context in which this function is parsed, used for diagnostics.
2393    /// This indicates the fn is a free function or method and so on.
2394    pub(super) context: FnContext,
2395    /// If this flag is set to `true`, then plain, semicolon-terminated function
2396    /// prototypes are not allowed here.
2397    ///
2398    /// ```text
2399    /// fn foo(alef: A) -> X { X::new() }
2400    ///                      ^^^^^^^^^^^^
2401    ///                      |
2402    ///                      this is always allowed
2403    ///
2404    /// fn bar(alef: A, bet: B) -> X;
2405    ///                             ^
2406    ///                             |
2407    ///                             if req_body is set to true, this is an error
2408    /// ```
2409    ///
2410    /// This field should only be set to false if the item is inside of a trait
2411    /// definition or extern block. Within an impl block or a module, it should
2412    /// always be set to true.
2413    pub(super) req_body: bool,
2414}
2415
2416/// The context in which a function is parsed.
2417/// FIXME(estebank, xizheyin): Use more variants.
2418#[derive(Clone, Copy, PartialEq, Eq)]
2419pub(crate) enum FnContext {
2420    /// Free context.
2421    Free,
2422    /// A Trait context.
2423    Trait,
2424    /// An Impl block.
2425    Impl,
2426}
2427
2428/// Parsing of functions and methods.
2429impl<'a> Parser<'a> {
2430    /// Parse a function starting from the front matter (`const ...`) to the body `{ ... }` or `;`.
2431    fn parse_fn(
2432        &mut self,
2433        attrs: &mut AttrVec,
2434        fn_parse_mode: FnParseMode,
2435        sig_lo: Span,
2436        vis: &Visibility,
2437        case: Case,
2438    ) -> PResult<'a, (Ident, FnSig, Generics, Option<Box<FnContract>>, Option<Box<Block>>)> {
2439        let fn_span = self.token.span;
2440        let header = self.parse_fn_front_matter(vis, case, FrontMatterParsingMode::Function)?; // `const ... fn`
2441        let ident = self.parse_ident()?; // `foo`
2442        let mut generics = self.parse_generics()?; // `<'a, T, ...>`
2443        let decl = match self.parse_fn_decl(&fn_parse_mode, AllowPlus::Yes, RecoverReturnSign::Yes)
2444        {
2445            Ok(decl) => decl,
2446            Err(old_err) => {
2447                // If we see `for Ty ...` then user probably meant `impl` item.
2448                if self.token.is_keyword(kw::For) {
2449                    old_err.cancel();
2450                    return Err(self.dcx().create_err(errors::FnTypoWithImpl { fn_span }));
2451                } else {
2452                    return Err(old_err);
2453                }
2454            }
2455        };
2456
2457        // Store the end of function parameters to give better diagnostics
2458        // inside `parse_fn_body()`.
2459        let fn_params_end = self.prev_token.span.shrink_to_hi();
2460
2461        let contract = self.parse_contract()?;
2462
2463        generics.where_clause = self.parse_where_clause()?; // `where T: Ord`
2464
2465        // `fn_params_end` is needed only when it's followed by a where clause.
2466        let fn_params_end =
2467            if generics.where_clause.has_where_token { Some(fn_params_end) } else { None };
2468
2469        let mut sig_hi = self.prev_token.span;
2470        // Either `;` or `{ ... }`.
2471        let body =
2472            self.parse_fn_body(attrs, &ident, &mut sig_hi, fn_parse_mode.req_body, fn_params_end)?;
2473        let fn_sig_span = sig_lo.to(sig_hi);
2474        Ok((ident, FnSig { header, decl, span: fn_sig_span }, generics, contract, body))
2475    }
2476
2477    /// Provide diagnostics when function body is not found
2478    fn error_fn_body_not_found(
2479        &mut self,
2480        ident_span: Span,
2481        req_body: bool,
2482        fn_params_end: Option<Span>,
2483    ) -> PResult<'a, ErrorGuaranteed> {
2484        let expected: &[_] =
2485            if req_body { &[exp!(OpenBrace)] } else { &[exp!(Semi), exp!(OpenBrace)] };
2486        match self.expected_one_of_not_found(&[], expected) {
2487            Ok(error_guaranteed) => Ok(error_guaranteed),
2488            Err(mut err) => {
2489                if self.token == token::CloseBrace {
2490                    // The enclosing `mod`, `trait` or `impl` is being closed, so keep the `fn` in
2491                    // the AST for typechecking.
2492                    err.span_label(ident_span, "while parsing this `fn`");
2493                    Ok(err.emit())
2494                } else if self.token == token::RArrow
2495                    && let Some(fn_params_end) = fn_params_end
2496                {
2497                    // Instead of a function body, the parser has encountered a right arrow
2498                    // preceded by a where clause.
2499
2500                    // Find whether token behind the right arrow is a function trait and
2501                    // store its span.
2502                    let fn_trait_span =
2503                        [sym::FnOnce, sym::FnMut, sym::Fn].into_iter().find_map(|symbol| {
2504                            if self.prev_token.is_ident_named(symbol) {
2505                                Some(self.prev_token.span)
2506                            } else {
2507                                None
2508                            }
2509                        });
2510
2511                    // Parse the return type (along with the right arrow) and store its span.
2512                    // If there's a parse error, cancel it and return the existing error
2513                    // as we are primarily concerned with the
2514                    // expected-function-body-but-found-something-else error here.
2515                    let arrow_span = self.token.span;
2516                    let ty_span = match self.parse_ret_ty(
2517                        AllowPlus::Yes,
2518                        RecoverQPath::Yes,
2519                        RecoverReturnSign::Yes,
2520                    ) {
2521                        Ok(ty_span) => ty_span.span().shrink_to_hi(),
2522                        Err(parse_error) => {
2523                            parse_error.cancel();
2524                            return Err(err);
2525                        }
2526                    };
2527                    let ret_ty_span = arrow_span.to(ty_span);
2528
2529                    if let Some(fn_trait_span) = fn_trait_span {
2530                        // Typo'd Fn* trait bounds such as
2531                        // fn foo<F>() where F: FnOnce -> () {}
2532                        err.subdiagnostic(errors::FnTraitMissingParen { span: fn_trait_span });
2533                    } else if let Ok(snippet) = self.psess.source_map().span_to_snippet(ret_ty_span)
2534                    {
2535                        // If token behind right arrow is not a Fn* trait, the programmer
2536                        // probably misplaced the return type after the where clause like
2537                        // `fn foo<T>() where T: Default -> u8 {}`
2538                        err.primary_message(
2539                            "return type should be specified after the function parameters",
2540                        );
2541                        err.subdiagnostic(errors::MisplacedReturnType {
2542                            fn_params_end,
2543                            snippet,
2544                            ret_ty_span,
2545                        });
2546                    }
2547                    Err(err)
2548                } else {
2549                    Err(err)
2550                }
2551            }
2552        }
2553    }
2554
2555    /// Parse the "body" of a function.
2556    /// This can either be `;` when there's no body,
2557    /// or e.g. a block when the function is a provided one.
2558    fn parse_fn_body(
2559        &mut self,
2560        attrs: &mut AttrVec,
2561        ident: &Ident,
2562        sig_hi: &mut Span,
2563        req_body: bool,
2564        fn_params_end: Option<Span>,
2565    ) -> PResult<'a, Option<Box<Block>>> {
2566        let has_semi = if req_body {
2567            self.token == TokenKind::Semi
2568        } else {
2569            // Only include `;` in list of expected tokens if body is not required
2570            self.check(exp!(Semi))
2571        };
2572        let (inner_attrs, body) = if has_semi {
2573            // Include the trailing semicolon in the span of the signature
2574            self.expect_semi()?;
2575            *sig_hi = self.prev_token.span;
2576            (AttrVec::new(), None)
2577        } else if self.check(exp!(OpenBrace)) || self.token.is_metavar_block() {
2578            self.parse_block_common(self.token.span, BlockCheckMode::Default, None)
2579                .map(|(attrs, body)| (attrs, Some(body)))?
2580        } else if self.token == token::Eq {
2581            // Recover `fn foo() = $expr;`.
2582            self.bump(); // `=`
2583            let eq_sp = self.prev_token.span;
2584            let _ = self.parse_expr()?;
2585            self.expect_semi()?; // `;`
2586            let span = eq_sp.to(self.prev_token.span);
2587            let guar = self.dcx().emit_err(errors::FunctionBodyEqualsExpr {
2588                span,
2589                sugg: errors::FunctionBodyEqualsExprSugg { eq: eq_sp, semi: self.prev_token.span },
2590            });
2591            (AttrVec::new(), Some(self.mk_block_err(span, guar)))
2592        } else {
2593            self.error_fn_body_not_found(ident.span, req_body, fn_params_end)?;
2594            (AttrVec::new(), None)
2595        };
2596        attrs.extend(inner_attrs);
2597        Ok(body)
2598    }
2599
2600    /// Is the current token the start of an `FnHeader` / not a valid parse?
2601    ///
2602    /// `check_pub` adds additional `pub` to the checks in case users place it
2603    /// wrongly, can be used to ensure `pub` never comes after `default`.
2604    pub(super) fn check_fn_front_matter(&mut self, check_pub: bool, case: Case) -> bool {
2605        const ALL_QUALS: &[ExpKeywordPair] = &[
2606            exp!(Pub),
2607            exp!(Gen),
2608            exp!(Const),
2609            exp!(Async),
2610            exp!(Unsafe),
2611            exp!(Safe),
2612            exp!(Extern),
2613        ];
2614
2615        // We use an over-approximation here.
2616        // `const const`, `fn const` won't parse, but we're not stepping over other syntax either.
2617        // `pub` is added in case users got confused with the ordering like `async pub fn`,
2618        // only if it wasn't preceded by `default` as `default pub` is invalid.
2619        let quals: &[_] = if check_pub {
2620            ALL_QUALS
2621        } else {
2622            &[exp!(Gen), exp!(Const), exp!(Async), exp!(Unsafe), exp!(Safe), exp!(Extern)]
2623        };
2624        self.check_keyword_case(exp!(Fn), case) // Definitely an `fn`.
2625            // `$qual fn` or `$qual $qual`:
2626            || quals.iter().any(|&exp| self.check_keyword_case(exp, case))
2627                && self.look_ahead(1, |t| {
2628                    // `$qual fn`, e.g. `const fn` or `async fn`.
2629                    t.is_keyword_case(kw::Fn, case)
2630                    // Two qualifiers `$qual $qual` is enough, e.g. `async unsafe`.
2631                    || (
2632                        (
2633                            t.is_non_raw_ident_where(|i|
2634                                quals.iter().any(|exp| exp.kw == i.name)
2635                                    // Rule out 2015 `const async: T = val`.
2636                                    && i.is_reserved()
2637                            )
2638                            || case == Case::Insensitive
2639                                && t.is_non_raw_ident_where(|i| quals.iter().any(|exp| {
2640                                    exp.kw.as_str() == i.name.as_str().to_lowercase()
2641                                }))
2642                        )
2643                        // Rule out `unsafe extern {`.
2644                        && !self.is_unsafe_foreign_mod()
2645                        // Rule out `async gen {` and `async gen move {`
2646                        && !self.is_async_gen_block()
2647                        // Rule out `const unsafe auto` and `const unsafe trait`.
2648                        && !self.is_keyword_ahead(2, &[kw::Auto, kw::Trait])
2649                    )
2650                })
2651            // `extern ABI fn`
2652            || self.check_keyword_case(exp!(Extern), case)
2653                // Use `tree_look_ahead` because `ABI` might be a metavariable,
2654                // i.e. an invisible-delimited sequence, and `tree_look_ahead`
2655                // will consider that a single element when looking ahead.
2656                && self.look_ahead(1, |t| t.can_begin_string_literal())
2657                && (self.tree_look_ahead(2, |tt| {
2658                    match tt {
2659                        TokenTree::Token(t, _) => t.is_keyword_case(kw::Fn, case),
2660                        TokenTree::Delimited(..) => false,
2661                    }
2662                }) == Some(true) ||
2663                    // This branch is only for better diagnostics; `pub`, `unsafe`, etc. are not
2664                    // allowed here.
2665                    (self.may_recover()
2666                        && self.tree_look_ahead(2, |tt| {
2667                            match tt {
2668                                TokenTree::Token(t, _) =>
2669                                    ALL_QUALS.iter().any(|exp| {
2670                                        t.is_keyword(exp.kw)
2671                                    }),
2672                                TokenTree::Delimited(..) => false,
2673                            }
2674                        }) == Some(true)
2675                        && self.tree_look_ahead(3, |tt| {
2676                            match tt {
2677                                TokenTree::Token(t, _) => t.is_keyword_case(kw::Fn, case),
2678                                TokenTree::Delimited(..) => false,
2679                            }
2680                        }) == Some(true)
2681                    )
2682                )
2683    }
2684
2685    /// Parses all the "front matter" (or "qualifiers") for a `fn` declaration,
2686    /// up to and including the `fn` keyword. The formal grammar is:
2687    ///
2688    /// ```text
2689    /// Extern = "extern" StringLit? ;
2690    /// FnQual = "const"? "async"? "unsafe"? Extern? ;
2691    /// FnFrontMatter = FnQual "fn" ;
2692    /// ```
2693    ///
2694    /// `vis` represents the visibility that was already parsed, if any. Use
2695    /// `Visibility::Inherited` when no visibility is known.
2696    ///
2697    /// If `parsing_mode` is `FrontMatterParsingMode::FunctionPtrType`, we error on `const` and `async` qualifiers,
2698    /// which are not allowed in function pointer types.
2699    pub(super) fn parse_fn_front_matter(
2700        &mut self,
2701        orig_vis: &Visibility,
2702        case: Case,
2703        parsing_mode: FrontMatterParsingMode,
2704    ) -> PResult<'a, FnHeader> {
2705        let sp_start = self.token.span;
2706        let constness = self.parse_constness(case);
2707        if parsing_mode == FrontMatterParsingMode::FunctionPtrType
2708            && let Const::Yes(const_span) = constness
2709        {
2710            self.dcx().emit_err(FnPointerCannotBeConst {
2711                span: const_span,
2712                suggestion: const_span.until(self.token.span),
2713            });
2714        }
2715
2716        let async_start_sp = self.token.span;
2717        let coroutine_kind = self.parse_coroutine_kind(case);
2718        if parsing_mode == FrontMatterParsingMode::FunctionPtrType
2719            && let Some(ast::CoroutineKind::Async { span: async_span, .. }) = coroutine_kind
2720        {
2721            self.dcx().emit_err(FnPointerCannotBeAsync {
2722                span: async_span,
2723                suggestion: async_span.until(self.token.span),
2724            });
2725        }
2726        // FIXME(gen_blocks): emit a similar error for `gen fn()`
2727
2728        let unsafe_start_sp = self.token.span;
2729        let safety = self.parse_safety(case);
2730
2731        let ext_start_sp = self.token.span;
2732        let ext = self.parse_extern(case);
2733
2734        if let Some(CoroutineKind::Async { span, .. }) = coroutine_kind {
2735            if span.is_rust_2015() {
2736                self.dcx().emit_err(errors::AsyncFnIn2015 {
2737                    span,
2738                    help: errors::HelpUseLatestEdition::new(),
2739                });
2740            }
2741        }
2742
2743        match coroutine_kind {
2744            Some(CoroutineKind::Gen { span, .. }) | Some(CoroutineKind::AsyncGen { span, .. }) => {
2745                self.psess.gated_spans.gate(sym::gen_blocks, span);
2746            }
2747            Some(CoroutineKind::Async { .. }) | None => {}
2748        }
2749
2750        if !self.eat_keyword_case(exp!(Fn), case) {
2751            // It is possible for `expect_one_of` to recover given the contents of
2752            // `self.expected_token_types`, therefore, do not use `self.unexpected()` which doesn't
2753            // account for this.
2754            match self.expect_one_of(&[], &[]) {
2755                Ok(Recovered::Yes(_)) => {}
2756                Ok(Recovered::No) => unreachable!(),
2757                Err(mut err) => {
2758                    // Qualifier keywords ordering check
2759                    enum WrongKw {
2760                        Duplicated(Span),
2761                        Misplaced(Span),
2762                        /// `MisplacedDisallowedQualifier` is only used instead of `Misplaced`,
2763                        /// when the misplaced keyword is disallowed by the current `FrontMatterParsingMode`.
2764                        /// In this case, we avoid generating the suggestion to swap around the keywords,
2765                        /// as we already generated a suggestion to remove the keyword earlier.
2766                        MisplacedDisallowedQualifier,
2767                    }
2768
2769                    // We may be able to recover
2770                    let mut recover_constness = constness;
2771                    let mut recover_coroutine_kind = coroutine_kind;
2772                    let mut recover_safety = safety;
2773                    // This will allow the machine fix to directly place the keyword in the correct place or to indicate
2774                    // that the keyword is already present and the second instance should be removed.
2775                    let wrong_kw = if self.check_keyword(exp!(Const)) {
2776                        match constness {
2777                            Const::Yes(sp) => Some(WrongKw::Duplicated(sp)),
2778                            Const::No => {
2779                                recover_constness = Const::Yes(self.token.span);
2780                                match parsing_mode {
2781                                    FrontMatterParsingMode::Function => {
2782                                        Some(WrongKw::Misplaced(async_start_sp))
2783                                    }
2784                                    FrontMatterParsingMode::FunctionPtrType => {
2785                                        self.dcx().emit_err(FnPointerCannotBeConst {
2786                                            span: self.token.span,
2787                                            suggestion: self
2788                                                .token
2789                                                .span
2790                                                .with_lo(self.prev_token.span.hi()),
2791                                        });
2792                                        Some(WrongKw::MisplacedDisallowedQualifier)
2793                                    }
2794                                }
2795                            }
2796                        }
2797                    } else if self.check_keyword(exp!(Async)) {
2798                        match coroutine_kind {
2799                            Some(CoroutineKind::Async { span, .. }) => {
2800                                Some(WrongKw::Duplicated(span))
2801                            }
2802                            Some(CoroutineKind::AsyncGen { span, .. }) => {
2803                                Some(WrongKw::Duplicated(span))
2804                            }
2805                            Some(CoroutineKind::Gen { .. }) => {
2806                                recover_coroutine_kind = Some(CoroutineKind::AsyncGen {
2807                                    span: self.token.span,
2808                                    closure_id: DUMMY_NODE_ID,
2809                                    return_impl_trait_id: DUMMY_NODE_ID,
2810                                });
2811                                // FIXME(gen_blocks): This span is wrong, didn't want to think about it.
2812                                Some(WrongKw::Misplaced(unsafe_start_sp))
2813                            }
2814                            None => {
2815                                recover_coroutine_kind = Some(CoroutineKind::Async {
2816                                    span: self.token.span,
2817                                    closure_id: DUMMY_NODE_ID,
2818                                    return_impl_trait_id: DUMMY_NODE_ID,
2819                                });
2820                                match parsing_mode {
2821                                    FrontMatterParsingMode::Function => {
2822                                        Some(WrongKw::Misplaced(async_start_sp))
2823                                    }
2824                                    FrontMatterParsingMode::FunctionPtrType => {
2825                                        self.dcx().emit_err(FnPointerCannotBeAsync {
2826                                            span: self.token.span,
2827                                            suggestion: self
2828                                                .token
2829                                                .span
2830                                                .with_lo(self.prev_token.span.hi()),
2831                                        });
2832                                        Some(WrongKw::MisplacedDisallowedQualifier)
2833                                    }
2834                                }
2835                            }
2836                        }
2837                    } else if self.check_keyword(exp!(Unsafe)) {
2838                        match safety {
2839                            Safety::Unsafe(sp) => Some(WrongKw::Duplicated(sp)),
2840                            Safety::Safe(sp) => {
2841                                recover_safety = Safety::Unsafe(self.token.span);
2842                                Some(WrongKw::Misplaced(sp))
2843                            }
2844                            Safety::Default => {
2845                                recover_safety = Safety::Unsafe(self.token.span);
2846                                Some(WrongKw::Misplaced(ext_start_sp))
2847                            }
2848                        }
2849                    } else if self.check_keyword(exp!(Safe)) {
2850                        match safety {
2851                            Safety::Safe(sp) => Some(WrongKw::Duplicated(sp)),
2852                            Safety::Unsafe(sp) => {
2853                                recover_safety = Safety::Safe(self.token.span);
2854                                Some(WrongKw::Misplaced(sp))
2855                            }
2856                            Safety::Default => {
2857                                recover_safety = Safety::Safe(self.token.span);
2858                                Some(WrongKw::Misplaced(ext_start_sp))
2859                            }
2860                        }
2861                    } else {
2862                        None
2863                    };
2864
2865                    // The keyword is already present, suggest removal of the second instance
2866                    if let Some(WrongKw::Duplicated(original_sp)) = wrong_kw {
2867                        let original_kw = self
2868                            .span_to_snippet(original_sp)
2869                            .expect("Span extracted directly from keyword should always work");
2870
2871                        err.span_suggestion(
2872                            self.token_uninterpolated_span(),
2873                            format!("`{original_kw}` already used earlier, remove this one"),
2874                            "",
2875                            Applicability::MachineApplicable,
2876                        )
2877                        .span_note(original_sp, format!("`{original_kw}` first seen here"));
2878                    }
2879                    // The keyword has not been seen yet, suggest correct placement in the function front matter
2880                    else if let Some(WrongKw::Misplaced(correct_pos_sp)) = wrong_kw {
2881                        let correct_pos_sp = correct_pos_sp.to(self.prev_token.span);
2882                        if let Ok(current_qual) = self.span_to_snippet(correct_pos_sp) {
2883                            let misplaced_qual_sp = self.token_uninterpolated_span();
2884                            let misplaced_qual = self.span_to_snippet(misplaced_qual_sp).unwrap();
2885
2886                            err.span_suggestion(
2887                                    correct_pos_sp.to(misplaced_qual_sp),
2888                                    format!("`{misplaced_qual}` must come before `{current_qual}`"),
2889                                    format!("{misplaced_qual} {current_qual}"),
2890                                    Applicability::MachineApplicable,
2891                                ).note("keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern`");
2892                        }
2893                    }
2894                    // Recover incorrect visibility order such as `async pub`
2895                    else if self.check_keyword(exp!(Pub)) {
2896                        let sp = sp_start.to(self.prev_token.span);
2897                        if let Ok(snippet) = self.span_to_snippet(sp) {
2898                            let current_vis = match self.parse_visibility(FollowedByType::No) {
2899                                Ok(v) => v,
2900                                Err(d) => {
2901                                    d.cancel();
2902                                    return Err(err);
2903                                }
2904                            };
2905                            let vs = pprust::vis_to_string(&current_vis);
2906                            let vs = vs.trim_end();
2907
2908                            // There was no explicit visibility
2909                            if matches!(orig_vis.kind, VisibilityKind::Inherited) {
2910                                err.span_suggestion(
2911                                    sp_start.to(self.prev_token.span),
2912                                    format!("visibility `{vs}` must come before `{snippet}`"),
2913                                    format!("{vs} {snippet}"),
2914                                    Applicability::MachineApplicable,
2915                                );
2916                            }
2917                            // There was an explicit visibility
2918                            else {
2919                                err.span_suggestion(
2920                                    current_vis.span,
2921                                    "there is already a visibility modifier, remove one",
2922                                    "",
2923                                    Applicability::MachineApplicable,
2924                                )
2925                                .span_note(orig_vis.span, "explicit visibility first seen here");
2926                            }
2927                        }
2928                    }
2929
2930                    // FIXME(gen_blocks): add keyword recovery logic for genness
2931
2932                    if let Some(wrong_kw) = wrong_kw
2933                        && self.may_recover()
2934                        && self.look_ahead(1, |tok| tok.is_keyword_case(kw::Fn, case))
2935                    {
2936                        // Advance past the misplaced keyword and `fn`
2937                        self.bump();
2938                        self.bump();
2939                        // When we recover from a `MisplacedDisallowedQualifier`, we already emitted an error for the disallowed qualifier
2940                        // So we don't emit another error that the qualifier is unexpected.
2941                        if matches!(wrong_kw, WrongKw::MisplacedDisallowedQualifier) {
2942                            err.cancel();
2943                        } else {
2944                            err.emit();
2945                        }
2946                        return Ok(FnHeader {
2947                            constness: recover_constness,
2948                            safety: recover_safety,
2949                            coroutine_kind: recover_coroutine_kind,
2950                            ext,
2951                        });
2952                    }
2953
2954                    return Err(err);
2955                }
2956            }
2957        }
2958
2959        Ok(FnHeader { constness, safety, coroutine_kind, ext })
2960    }
2961
2962    /// Parses the parameter list and result type of a function declaration.
2963    pub(super) fn parse_fn_decl(
2964        &mut self,
2965        fn_parse_mode: &FnParseMode,
2966        ret_allow_plus: AllowPlus,
2967        recover_return_sign: RecoverReturnSign,
2968    ) -> PResult<'a, Box<FnDecl>> {
2969        Ok(Box::new(FnDecl {
2970            inputs: self.parse_fn_params(fn_parse_mode)?,
2971            output: self.parse_ret_ty(ret_allow_plus, RecoverQPath::Yes, recover_return_sign)?,
2972        }))
2973    }
2974
2975    /// Parses the parameter list of a function, including the `(` and `)` delimiters.
2976    pub(super) fn parse_fn_params(
2977        &mut self,
2978        fn_parse_mode: &FnParseMode,
2979    ) -> PResult<'a, ThinVec<Param>> {
2980        let mut first_param = true;
2981        // Parse the arguments, starting out with `self` being allowed...
2982        if self.token != TokenKind::OpenParen
2983        // might be typo'd trait impl, handled elsewhere
2984        && !self.token.is_keyword(kw::For)
2985        {
2986            // recover from missing argument list, e.g. `fn main -> () {}`
2987            self.dcx()
2988                .emit_err(errors::MissingFnParams { span: self.prev_token.span.shrink_to_hi() });
2989            return Ok(ThinVec::new());
2990        }
2991
2992        let (mut params, _) = self.parse_paren_comma_seq(|p| {
2993            p.recover_vcs_conflict_marker();
2994            let snapshot = p.create_snapshot_for_diagnostic();
2995            let param = p.parse_param_general(fn_parse_mode, first_param, true).or_else(|e| {
2996                let guar = e.emit();
2997                // When parsing a param failed, we should check to make the span of the param
2998                // not contain '(' before it.
2999                // For example when parsing `*mut Self` in function `fn oof(*mut Self)`.
3000                let lo = if let TokenKind::OpenParen = p.prev_token.kind {
3001                    p.prev_token.span.shrink_to_hi()
3002                } else {
3003                    p.prev_token.span
3004                };
3005                p.restore_snapshot(snapshot);
3006                // Skip every token until next possible arg or end.
3007                p.eat_to_tokens(&[exp!(Comma), exp!(CloseParen)]);
3008                // Create a placeholder argument for proper arg count (issue #34264).
3009                Ok(dummy_arg(Ident::new(sym::dummy, lo.to(p.prev_token.span)), guar))
3010            });
3011            // ...now that we've parsed the first argument, `self` is no longer allowed.
3012            first_param = false;
3013            param
3014        })?;
3015        // Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
3016        self.deduplicate_recovered_params_names(&mut params);
3017        Ok(params)
3018    }
3019
3020    /// Parses a single function parameter.
3021    ///
3022    /// - `self` is syntactically allowed when `first_param` holds.
3023    /// - `recover_arg_parse` is used to recover from a failed argument parse.
3024    pub(super) fn parse_param_general(
3025        &mut self,
3026        fn_parse_mode: &FnParseMode,
3027        first_param: bool,
3028        recover_arg_parse: bool,
3029    ) -> PResult<'a, Param> {
3030        let lo = self.token.span;
3031        let attrs = self.parse_outer_attributes()?;
3032        self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
3033            // Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
3034            if let Some(mut param) = this.parse_self_param()? {
3035                param.attrs = attrs;
3036                let res = if first_param { Ok(param) } else { this.recover_bad_self_param(param) };
3037                return Ok((res?, Trailing::No, UsePreAttrPos::No));
3038            }
3039
3040            let is_name_required = match this.token.kind {
3041                token::DotDotDot => false,
3042                _ => (fn_parse_mode.req_name)(
3043                    this.token.span.with_neighbor(this.prev_token.span).edition(),
3044                ),
3045            };
3046            let (pat, ty) = if is_name_required || this.is_named_param() {
3047                debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);
3048                let (pat, colon) = this.parse_fn_param_pat_colon()?;
3049                if !colon {
3050                    let mut err = this.unexpected().unwrap_err();
3051                    return if let Some(ident) = this.parameter_without_type(
3052                        &mut err,
3053                        pat,
3054                        is_name_required,
3055                        first_param,
3056                        fn_parse_mode,
3057                    ) {
3058                        let guar = err.emit();
3059                        Ok((dummy_arg(ident, guar), Trailing::No, UsePreAttrPos::No))
3060                    } else {
3061                        Err(err)
3062                    };
3063                }
3064
3065                this.eat_incorrect_doc_comment_for_param_type();
3066                (pat, this.parse_ty_for_param()?)
3067            } else {
3068                debug!("parse_param_general ident_to_pat");
3069                let parser_snapshot_before_ty = this.create_snapshot_for_diagnostic();
3070                this.eat_incorrect_doc_comment_for_param_type();
3071                let mut ty = this.parse_ty_for_param();
3072
3073                if let Ok(t) = &ty {
3074                    // Check for trailing angle brackets
3075                    if let TyKind::Path(_, Path { segments, .. }) = &t.kind
3076                        && let Some(segment) = segments.last()
3077                        && let Some(guar) =
3078                            this.check_trailing_angle_brackets(segment, &[exp!(CloseParen)])
3079                    {
3080                        return Ok((
3081                            dummy_arg(segment.ident, guar),
3082                            Trailing::No,
3083                            UsePreAttrPos::No,
3084                        ));
3085                    }
3086
3087                    if this.token != token::Comma && this.token != token::CloseParen {
3088                        // This wasn't actually a type, but a pattern looking like a type,
3089                        // so we are going to rollback and re-parse for recovery.
3090                        ty = this.unexpected_any();
3091                    }
3092                }
3093                match ty {
3094                    Ok(ty) => {
3095                        let pat = this.mk_pat(ty.span, PatKind::Missing);
3096                        (Box::new(pat), ty)
3097                    }
3098                    // If this is a C-variadic argument and we hit an error, return the error.
3099                    Err(err) if this.token == token::DotDotDot => return Err(err),
3100                    Err(err) if this.unmatched_angle_bracket_count > 0 => return Err(err),
3101                    Err(err) if recover_arg_parse => {
3102                        // Recover from attempting to parse the argument as a type without pattern.
3103                        err.cancel();
3104                        this.restore_snapshot(parser_snapshot_before_ty);
3105                        this.recover_arg_parse()?
3106                    }
3107                    Err(err) => return Err(err),
3108                }
3109            };
3110
3111            let span = lo.to(this.prev_token.span);
3112
3113            Ok((
3114                Param { attrs, id: ast::DUMMY_NODE_ID, is_placeholder: false, pat, span, ty },
3115                Trailing::No,
3116                UsePreAttrPos::No,
3117            ))
3118        })
3119    }
3120
3121    /// Returns the parsed optional self parameter and whether a self shortcut was used.
3122    fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
3123        // Extract an identifier *after* having confirmed that the token is one.
3124        let expect_self_ident = |this: &mut Self| match this.token.ident() {
3125            Some((ident, IdentIsRaw::No)) => {
3126                this.bump();
3127                ident
3128            }
3129            _ => unreachable!(),
3130        };
3131        // is lifetime `n` tokens ahead?
3132        let is_lifetime = |this: &Self, n| this.look_ahead(n, |t| t.is_lifetime());
3133        // Is `self` `n` tokens ahead?
3134        let is_isolated_self = |this: &Self, n| {
3135            this.is_keyword_ahead(n, &[kw::SelfLower])
3136                && this.look_ahead(n + 1, |t| t != &token::PathSep)
3137        };
3138        // Is `pin const self` `n` tokens ahead?
3139        let is_isolated_pin_const_self = |this: &Self, n| {
3140            this.look_ahead(n, |token| token.is_ident_named(sym::pin))
3141                && this.is_keyword_ahead(n + 1, &[kw::Const])
3142                && is_isolated_self(this, n + 2)
3143        };
3144        // Is `mut self` `n` tokens ahead?
3145        let is_isolated_mut_self =
3146            |this: &Self, n| this.is_keyword_ahead(n, &[kw::Mut]) && is_isolated_self(this, n + 1);
3147        // Is `pin mut self` `n` tokens ahead?
3148        let is_isolated_pin_mut_self = |this: &Self, n| {
3149            this.look_ahead(n, |token| token.is_ident_named(sym::pin))
3150                && is_isolated_mut_self(this, n + 1)
3151        };
3152        // Parse `self` or `self: TYPE`. We already know the current token is `self`.
3153        let parse_self_possibly_typed = |this: &mut Self, m| {
3154            let eself_ident = expect_self_ident(this);
3155            let eself_hi = this.prev_token.span;
3156            let eself = if this.eat(exp!(Colon)) {
3157                SelfKind::Explicit(this.parse_ty()?, m)
3158            } else {
3159                SelfKind::Value(m)
3160            };
3161            Ok((eself, eself_ident, eself_hi))
3162        };
3163        let expect_self_ident_not_typed =
3164            |this: &mut Self, modifier: &SelfKind, modifier_span: Span| {
3165                let eself_ident = expect_self_ident(this);
3166
3167                // Recover `: Type` after a qualified self
3168                if this.may_recover() && this.eat_noexpect(&token::Colon) {
3169                    let snap = this.create_snapshot_for_diagnostic();
3170                    match this.parse_ty() {
3171                        Ok(ty) => {
3172                            this.dcx().emit_err(errors::IncorrectTypeOnSelf {
3173                                span: ty.span,
3174                                move_self_modifier: errors::MoveSelfModifier {
3175                                    removal_span: modifier_span,
3176                                    insertion_span: ty.span.shrink_to_lo(),
3177                                    modifier: modifier.to_ref_suggestion(),
3178                                },
3179                            });
3180                        }
3181                        Err(diag) => {
3182                            diag.cancel();
3183                            this.restore_snapshot(snap);
3184                        }
3185                    }
3186                }
3187                eself_ident
3188            };
3189        // Recover for the grammar `*self`, `*const self`, and `*mut self`.
3190        let recover_self_ptr = |this: &mut Self| {
3191            this.dcx().emit_err(errors::SelfArgumentPointer { span: this.token.span });
3192
3193            Ok((SelfKind::Value(Mutability::Not), expect_self_ident(this), this.prev_token.span))
3194        };
3195
3196        // Parse optional `self` parameter of a method.
3197        // Only a limited set of initial token sequences is considered `self` parameters; anything
3198        // else is parsed as a normal function parameter list, so some lookahead is required.
3199        let eself_lo = self.token.span;
3200        let (eself, eself_ident, eself_hi) = match self.token.uninterpolate().kind {
3201            token::And => {
3202                let has_lifetime = is_lifetime(self, 1);
3203                let skip_lifetime_count = has_lifetime as usize;
3204                let eself = if is_isolated_self(self, skip_lifetime_count + 1) {
3205                    // `&{'lt} self`
3206                    self.bump(); // &
3207                    let lifetime = has_lifetime.then(|| self.expect_lifetime());
3208                    SelfKind::Region(lifetime, Mutability::Not)
3209                } else if is_isolated_mut_self(self, skip_lifetime_count + 1) {
3210                    // `&{'lt} mut self`
3211                    self.bump(); // &
3212                    let lifetime = has_lifetime.then(|| self.expect_lifetime());
3213                    self.bump(); // mut
3214                    SelfKind::Region(lifetime, Mutability::Mut)
3215                } else if is_isolated_pin_const_self(self, skip_lifetime_count + 1) {
3216                    // `&{'lt} pin const self`
3217                    self.bump(); // &
3218                    let lifetime = has_lifetime.then(|| self.expect_lifetime());
3219                    self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
3220                    self.bump(); // pin
3221                    self.bump(); // const
3222                    SelfKind::Pinned(lifetime, Mutability::Not)
3223                } else if is_isolated_pin_mut_self(self, skip_lifetime_count + 1) {
3224                    // `&{'lt} pin mut self`
3225                    self.bump(); // &
3226                    let lifetime = has_lifetime.then(|| self.expect_lifetime());
3227                    self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
3228                    self.bump(); // pin
3229                    self.bump(); // mut
3230                    SelfKind::Pinned(lifetime, Mutability::Mut)
3231                } else {
3232                    // `&not_self`
3233                    return Ok(None);
3234                };
3235                let hi = self.token.span;
3236                let self_ident = expect_self_ident_not_typed(self, &eself, eself_lo.until(hi));
3237                (eself, self_ident, hi)
3238            }
3239            // `*self`
3240            token::Star if is_isolated_self(self, 1) => {
3241                self.bump();
3242                recover_self_ptr(self)?
3243            }
3244            // `*mut self` and `*const self`
3245            token::Star
3246                if self.look_ahead(1, |t| t.is_mutability()) && is_isolated_self(self, 2) =>
3247            {
3248                self.bump();
3249                self.bump();
3250                recover_self_ptr(self)?
3251            }
3252            // `self` and `self: TYPE`
3253            token::Ident(..) if is_isolated_self(self, 0) => {
3254                parse_self_possibly_typed(self, Mutability::Not)?
3255            }
3256            // `mut self` and `mut self: TYPE`
3257            token::Ident(..) if is_isolated_mut_self(self, 0) => {
3258                self.bump();
3259                parse_self_possibly_typed(self, Mutability::Mut)?
3260            }
3261            _ => return Ok(None),
3262        };
3263
3264        let eself = source_map::respan(eself_lo.to(eself_hi), eself);
3265        Ok(Some(Param::from_self(AttrVec::default(), eself, eself_ident)))
3266    }
3267
3268    fn is_named_param(&self) -> bool {
3269        let offset = match &self.token.kind {
3270            token::OpenInvisible(origin) => match origin {
3271                InvisibleOrigin::MetaVar(MetaVarKind::Pat(_)) => {
3272                    return self.check_noexpect_past_close_delim(&token::Colon);
3273                }
3274                _ => 0,
3275            },
3276            token::And | token::AndAnd => 1,
3277            _ if self.token.is_keyword(kw::Mut) => 1,
3278            _ => 0,
3279        };
3280
3281        self.look_ahead(offset, |t| t.is_ident())
3282            && self.look_ahead(offset + 1, |t| t == &token::Colon)
3283    }
3284
3285    fn recover_self_param(&mut self) -> bool {
3286        matches!(
3287            self.parse_outer_attributes()
3288                .and_then(|_| self.parse_self_param())
3289                .map_err(|e| e.cancel()),
3290            Ok(Some(_))
3291        )
3292    }
3293}
3294
3295enum IsMacroRulesItem {
3296    Yes { has_bang: bool },
3297    No,
3298}
3299
3300#[derive(Copy, Clone, PartialEq, Eq)]
3301pub(super) enum FrontMatterParsingMode {
3302    /// Parse the front matter of a function declaration
3303    Function,
3304    /// Parse the front matter of a function pointet type.
3305    /// For function pointer types, the `const` and `async` keywords are not permitted.
3306    FunctionPtrType,
3307}