From e65c060d78db59c5f21cb13f4ec322e407f0a642 Mon Sep 17 00:00:00 2001 From: Hirochika Matsumoto Date: Wed, 3 May 2023 22:32:52 +0900 Subject: Detect Python-like slicing and suggest how to fix Fix #108215 --- compiler/rustc_parse/src/parser/stmt.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'compiler/rustc_parse/src') diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 5316dde6096..94d200188ea 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -567,6 +567,22 @@ impl<'a> Parser<'a> { snapshot.recover_diff_marker(); } if self.token == token::Colon { + // if a previous and next token of the current one is + // integer literal (e.g. `1:42`), it's likely a range + // expression for Pythonistas and we can suggest so. + if self.prev_token.is_integer_lit() + && self.look_ahead(1, |token| token.is_integer_lit()) + { + // TODO(hkmatsumoto): Might be better to trigger + // this only when parsing an index expression. + err.span_suggestion_verbose( + self.token.span, + "you might have meant to make a slice with range index", + "..", + Applicability::MaybeIncorrect, + ); + } + // if next token is following a colon, it's likely a path // and we can suggest a path separator self.bump(); -- cgit 1.4.1-3-g733a5