diff options
| author | yukang <moorekang@gmail.com> | 2023-07-03 23:54:49 +0800 |
|---|---|---|
| committer | yukang <moorekang@gmail.com> | 2023-07-04 18:13:31 +0800 |
| commit | 799d2917e7bb36c95224436a39cd2d468f93f45d (patch) | |
| tree | 55ac2d0caa0d6e6620b1c2a6f757a79233c89a38 | |
| parent | 0ab38e95bb1cbf0bd038d359bdecbfa501f003a7 (diff) | |
| download | rust-799d2917e7bb36c95224436a39cd2d468f93f45d.tar.gz rust-799d2917e7bb36c95224436a39cd2d468f93f45d.zip | |
Detect extra space in keyword for better hint
| -rw-r--r-- | compiler/rustc_parse/src/parser/diagnostics.rs | 16 | ||||
| -rw-r--r-- | tests/ui/parser/utf16-be-without-bom.stderr | bin | 3537 -> 3641 bytes | |||
| -rw-r--r-- | tests/ui/parser/utf16-le-without-bom.stderr | bin | 3500 -> 3603 bytes | |||
| -rw-r--r-- | tests/ui/suggestions/issue-89640.rs | 3 | ||||
| -rw-r--r-- | tests/ui/suggestions/issue-89640.stderr | 13 |
5 files changed, 32 insertions, 0 deletions
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 @@ impl<'a> Parser<'a> { } } + 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 768d2c53164..c041f3ecf53 100644 --- a/tests/ui/parser/utf16-be-without-bom.stderr +++ b/tests/ui/parser/utf16-be-without-bom.stderr Binary files differdiff --git a/tests/ui/parser/utf16-le-without-bom.stderr b/tests/ui/parser/utf16-le-without-bom.stderr index 4f4b91e39ed..cc2220441ac 100644 --- a/tests/ui/parser/utf16-le-without-bom.stderr +++ b/tests/ui/parser/utf16-le-without-bom.stderr Binary files differdiff --git a/tests/ui/suggestions/issue-89640.rs b/tests/ui/suggestions/issue-89640.rs new file mode 100644 index 00000000000..6bb33ad8f30 --- /dev/null +++ b/tests/ui/suggestions/issue-89640.rs @@ -0,0 +1,3 @@ +fn main() { + le t x: i32 = 3; //~ ERROR expected one of +} diff --git a/tests/ui/suggestions/issue-89640.stderr b/tests/ui/suggestions/issue-89640.stderr new file mode 100644 index 00000000000..8ff4ef4f0ec --- /dev/null +++ b/tests/ui/suggestions/issue-89640.stderr @@ -0,0 +1,13 @@ +error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `t` + --> $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 + |
