From 799d2917e7bb36c95224436a39cd2d468f93f45d Mon Sep 17 00:00:00 2001 From: yukang Date: Mon, 3 Jul 2023 23:54:49 +0800 Subject: [PATCH] Detect extra space in keyword for better hint --- .../rustc_parse/src/parser/diagnostics.rs | 16 ++++++++++++++++ tests/ui/parser/utf16-be-without-bom.stderr | Bin 3537 -> 3641 bytes tests/ui/parser/utf16-le-without-bom.stderr | Bin 3500 -> 3603 bytes tests/ui/suggestions/issue-89640.rs | 3 +++ tests/ui/suggestions/issue-89640.stderr | 13 +++++++++++++ 5 files changed, 32 insertions(+) create mode 100644 tests/ui/suggestions/issue-89640.rs create mode 100644 tests/ui/suggestions/issue-89640.stderr diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 228eff1269f..0ce6a570d25 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -605,6 +605,22 @@ fn is_ident_eq_keyword(found: &TokenKind, expected: &TokenType) -> bool { } } + if let TokenKind::Ident(prev, _) = &self.prev_token.kind + && let TokenKind::Ident(cur, _) = &self.token.kind + { + let concat = Symbol::intern(&format!("{}{}", prev, cur)); + let ident = Ident::new(concat, DUMMY_SP); + if ident.is_used_keyword() || ident.is_reserved() || ident.is_raw_guess() { + let span = self.prev_token.span.to(self.token.span); + err.span_suggestion_verbose( + span, + format!("consider removing the space to spell keyword `{}`", concat), + concat, + Applicability::MachineApplicable, + ); + } + } + // `pub` may be used for an item or `pub(crate)` if self.prev_token.is_ident_named(sym::public) && (self.token.can_begin_item() diff --git a/tests/ui/parser/utf16-be-without-bom.stderr b/tests/ui/parser/utf16-be-without-bom.stderr index 768d2c531641732c05cff6bd77f1662976f9f6e0..c041f3ecf53b22d8bfd994f7a8c24d583594e027 100644 GIT binary patch delta 92 zcmca8y;EkxQeIC51%(=}jMSV0D~06zyyDE1)FOqV)ZF~C%)E4kl8jV^;)2BFRE3g! rAR{#=MiMIj+AFM$iJ*~dqrMu8!1asZbMQ(fKUCA_)-=}{j4 delta 11 Scmdlfb5VN3Qr^kDd^!Le@dN$< diff --git a/tests/ui/parser/utf16-le-without-bom.stderr b/tests/ui/parser/utf16-le-without-bom.stderr index 4f4b91e39ed8779be5e03134b51ade7a8154ff05..cc2220441ac1073c755b178c4da74085faedd2a1 100644 GIT binary patch delta 91 zcmZ1@Jy~W$1FwgIf $DIR/issue-89640.rs:2:8 + | +LL | le t x: i32 = 3; + | ^ expected one of 8 possible tokens + | +help: consider removing the space to spell keyword `let` + | +LL | let x: i32 = 3; + | ~~~ + +error: aborting due to previous error +