From 745c1ea4388864d6a832aeacb9fc3b9db1ab9c2f Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Tue, 3 Oct 2023 21:21:02 +0000 Subject: Detect missing `=>` after match guard during parsing ``` error: expected one of `,`, `:`, or `}`, found `.` --> $DIR/missing-fat-arrow.rs:25:14 | LL | Some(a) if a.value == b { | - while parsing this struct LL | a.value = 1; | -^ expected one of `,`, `:`, or `}` | | | while parsing this struct field | help: try naming a field | LL | a: a.value = 1; | ++ help: you might have meant to start a match arm after the match guard | LL | Some(a) if a.value == b => { | ++ ``` Fix #78585. --- compiler/rustc_parse/src/parser/expr.rs | 93 ++++++++++++++++++++++++++------- compiler/rustc_parse/src/parser/mod.rs | 1 + 2 files changed, 75 insertions(+), 19 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index f4cee3a661e..2f8e3bb6497 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -2834,7 +2834,7 @@ impl<'a> Parser<'a> { )?; let guard = if this.eat_keyword(kw::If) { let if_span = this.prev_token.span; - let mut cond = this.parse_expr_res(Restrictions::ALLOW_LET, None)?; + let mut cond = this.parse_match_guard_condition()?; CondChecker { parser: this, forbid_let_reason: None }.visit_expr(&mut cond); @@ -2860,9 +2860,9 @@ impl<'a> Parser<'a> { { err.span_suggestion( this.token.span, - "try using a fat arrow here", + "use a fat arrow to start a match arm", "=>", - Applicability::MaybeIncorrect, + Applicability::MachineApplicable, ); err.emit(); this.bump(); @@ -2979,6 +2979,34 @@ impl<'a> Parser<'a> { }) } + fn parse_match_guard_condition(&mut self) -> PResult<'a, P> { + self.parse_expr_res(Restrictions::ALLOW_LET | Restrictions::IN_IF_GUARD, None).map_err( + |mut err| { + if self.prev_token == token::OpenDelim(Delimiter::Brace) { + let sugg_sp = self.prev_token.span.shrink_to_lo(); + // Consume everything within the braces, let's avoid further parse + // errors. + self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore); + let msg = "you might have meant to start a match arm after the match guard"; + if self.eat(&token::CloseDelim(Delimiter::Brace)) { + let applicability = if self.token.kind != token::FatArrow { + // We have high confidence that we indeed didn't have a struct + // literal in the match guard, but rather we had some operation + // that ended in a path, immediately followed by a block that was + // meant to be the match arm. + Applicability::MachineApplicable + } else { + Applicability::MaybeIncorrect + }; + // self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore); + err.span_suggestion_verbose(sugg_sp, msg, "=> ".to_string(), applicability); + } + } + err + }, + ) + } + pub(crate) fn is_builtin(&self) -> bool { self.token.is_keyword(kw::Builtin) && self.look_ahead(1, |t| *t == token::Pound) } @@ -3049,9 +3077,10 @@ impl<'a> Parser<'a> { || self.look_ahead(2, |t| t == &token::Colon) && ( // `{ ident: token, ` cannot start a block. - self.look_ahead(4, |t| t == &token::Comma) || - // `{ ident: ` cannot start a block unless it's a type ascription `ident: Type`. - self.look_ahead(3, |t| !t.can_begin_type()) + self.look_ahead(4, |t| t == &token::Comma) + // `{ ident: ` cannot start a block unless it's a type ascription + // `ident: Type`. + || self.look_ahead(3, |t| !t.can_begin_type()) ) ) } @@ -3091,6 +3120,7 @@ impl<'a> Parser<'a> { let mut fields = ThinVec::new(); let mut base = ast::StructRest::None; let mut recover_async = false; + let in_if_guard = self.restrictions.contains(Restrictions::IN_IF_GUARD); let mut async_block_err = |e: &mut Diagnostic, span: Span| { recover_async = true; @@ -3128,6 +3158,26 @@ impl<'a> Parser<'a> { e.span_label(pth.span, "while parsing this struct"); } + if let Some((ident, _)) = self.token.ident() + && !self.token.is_reserved_ident() + && self.look_ahead(1, |t| { + AssocOp::from_token(&t).is_some() + || matches!(t.kind, token::OpenDelim(_)) + || t.kind == token::Dot + }) + { + // Looks like they tried to write a shorthand, complex expression. + e.span_suggestion_verbose( + self.token.span.shrink_to_lo(), + "try naming a field", + &format!("{ident}: ", ), + Applicability::HasPlaceholders, + ); + } + if in_if_guard && close_delim == Delimiter::Brace { + return Err(e); + } + if !recover { return Err(e); } @@ -3173,19 +3223,6 @@ impl<'a> Parser<'a> { ",", Applicability::MachineApplicable, ); - } else if is_shorthand - && (AssocOp::from_token(&self.token).is_some() - || matches!(&self.token.kind, token::OpenDelim(_)) - || self.token.kind == token::Dot) - { - // Looks like they tried to write a shorthand, complex expression. - let ident = parsed_field.expect("is_shorthand implies Some").ident; - e.span_suggestion( - ident.span.shrink_to_lo(), - "try naming a field", - &format!("{ident}: "), - Applicability::HasPlaceholders, - ); } } if !recover { @@ -3288,6 +3325,24 @@ impl<'a> Parser<'a> { // Check if a colon exists one ahead. This means we're parsing a fieldname. let is_shorthand = !this.look_ahead(1, |t| t == &token::Colon || t == &token::Eq); + // Proactively check whether parsing the field will be correct. + let is_wrong = this.token.is_ident() + && !this.token.is_reserved_ident() + && !this.look_ahead(1, |t| { + t == &token::Colon + || t == &token::Eq + || t == &token::Comma + || t == &token::CloseDelim(Delimiter::Brace) + || t == &token::CloseDelim(Delimiter::Parenthesis) + }); + if is_wrong { + return Err(errors::ExpectedStructField { + span: this.look_ahead(1, |t| t.span), + ident_span: this.token.span, + token: this.look_ahead(1, |t| t.clone()), + } + .into_diagnostic(&self.sess.span_diagnostic)); + } let (ident, expr) = if is_shorthand { // Mimic `x: x` for the `x` field shorthand. let ident = this.parse_ident_common(false)?; diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index e84d8f5b358..6c24646f39a 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -52,6 +52,7 @@ bitflags::bitflags! { const NO_STRUCT_LITERAL = 1 << 1; const CONST_EXPR = 1 << 2; const ALLOW_LET = 1 << 3; + const IN_IF_GUARD = 1 << 4; } } -- cgit 1.4.1-3-g733a5 From 8fd345dd4b85b6758896995beb5b0417efd52364 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Wed, 4 Oct 2023 01:35:07 +0000 Subject: review comments --- compiler/rustc_parse/src/parser/expr.rs | 5 ++--- tests/ui/parser/missing-fat-arrow.rs | 5 +---- tests/ui/parser/missing-fat-arrow.stderr | 10 +++++----- 3 files changed, 8 insertions(+), 12 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 2f8e3bb6497..91bb2d9eb66 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -2998,7 +2998,6 @@ impl<'a> Parser<'a> { } else { Applicability::MaybeIncorrect }; - // self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore); err.span_suggestion_verbose(sugg_sp, msg, "=> ".to_string(), applicability); } } @@ -3171,7 +3170,7 @@ impl<'a> Parser<'a> { self.token.span.shrink_to_lo(), "try naming a field", &format!("{ident}: ", ), - Applicability::HasPlaceholders, + Applicability::MaybeIncorrect, ); } if in_if_guard && close_delim == Delimiter::Brace { @@ -3325,7 +3324,7 @@ impl<'a> Parser<'a> { // Check if a colon exists one ahead. This means we're parsing a fieldname. let is_shorthand = !this.look_ahead(1, |t| t == &token::Colon || t == &token::Eq); - // Proactively check whether parsing the field will be correct. + // Proactively check whether parsing the field will be incorrect. let is_wrong = this.token.is_ident() && !this.token.is_reserved_ident() && !this.look_ahead(1, |t| { diff --git a/tests/ui/parser/missing-fat-arrow.rs b/tests/ui/parser/missing-fat-arrow.rs index fef16129cd0..325f1ccf2fd 100644 --- a/tests/ui/parser/missing-fat-arrow.rs +++ b/tests/ui/parser/missing-fat-arrow.rs @@ -6,11 +6,8 @@ fn main() { match value { Some(x) if x == y { self.next_token()?; //~ ERROR expected identifier, found keyword `self` - Ok(true) }, - _ => { - Ok(false) - } + _ => {} } let _: i32 = (); //~ ERROR mismatched types } diff --git a/tests/ui/parser/missing-fat-arrow.stderr b/tests/ui/parser/missing-fat-arrow.stderr index abfed7a5683..a6c786905e9 100644 --- a/tests/ui/parser/missing-fat-arrow.stderr +++ b/tests/ui/parser/missing-fat-arrow.stderr @@ -12,7 +12,7 @@ LL | Some(x) if x == y => { | ++ error: expected one of `,`, `:`, or `}`, found `.` - --> $DIR/missing-fat-arrow.rs:25:14 + --> $DIR/missing-fat-arrow.rs:22:14 | LL | Some(a) if a.value == b { | - while parsing this struct @@ -31,7 +31,7 @@ LL | Some(a) if a.value == b => { | ++ error: expected one of `,`, `:`, or `}`, found `.` - --> $DIR/missing-fat-arrow.rs:35:14 + --> $DIR/missing-fat-arrow.rs:32:14 | LL | Some(a) if a.value == b { | - while parsing this struct @@ -50,7 +50,7 @@ LL | Some(a) if a.value == b => { | ++ error[E0308]: mismatched types - --> $DIR/missing-fat-arrow.rs:15:18 + --> $DIR/missing-fat-arrow.rs:12:18 | LL | let _: i32 = (); | --- ^^ expected `i32`, found `()` @@ -58,7 +58,7 @@ LL | let _: i32 = (); | expected due to this error[E0308]: mismatched types - --> $DIR/missing-fat-arrow.rs:29:18 + --> $DIR/missing-fat-arrow.rs:26:18 | LL | let _: i32 = (); | --- ^^ expected `i32`, found `()` @@ -66,7 +66,7 @@ LL | let _: i32 = (); | expected due to this error[E0308]: mismatched types - --> $DIR/missing-fat-arrow.rs:40:18 + --> $DIR/missing-fat-arrow.rs:37:18 | LL | let _: i32 = (); | --- ^^ expected `i32`, found `()` -- cgit 1.4.1-3-g733a5