Skip to content

Commit

Permalink
Fix ICE in suggestion caused by being recovered as ==
Browse files Browse the repository at this point in the history
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;
   |                ~
```
  • Loading branch information
estebank committed Jul 16, 2024
1 parent 16b5690 commit b6e4b0f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
3 changes: 1 addition & 2 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/parser/recover/unicode-double-equals-recovery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const A: usize2;

Check failure on line 1 in tests/ui/parser/recover/unicode-double-equals-recovery.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

trailing whitespace
//~^ ERROR unknown start of token: \u{2a75}
//~| ERROR unexpected `==`
24 changes: 24 additions & 0 deletions tests/ui/parser/recover/unicode-double-equals-recovery.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b6e4b0f

Please sign in to comment.