about summary refs log tree commit diff
path: root/compiler/rustc_parse/src
diff options
context:
space:
mode:
authorHirochika Matsumoto <hirochika.k.matsumoto@gmail.com>2023-05-03 22:32:52 +0900
committerHirochika Matsumoto <hirochika.k.matsumoto@gmail.com>2023-11-27 21:48:10 +0900
commite65c060d78db59c5f21cb13f4ec322e407f0a642 (patch)
tree5e26403f9c50f738189cdcb28c0b339ef2333e29 /compiler/rustc_parse/src
parentb29a1e00f850e548f3021ea523d0e143724fa9b7 (diff)
downloadrust-e65c060d78db59c5f21cb13f4ec322e407f0a642.tar.gz
rust-e65c060d78db59c5f21cb13f4ec322e407f0a642.zip
Detect Python-like slicing and suggest how to fix
Fix #108215
Diffstat (limited to 'compiler/rustc_parse/src')
-rw-r--r--compiler/rustc_parse/src/parser/stmt.rs16
1 files changed, 16 insertions, 0 deletions
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();