]> git.lizzy.rs Git - rust.git/commitdiff
Allow non-literal static range pattern for match arms
authorJeong YunWon <jeong@youknowone.org>
Sun, 5 May 2013 01:57:14 +0000 (10:57 +0900)
committerJeong YunWon <jeong@youknowone.org>
Sun, 5 May 2013 02:05:06 +0000 (11:05 +0900)
Fix unintended error problem of:

static s: int = 1;
static e: int = 42;

fn main() {
    match 7 {
        s..e => (),
         ^~                 error: expected `=>` but found `..`
        _ => (),
    }
}

src/libsyntax/parse/parser.rs
src/test/run-pass/match-range-static.rs [new file with mode: 0644]

index a6528160398e88b2aa8a8dace5cc63a492cf3b9d..de88089b1bf7d5fb2e0f0903fd3bee8b1ea58abc 100644 (file)
@@ -2383,7 +2383,13 @@ fn parse_pat(&self, refutable: bool) -> @pat {
                         can_be_enum_or_struct = false
                 }
 
-                if is_plain_ident(&*self.token) && !can_be_enum_or_struct {
+                if self.look_ahead(1) == token::DOTDOT {
+                    let start = self.parse_expr_res(RESTRICT_NO_BAR_OP);
+                    self.eat(&token::DOTDOT);
+                    let end = self.parse_expr_res(RESTRICT_NO_BAR_OP);
+                    pat = pat_range(start, end);
+                }
+                else if is_plain_ident(&*self.token) && !can_be_enum_or_struct {
                     let name = self.parse_path_without_tps();
                     let sub;
                     if self.eat(&token::AT) {
@@ -2392,7 +2398,7 @@ fn parse_pat(&self, refutable: bool) -> @pat {
                     } else {
                         // or just foo
                         sub = None;
-                    };
+                    }
                     pat = pat_ident(binding_mode, name, sub);
                 } else {
                     // parse an enum pat
diff --git a/src/test/run-pass/match-range-static.rs b/src/test/run-pass/match-range-static.rs
new file mode 100644 (file)
index 0000000..3eefc38
--- /dev/null
@@ -0,0 +1,10 @@
+static s: int = 1;
+static e: int = 42;
+
+fn main() {
+    match 7 {
+        s..e => (),
+        _ => (),
+    }
+}
+