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