From eab1f30c297027f2349ecd1322573ae3b5c9d668 Mon Sep 17 00:00:00 2001 From: Daniel Sedlak Date: Tue, 12 Mar 2024 19:23:53 +0100 Subject: [PATCH] Fix ICE in diagnostics for parenthesized type arguments --- compiler/rustc_parse/src/parser/path.rs | 52 +++++++++++-------- ...hesized-type-arguments-ice-issue-122345.rs | 7 +++ ...zed-type-arguments-ice-issue-122345.stderr | 16 ++++++ 3 files changed, 54 insertions(+), 21 deletions(-) create mode 100644 tests/ui/parser/diagnostics-parenthesized-type-arguments-ice-issue-122345.rs create mode 100644 tests/ui/parser/diagnostics-parenthesized-type-arguments-ice-issue-122345.stderr diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index 2edf2111de732..163d10d03f03c 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -449,9 +449,13 @@ impl<'a> Parser<'a> { prev_token_before_parsing: Token, error: &mut Diag<'_>, ) { - if ((style == PathStyle::Expr && self.parse_paren_comma_seq(|p| p.parse_expr()).is_ok()) - || (style == PathStyle::Pat - && self + match style { + PathStyle::Expr + if let Ok(_) = self + .parse_paren_comma_seq(|p| p.parse_expr()) + .map_err(|error| error.cancel()) => {} + PathStyle::Pat + if let Ok(_) = self .parse_paren_comma_seq(|p| { p.parse_pat_allow_top_alt( None, @@ -460,25 +464,31 @@ impl<'a> Parser<'a> { CommaRecoveryMode::LikelyTuple, ) }) - .is_ok())) - && !matches!(self.token.kind, token::ModSep | token::RArrow) - { - error.span_suggestion_verbose( - prev_token_before_parsing.span, - format!( - "consider removing the `::` here to {}", - match style { - PathStyle::Expr => "call the expression", - PathStyle::Pat => "turn this into a tuple struct pattern", - _ => { - return; - } - } - ), - "", - Applicability::MaybeIncorrect, - ); + .map_err(|error| error.cancel()) => {} + _ => { + return; + } } + + if let token::ModSep | token::RArrow = self.token.kind { + return; + } + + error.span_suggestion_verbose( + prev_token_before_parsing.span, + format!( + "consider removing the `::` here to {}", + match style { + PathStyle::Expr => "call the expression", + PathStyle::Pat => "turn this into a tuple struct pattern", + _ => { + return; + } + } + ), + "", + Applicability::MaybeIncorrect, + ); } /// Parses generic args (within a path segment) with recovery for extra leading angle brackets. diff --git a/tests/ui/parser/diagnostics-parenthesized-type-arguments-ice-issue-122345.rs b/tests/ui/parser/diagnostics-parenthesized-type-arguments-ice-issue-122345.rs new file mode 100644 index 0000000000000..47df107a26123 --- /dev/null +++ b/tests/ui/parser/diagnostics-parenthesized-type-arguments-ice-issue-122345.rs @@ -0,0 +1,7 @@ +fn main() { + unsafe { + dealloc(ptr2, Layout::(x: !)(1, 1)); //~ ERROR: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `:` + //~^ ERROR: expected one of `.`, `;`, `?`, `}`, or an operator, found `)` + //~| while parsing this parenthesized list of type arguments starting here + } +} diff --git a/tests/ui/parser/diagnostics-parenthesized-type-arguments-ice-issue-122345.stderr b/tests/ui/parser/diagnostics-parenthesized-type-arguments-ice-issue-122345.stderr new file mode 100644 index 0000000000000..8067c97ae4b5a --- /dev/null +++ b/tests/ui/parser/diagnostics-parenthesized-type-arguments-ice-issue-122345.stderr @@ -0,0 +1,16 @@ +error: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `:` + --> $DIR/diagnostics-parenthesized-type-arguments-ice-issue-122345.rs:3:33 + | +LL | dealloc(ptr2, Layout::(x: !)(1, 1)); + | --- ^ expected one of 7 possible tokens + | | + | while parsing this parenthesized list of type arguments starting here + +error: expected one of `.`, `;`, `?`, `}`, or an operator, found `)` + --> $DIR/diagnostics-parenthesized-type-arguments-ice-issue-122345.rs:3:43 + | +LL | dealloc(ptr2, Layout::(x: !)(1, 1)); + | ^ expected one of `.`, `;`, `?`, `}`, or an operator + +error: aborting due to 2 previous errors +