From 67ec1326ee4e360b92dc6a6a3834bf8948532770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 16 Jul 2024 22:43:30 +0000 Subject: [PATCH] =?UTF-8?q?Fix=20ICE=20in=20suggestion=20caused=20by=20`?= =?UTF-8?q?=E2=A9=B5`=20being=20recovered=20as=20`=3D=3D`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The second suggestion shown here would previously incorrectly assume that the span corresponding to `⩵` was 2 bytes wide composed by 2 1 byte wide chars, so a span pointing at `==` could point only at one of the `=` to remove it. Instead, we now replace the whole thing (as we should have the whole time): ``` error: unknown start of token: \u{2a75} --> $DIR/unicode-double-equals-recovery.rs:1:16 | LL | const A: usize ⩵ 2; | ^ | help: Unicode character '⩵' (Two Consecutive Equals Signs) looks like '==' (Double Equals Sign), but it is not | LL | const A: usize == 2; | ~~ error: unexpected `==` --> $DIR/unicode-double-equals-recovery.rs:1:16 | LL | const A: usize ⩵ 2; | ^ | help: try using `=` instead | LL | const A: usize = 2; | ~ ``` --- compiler/rustc_parse/src/errors.rs | 3 +-- .../rustc_parse/src/parser/diagnostics.rs | 5 +--- .../ui/parser/issues/issue-101477-enum.stderr | 5 ++-- .../ui/parser/issues/issue-101477-let.stderr | 5 ++-- .../recover/unicode-double-equals-recovery.rs | 3 +++ .../unicode-double-equals-recovery.stderr | 24 +++++++++++++++++++ 6 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 tests/ui/parser/recover/unicode-double-equals-recovery.rs create mode 100644 tests/ui/parser/recover/unicode-double-equals-recovery.stderr diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 4222486034b..da0701efddb 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -660,9 +660,8 @@ pub(crate) struct RemoveLet { #[diag(parse_use_eq_instead)] pub(crate) struct UseEqInstead { #[primary_span] + #[suggestion(style = "verbose", applicability = "machine-applicable", code = "=")] pub span: Span, - #[suggestion(style = "verbose", applicability = "machine-applicable", code = "")] - pub suggestion: Span, } #[derive(Diagnostic)] diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 0da7fefe6ed..f24478581b8 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -566,10 +566,7 @@ impl<'a> Parser<'a> { && expected.iter().any(|tok| matches!(tok, TokenType::Token(TokenKind::Eq))) { // Likely typo: `=` → `==` in let expr or enum item - return Err(self.dcx().create_err(UseEqInstead { - span: self.token.span, - suggestion: self.token.span.with_lo(self.token.span.lo() + BytePos(1)), - })); + return Err(self.dcx().create_err(UseEqInstead { span: self.token.span })); } if self.token.is_keyword(kw::Move) && self.prev_token.is_keyword(kw::Async) { diff --git a/tests/ui/parser/issues/issue-101477-enum.stderr b/tests/ui/parser/issues/issue-101477-enum.stderr index c6dadeab8b3..8d4efdd17f7 100644 --- a/tests/ui/parser/issues/issue-101477-enum.stderr +++ b/tests/ui/parser/issues/issue-101477-enum.stderr @@ -7,9 +7,8 @@ LL | B == 2 = help: enum variants can be `Variant`, `Variant = `, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }` help: try using `=` instead | -LL - B == 2 -LL + B = 2 - | +LL | B = 2 + | ~ error: expected item, found `==` --> $DIR/issue-101477-enum.rs:6:7 diff --git a/tests/ui/parser/issues/issue-101477-let.stderr b/tests/ui/parser/issues/issue-101477-let.stderr index 59e90c8102f..d2671abbdea 100644 --- a/tests/ui/parser/issues/issue-101477-let.stderr +++ b/tests/ui/parser/issues/issue-101477-let.stderr @@ -6,9 +6,8 @@ LL | let x == 2; | help: try using `=` instead | -LL - let x == 2; -LL + let x = 2; - | +LL | let x = 2; + | ~ error: aborting due to 1 previous error diff --git a/tests/ui/parser/recover/unicode-double-equals-recovery.rs b/tests/ui/parser/recover/unicode-double-equals-recovery.rs new file mode 100644 index 00000000000..589f0a559bb --- /dev/null +++ b/tests/ui/parser/recover/unicode-double-equals-recovery.rs @@ -0,0 +1,3 @@ +const A: usize ⩵ 2; +//~^ ERROR unknown start of token: \u{2a75} +//~| ERROR unexpected `==` diff --git a/tests/ui/parser/recover/unicode-double-equals-recovery.stderr b/tests/ui/parser/recover/unicode-double-equals-recovery.stderr new file mode 100644 index 00000000000..6e10dcce04a --- /dev/null +++ b/tests/ui/parser/recover/unicode-double-equals-recovery.stderr @@ -0,0 +1,24 @@ +error: unknown start of token: \u{2a75} + --> $DIR/unicode-double-equals-recovery.rs:1:16 + | +LL | const A: usize ⩵ 2; + | ^ + | +help: Unicode character '⩵' (Two Consecutive Equals Signs) looks like '==' (Double Equals Sign), but it is not + | +LL | const A: usize == 2; + | ~~ + +error: unexpected `==` + --> $DIR/unicode-double-equals-recovery.rs:1:16 + | +LL | const A: usize ⩵ 2; + | ^ + | +help: try using `=` instead + | +LL | const A: usize = 2; + | ~ + +error: aborting due to 2 previous errors +