]> git.lizzy.rs Git - rust.git/commitdiff
Account for maybe_whole_expr in range patterns.
authorMazdak Farrokhzad <twingoow@gmail.com>
Tue, 30 Jul 2019 02:22:09 +0000 (04:22 +0200)
committerMazdak Farrokhzad <twingoow@gmail.com>
Tue, 30 Jul 2019 02:22:09 +0000 (04:22 +0200)
src/libsyntax/parse/parser.rs
src/libsyntax/parse/token.rs
src/test/ui/parser/issue-63115-range-pat-interpolated.rs [new file with mode: 0644]
src/test/ui/parser/recover-range-pats.rs
src/test/ui/parser/recover-range-pats.stderr

index 6cb965bf817d170892e56254d973553e1ff4c440..2fa6d20430bf1aa9033dc49c54da0db4d1cdb7fc 100644 (file)
@@ -3741,6 +3741,7 @@ fn is_pat_range_end_start(&self) -> bool {
         self.token.is_path_start() // e.g. `MY_CONST`;
             || self.token == token::Dot // e.g. `.5` for recovery;
             || self.token.can_begin_literal_or_bool() // e.g. `42`.
+            || self.token.is_whole_expr()
     }
 
     // Helper function to decide whether to parse as ident binding
index 472e4b474d627513c1c279e0ef94d89ef131200a..d6d13c19f7183c4a94274764068aeb7f9a47eda7 100644 (file)
@@ -476,6 +476,19 @@ fn is_path(&self) -> bool {
         false
     }
 
+    /// Would `maybe_whole_expr` in `parser.rs` return `Ok(..)`?
+    /// That is, is this a pre-parsed expression dropped into the token stream
+    /// (which happens while parsing the result ofmacro expansion)?
+    crate fn is_whole_expr(&self) -> bool {
+        if let Interpolated(ref nt) = self.kind {
+            if let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = **nt {
+                return true;
+            }
+        }
+
+        false
+    }
+
     /// Returns `true` if the token is either the `mut` or `const` keyword.
     crate fn is_mutability(&self) -> bool {
         self.is_keyword(kw::Mut) ||
diff --git a/src/test/ui/parser/issue-63115-range-pat-interpolated.rs b/src/test/ui/parser/issue-63115-range-pat-interpolated.rs
new file mode 100644 (file)
index 0000000..a7d10ca
--- /dev/null
@@ -0,0 +1,16 @@
+// check-pass
+
+#![feature(exclusive_range_pattern)]
+
+#![allow(ellipsis_inclusive_range_patterns)]
+
+fn main() {
+    macro_rules! mac_expr {
+        ($e:expr) => {
+            if let 2...$e = 3 {}
+            if let 2..=$e = 3 {}
+            if let 2..$e = 3 {}
+        }
+    }
+    mac_expr!(4);
+}
index c66652ff4fa01ef73cc0e5200e0d875894a0407a..260e108315973642bee25231146bc77780d777c7 100644 (file)
@@ -121,3 +121,31 @@ fn inclusive2_to() {
     //~| ERROR `...` range patterns are deprecated
     //~| ERROR mismatched types
 }
+
+fn with_macro_expr_var() {
+    macro_rules! mac2 {
+        ($e1:expr, $e2:expr) => {
+            let $e1..$e2;
+            let $e1...$e2;
+            //~^ ERROR `...` range patterns are deprecated
+            let $e1..=$e2;
+        }
+    }
+
+    mac2!(0, 1);
+
+    macro_rules! mac {
+        ($e:expr) => {
+            let ..$e; //~ ERROR `..X` range patterns are not supported
+            let ...$e; //~ ERROR `...X` range patterns are not supported
+            //~^ ERROR `...` range patterns are deprecated
+            let ..=$e; //~ ERROR `..=X` range patterns are not supported
+            let $e..; //~ ERROR `X..` range patterns are not supported
+            let $e...; //~ ERROR `X...` range patterns are not supported
+            //~^ ERROR `...` range patterns are deprecated
+            let $e..=; //~ ERROR `X..=` range patterns are not supported
+        }
+    }
+
+    mac!(0);
+}
index c50d5e6eb61537d83d3b25ec74343774dce304dd..89ec059cb8234c72b7f8ff35735d50327ccef9bb 100644 (file)
@@ -214,6 +214,60 @@ error: `...X` range patterns are not supported
 LL |     if let ....3 = 0 {}
    |            ^^^^^ help: try using the minimum value for the type: `MIN...0.3`
 
+error: `..X` range patterns are not supported
+  --> $DIR/recover-range-pats.rs:139:17
+   |
+LL |             let ..$e;
+   |                 ^^ help: try using the minimum value for the type: `MIN..0`
+...
+LL |     mac!(0);
+   |     -------- in this macro invocation
+
+error: `...X` range patterns are not supported
+  --> $DIR/recover-range-pats.rs:140:17
+   |
+LL |             let ...$e;
+   |                 ^^^ help: try using the minimum value for the type: `MIN...0`
+...
+LL |     mac!(0);
+   |     -------- in this macro invocation
+
+error: `..=X` range patterns are not supported
+  --> $DIR/recover-range-pats.rs:142:17
+   |
+LL |             let ..=$e;
+   |                 ^^^ help: try using the minimum value for the type: `MIN..=0`
+...
+LL |     mac!(0);
+   |     -------- in this macro invocation
+
+error: `X..` range patterns are not supported
+  --> $DIR/recover-range-pats.rs:143:19
+   |
+LL |             let $e..;
+   |                   ^^ help: try using the maximum value for the type: `0..MAX`
+...
+LL |     mac!(0);
+   |     -------- in this macro invocation
+
+error: `X...` range patterns are not supported
+  --> $DIR/recover-range-pats.rs:144:19
+   |
+LL |             let $e...;
+   |                   ^^^ help: try using the maximum value for the type: `0...MAX`
+...
+LL |     mac!(0);
+   |     -------- in this macro invocation
+
+error: `X..=` range patterns are not supported
+  --> $DIR/recover-range-pats.rs:146:19
+   |
+LL |             let $e..=;
+   |                   ^^^ help: try using the maximum value for the type: `0..=MAX`
+...
+LL |     mac!(0);
+   |     -------- in this macro invocation
+
 error: `...` range patterns are deprecated
   --> $DIR/recover-range-pats.rs:41:13
    |
@@ -316,6 +370,33 @@ error: `...` range patterns are deprecated
 LL |     if let ....3 = 0 {}
    |            ^^^ help: use `..=` for an inclusive range
 
+error: `...` range patterns are deprecated
+  --> $DIR/recover-range-pats.rs:129:20
+   |
+LL |             let $e1...$e2;
+   |                    ^^^ help: use `..=` for an inclusive range
+...
+LL |     mac2!(0, 1);
+   |     ------------ in this macro invocation
+
+error: `...` range patterns are deprecated
+  --> $DIR/recover-range-pats.rs:140:17
+   |
+LL |             let ...$e;
+   |                 ^^^ help: use `..=` for an inclusive range
+...
+LL |     mac!(0);
+   |     -------- in this macro invocation
+
+error: `...` range patterns are deprecated
+  --> $DIR/recover-range-pats.rs:144:19
+   |
+LL |             let $e...;
+   |                   ^^^ help: use `..=` for an inclusive range
+...
+LL |     mac!(0);
+   |     -------- in this macro invocation
+
 error[E0029]: only char and numeric types are allowed in range patterns
   --> $DIR/recover-range-pats.rs:19:12
    |
@@ -532,7 +613,7 @@ LL |     if let ....3 = 0 {}
    = note: expected type `{integer}`
               found type `{float}`
 
-error: aborting due to 76 previous errors
+error: aborting due to 85 previous errors
 
 Some errors have detailed explanations: E0029, E0308.
 For more information about an error, try `rustc --explain E0029`.