From ca1bcb64667410c7f59ec5745e012601eea3d65f Mon Sep 17 00:00:00 2001 From: clubby789 Date: Fri, 27 Oct 2023 20:12:45 +0000 Subject: Recover from missing param list in function definitions --- compiler/rustc_parse/src/errors.rs | 8 ++++++++ compiler/rustc_parse/src/parser/item.rs | 10 ++++++++++ 2 files changed, 18 insertions(+) (limited to 'compiler/rustc_parse/src') diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index aeb4fd0a304..f22d70ec809 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -1543,6 +1543,14 @@ pub(crate) enum AmbiguousMissingKwForItemSub { HelpMacro, } +#[derive(Diagnostic)] +#[diag(parse_missing_fn_params)] +pub(crate) struct MissingFnParams { + #[primary_span] + #[suggestion(code = "()", applicability = "machine-applicable", style = "short")] + pub span: Span, +} + #[derive(Diagnostic)] #[diag(parse_missing_trait_in_trait_impl)] pub(crate) struct MissingTraitInTraitImpl { diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 982f601c0d5..d4f7083c397 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -2492,6 +2492,16 @@ impl<'a> Parser<'a> { pub(super) fn parse_fn_params(&mut self, req_name: ReqName) -> PResult<'a, ThinVec> { let mut first_param = true; // Parse the arguments, starting out with `self` being allowed... + if self.token.kind != TokenKind::OpenDelim(Delimiter::Parenthesis) + // might be typo'd trait impl, handled elsewhere + && !self.token.is_keyword(kw::For) + { + // recover from missing argument list, e.g. `fn main -> () {}` + self.sess + .emit_err(errors::MissingFnParams { span: self.prev_token.span.shrink_to_hi() }); + return Ok(ThinVec::new()); + } + let (mut params, _) = self.parse_paren_comma_seq(|p| { p.recover_diff_marker(); let param = p.parse_param_general(req_name, first_param).or_else(|mut e| { -- cgit 1.4.1-3-g733a5