about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-09-09 07:38:17 -0700
committerAlex Crichton <alex@alexcrichton.com>2014-09-09 12:07:11 -0700
commit2c66c296dba34b1f5674578d85057250576d32ff (patch)
tree27162ce254eefd364d3849e1a2ea1fec81f723e4
parentfb3c67a65c6ed2acf95e8eb658d4072ec32227ff (diff)
parent22179f49e573edf3616895419f1acd8b93be9cb9 (diff)
downloadrust-2c66c296dba34b1f5674578d85057250576d32ff.tar.gz
rust-2c66c296dba34b1f5674578d85057250576d32ff.zip
rollup merge of #17052 : pcwalton/feature-gate-subslices
-rw-r--r--src/doc/rust.md12
-rw-r--r--src/librustc/front/feature_gate.rs15
-rw-r--r--src/test/compile-fail/borrowck-vec-pattern-element-loan.rs2
-rw-r--r--src/test/compile-fail/borrowck-vec-pattern-nesting.rs1
-rw-r--r--src/test/compile-fail/feature-gate-advanced-slice-features.rs19
-rw-r--r--src/test/compile-fail/non-exhaustive-pattern-witness.rs2
-rw-r--r--src/test/run-pass/ignore-all-the-things.rs2
-rw-r--r--src/test/run-pass/issue-7784.rs2
-rw-r--r--src/test/run-pass/match-vec-alternatives.rs2
-rw-r--r--src/test/run-pass/vec-matching-fixed.rs2
-rw-r--r--src/test/run-pass/vec-matching-fold.rs2
-rw-r--r--src/test/run-pass/vec-matching.rs2
12 files changed, 57 insertions, 6 deletions
diff --git a/src/doc/rust.md b/src/doc/rust.md
index 86776d50e79..552ff716e81 100644
--- a/src/doc/rust.md
+++ b/src/doc/rust.md
@@ -3290,13 +3290,15 @@ between `_` and `..` is that the pattern `C(_)` is only type-correct if `C` has
 exactly one argument, while the pattern `C(..)` is type-correct for any enum
 variant `C`, regardless of how many arguments `C` has.
 
-Used inside a vector pattern, `..` stands for any number of elements. This
-wildcard can be used at most once for a given vector, which implies that it
-cannot be used to specifically match elements that are at an unknown distance
-from both ends of a vector, like `[.., 42, ..]`. If followed by a variable name,
-it will bind the corresponding slice to the variable. Example:
+Used inside a vector pattern, `..` stands for any number of elements, when the
+`advanced_slice_patterns` feature gate is turned on. This wildcard can be used
+at most once for a given vector, which implies that it cannot be used to
+specifically match elements that are at an unknown distance from both ends of a
+vector, like `[.., 42, ..]`.  If followed by a variable name, it will bind the
+corresponding slice to the variable.  Example:
 
 ~~~~
+# #![feature(advanced_slice_patterns)]
 fn is_symmetric(list: &[uint]) -> bool {
     match list {
         [] | [_]                   => true,
diff --git a/src/librustc/front/feature_gate.rs b/src/librustc/front/feature_gate.rs
index 23054347265..c83b81660d5 100644
--- a/src/librustc/front/feature_gate.rs
+++ b/src/librustc/front/feature_gate.rs
@@ -69,6 +69,7 @@ static KNOWN_FEATURES: &'static [(&'static str, Status)] = &[
     ("rustc_diagnostic_macros", Active),
     ("unboxed_closures", Active),
     ("import_shadowing", Active),
+    ("advanced_slice_patterns", Active),
 
     // if you change this list without updating src/doc/rust.md, cmr will be sad
 
@@ -364,6 +365,20 @@ impl<'a> Visitor<()> for Context<'a> {
         }
     }
 
+    fn visit_pat(&mut self, pattern: &ast::Pat, (): ()) {
+        match pattern.node {
+            ast::PatVec(_, Some(_), ref last) if !last.is_empty() => {
+                self.gate_feature("advanced_slice_patterns",
+                                  pattern.span,
+                                  "multiple-element slice matches anywhere \
+                                   but at the end of a slice (e.g. \
+                                   `[0, ..xs, 0]` are experimental")
+            }
+            _ => {}
+        }
+        visit::walk_pat(self, pattern, ())
+    }
+
     fn visit_fn(&mut self,
                 fn_kind: &visit::FnKind,
                 fn_decl: &ast::FnDecl,
diff --git a/src/test/compile-fail/borrowck-vec-pattern-element-loan.rs b/src/test/compile-fail/borrowck-vec-pattern-element-loan.rs
index 53ebaa38fdd..62f4f4aaede 100644
--- a/src/test/compile-fail/borrowck-vec-pattern-element-loan.rs
+++ b/src/test/compile-fail/borrowck-vec-pattern-element-loan.rs
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#![feature(advanced_slice_patterns)]
+
 fn a<'a>() -> &'a [int] {
     let vec = vec!(1, 2, 3, 4);
     let vec: &[int] = vec.as_slice(); //~ ERROR does not live long enough
diff --git a/src/test/compile-fail/borrowck-vec-pattern-nesting.rs b/src/test/compile-fail/borrowck-vec-pattern-nesting.rs
index 4a56f982106..51e3c612c71 100644
--- a/src/test/compile-fail/borrowck-vec-pattern-nesting.rs
+++ b/src/test/compile-fail/borrowck-vec-pattern-nesting.rs
@@ -8,6 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#![feature(advanced_slice_patterns)]
 
 fn a() {
     let mut vec = [box 1i, box 2, box 3];
diff --git a/src/test/compile-fail/feature-gate-advanced-slice-features.rs b/src/test/compile-fail/feature-gate-advanced-slice-features.rs
new file mode 100644
index 00000000000..c6753baf057
--- /dev/null
+++ b/src/test/compile-fail/feature-gate-advanced-slice-features.rs
@@ -0,0 +1,19 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn main() {
+    let x = [ 1i, 2, 3, 4, 5 ];
+    match x {
+        [ ..xs, 4, 5 ] => {}    //~ ERROR multiple-element slice matches
+        [ 1, ..xs, 5 ] => {}    //~ ERROR multiple-element slice matches
+        [ 1, 2, ..xs ] => {}    // OK without feature gate
+    }
+}
+
diff --git a/src/test/compile-fail/non-exhaustive-pattern-witness.rs b/src/test/compile-fail/non-exhaustive-pattern-witness.rs
index 6dc5ad8b606..7b52e7774d4 100644
--- a/src/test/compile-fail/non-exhaustive-pattern-witness.rs
+++ b/src/test/compile-fail/non-exhaustive-pattern-witness.rs
@@ -8,7 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-#![feature(struct_variant)]
+#![feature(advanced_slice_patterns, struct_variant)]
 
 struct Foo {
     first: bool,
diff --git a/src/test/run-pass/ignore-all-the-things.rs b/src/test/run-pass/ignore-all-the-things.rs
index 27c63d425bf..1c87b6dad89 100644
--- a/src/test/run-pass/ignore-all-the-things.rs
+++ b/src/test/run-pass/ignore-all-the-things.rs
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#![feature(advanced_slice_patterns)]
+
 struct Foo(int, int, int, int);
 struct Bar{a: int, b: int, c: int, d: int}
 
diff --git a/src/test/run-pass/issue-7784.rs b/src/test/run-pass/issue-7784.rs
index d307a057038..f45b988b836 100644
--- a/src/test/run-pass/issue-7784.rs
+++ b/src/test/run-pass/issue-7784.rs
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#![feature(advanced_slice_patterns)]
+
 fn foo<T: Add<T, T> + Clone>([x, y, z]: [T, ..3]) -> (T, T, T) {
     (x.clone(), x.clone() + y.clone(), x + y + z)
 }
diff --git a/src/test/run-pass/match-vec-alternatives.rs b/src/test/run-pass/match-vec-alternatives.rs
index de1bb02bfef..ae4fd1f1993 100644
--- a/src/test/run-pass/match-vec-alternatives.rs
+++ b/src/test/run-pass/match-vec-alternatives.rs
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#![feature(advanced_slice_patterns)]
+
 fn match_vecs<'a, T>(l1: &'a [T], l2: &'a [T]) -> &'static str {
     match (l1, l2) {
         ([], []) => "both empty",
diff --git a/src/test/run-pass/vec-matching-fixed.rs b/src/test/run-pass/vec-matching-fixed.rs
index 3e9d4b9fc3a..a1a14823ff5 100644
--- a/src/test/run-pass/vec-matching-fixed.rs
+++ b/src/test/run-pass/vec-matching-fixed.rs
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#![feature(advanced_slice_patterns)]
+
 fn a() {
     let x = [1i, 2, 3];
     match x {
diff --git a/src/test/run-pass/vec-matching-fold.rs b/src/test/run-pass/vec-matching-fold.rs
index 07ee5f535e9..3a55b4b68a9 100644
--- a/src/test/run-pass/vec-matching-fold.rs
+++ b/src/test/run-pass/vec-matching-fold.rs
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#![feature(advanced_slice_patterns)]
+
 fn foldl<T,U:Clone>(values: &[T],
                     initial: U,
                     function: |partial: U, element: &T| -> U)
diff --git a/src/test/run-pass/vec-matching.rs b/src/test/run-pass/vec-matching.rs
index e95495a42d2..89d4e3743e3 100644
--- a/src/test/run-pass/vec-matching.rs
+++ b/src/test/run-pass/vec-matching.rs
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#![feature(advanced_slice_patterns)]
+
 fn a() {
     let x = [1i];
     match x {