Rollup merge of #127835 - estebank:issue-127823, r=compiler-errors

Fix ICE in suggestion caused by `⩵` being recovered as `==`

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;
   |                ~
```

Fix #127823.
This commit is contained in:
Matthias Krüger 2024-07-18 23:05:21 +02:00 committed by GitHub
commit 50a90e394e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 33 additions and 12 deletions

View File

@ -660,9 +660,8 @@ pub(crate) struct RemoveLet {
#[diag(parse_use_eq_instead)] #[diag(parse_use_eq_instead)]
pub(crate) struct UseEqInstead { pub(crate) struct UseEqInstead {
#[primary_span] #[primary_span]
#[suggestion(style = "verbose", applicability = "machine-applicable", code = "=")]
pub span: Span, pub span: Span,
#[suggestion(style = "verbose", applicability = "machine-applicable", code = "")]
pub suggestion: Span,
} }
#[derive(Diagnostic)] #[derive(Diagnostic)]

View File

@ -566,10 +566,7 @@ impl<'a> Parser<'a> {
&& expected.iter().any(|tok| matches!(tok, TokenType::Token(TokenKind::Eq))) && expected.iter().any(|tok| matches!(tok, TokenType::Token(TokenKind::Eq)))
{ {
// Likely typo: `=` → `==` in let expr or enum item // Likely typo: `=` → `==` in let expr or enum item
return Err(self.dcx().create_err(UseEqInstead { return Err(self.dcx().create_err(UseEqInstead { span: self.token.span }));
span: self.token.span,
suggestion: self.token.span.with_lo(self.token.span.lo() + BytePos(1)),
}));
} }
if self.token.is_keyword(kw::Move) && self.prev_token.is_keyword(kw::Async) { if self.token.is_keyword(kw::Move) && self.prev_token.is_keyword(kw::Async) {

View File

@ -7,9 +7,8 @@ LL | B == 2
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }` = help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
help: try using `=` instead help: try using `=` instead
| |
LL - B == 2 LL | B = 2
LL + B = 2 | ~
|
error: expected item, found `==` error: expected item, found `==`
--> $DIR/issue-101477-enum.rs:6:7 --> $DIR/issue-101477-enum.rs:6:7

View File

@ -6,9 +6,8 @@ LL | let x == 2;
| |
help: try using `=` instead help: try using `=` instead
| |
LL - let x == 2; LL | let x = 2;
LL + let x = 2; | ~
|
error: aborting due to 1 previous error error: aborting due to 1 previous error

View File

@ -0,0 +1,3 @@
const A: usize 2;
//~^ ERROR unknown start of token: \u{2a75}
//~| ERROR unexpected `==`

View File

@ -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