]> git.lizzy.rs Git - rust.git/commitdiff
Implement `..` syntax for RangeFull as expression
authorUlrik Sverdrup <root@localhost>
Wed, 4 Feb 2015 22:23:12 +0000 (23:23 +0100)
committerUlrik Sverdrup <root@localhost>
Wed, 4 Feb 2015 22:23:12 +0000 (23:23 +0100)
Allows the expression `..` (without either endpoint) in general, can be
used in slicing syntax `&expr[..]` where we previously wrote `&expr[]`.

The old syntax &expr[] is not yet removed or warned for.

src/libsyntax/parse/parser.rs
src/test/compile-fail/slice-1.rs
src/test/compile-fail/slice-2.rs
src/test/run-pass/range.rs
src/test/run-pass/ranges-precedence.rs
src/test/run-pass/slice-2.rs

index c3182602a4b89672cab1ff008c7b0a569a6d5946..d8d03349019bdbccf0ca773f36c6ba9530d5ffa5 100644 (file)
@@ -2524,16 +2524,7 @@ pub fn parse_dot_or_call_expr_with(&mut self, e0: P<Expr>) -> P<Expr> {
                 let bracket_pos = self.span.lo;
                 self.bump();
 
-                let mut found_dotdot = false;
-                if self.token == token::DotDot &&
-                   self.look_ahead(1, |t| t == &token::CloseDelim(token::Bracket)) {
-                    // Using expr[..], which is a mistake, should be expr[]
-                    self.bump();
-                    self.bump();
-                    found_dotdot = true;
-                }
-
-                if found_dotdot || self.eat(&token::CloseDelim(token::Bracket)) {
+                if self.eat(&token::CloseDelim(token::Bracket)) {
                     // No expression, expand to a RangeFull
                     // FIXME(#20516) It would be better to use a lang item or
                     // something for RangeFull.
@@ -2557,7 +2548,11 @@ pub fn parse_dot_or_call_expr_with(&mut self, e0: P<Expr>) -> P<Expr> {
                     let range = ExprStruct(path, vec![], None);
                     let ix = self.mk_expr(bracket_pos, hi, range);
                     let index = self.mk_index(e, ix);
-                    e = self.mk_expr(lo, hi, index)
+                    e = self.mk_expr(lo, hi, index);
+                    // Enable after snapshot.
+                    // self.span_warn(e.span, "deprecated slicing syntax: `[]`");
+                    // self.span_note(e.span,
+                    //               "use `&expr[..]` to construct a slice of the whole of expr");
                 } else {
                     let ix = self.parse_expr();
                     hi = self.span.hi;
@@ -2566,11 +2561,6 @@ pub fn parse_dot_or_call_expr_with(&mut self, e0: P<Expr>) -> P<Expr> {
                     e = self.mk_expr(lo, hi, index)
                 }
 
-                if found_dotdot {
-                    self.span_err(e.span, "incorrect slicing expression: `[..]`");
-                    self.span_note(e.span,
-                                   "use `&expr[]` to construct a slice of the whole of expr");
-                }
               }
               _ => return e
             }
@@ -2931,9 +2921,14 @@ pub fn parse_assign_expr(&mut self) -> P<Expr> {
             // with the postfix-form 'expr..'
             let lo = self.span.lo;
             self.bump();
-            let rhs = self.parse_binops();
-            let hi = rhs.span.hi;
-            let ex = self.mk_range(None, Some(rhs));
+            let opt_end = if self.is_at_start_of_range_notation_rhs() {
+                let end = self.parse_binops();
+                Some(end)
+            } else {
+                None
+            };
+            let hi = self.span.hi;
+            let ex = self.mk_range(None, opt_end);
             self.mk_expr(lo, hi, ex)
           }
           _ => {
index 903760caf1a1eb64bf73184e490c8ed8e24e1d72..23ad5b09950effba47277fc56bf1533bb6b3f49e 100644 (file)
@@ -8,12 +8,15 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-// Test slicing expr[..] is an error and gives a helpful error message.
+// Test slicing &expr[] is deprecated and gives a helpful error message.
+//
+// ignore-test
 
 struct Foo;
 
 fn main() {
     let x = Foo;
-    &x[..]; //~ ERROR incorrect slicing expression: `[..]`
-    //~^ NOTE use `&expr[]` to construct a slice of the whole of expr
+    &x[]; //~ WARNING deprecated slicing syntax: `[]`
+          //~^ NOTE use `&expr[..]` to construct a slice of the whole of expr
+          //~^^ ERROR cannot index a value of type `Foo`
 }
index 07162293565ed2c9e3f93e4d89521212497bda44..99dc3e68c8f796d47f78ea644932096619e7dfab 100644 (file)
@@ -14,7 +14,7 @@
 
 fn main() {
     let x = Foo;
-    &x[]; //~ ERROR cannot index a value of type `Foo`
+    &x[..]; //~ ERROR cannot index a value of type `Foo`
     &x[Foo..]; //~ ERROR cannot index a value of type `Foo`
     &x[..Foo]; //~ ERROR cannot index a value of type `Foo`
     &x[Foo..Foo]; //~ ERROR cannot index a value of type `Foo`
index 11e8bfa48f694fd11f373dbdfd4fe4b9f8935a83..5d2337e3819aa925daa363d285530ec6c8df1947 100644 (file)
@@ -14,6 +14,7 @@ fn foo() -> int { 42 }
 
 // Test that range syntax works in return statements
 fn return_range_to() -> ::std::ops::RangeTo<i32> { return ..1; }
+fn return_full_range() -> ::std::ops::RangeFull { return ..; }
 
 pub fn main() {
     let mut count = 0;
index c947220f1f858886c19e93a6706db88dc1582c69..cd49094851695df64a5454475015023341ff0dd2 100644 (file)
@@ -55,5 +55,8 @@ fn main() {
 
     let x = [1]..[2];
     assert!(x == (([1])..([2])));
+
+    let y = ..;
+    assert!(y == (..));
 }
 
index 43e517404cb09b3adc9414f17584d9da7cfae8b2..3f6afc8d987a6b9f0970803036f77b14bb6efd4d 100644 (file)
@@ -14,6 +14,7 @@ fn main() {
     let x: &[int] = &[1, 2, 3, 4, 5];
     let cmp: &[int] = &[1, 2, 3, 4, 5];
     assert!(&x[] == cmp);
+    assert!(&x[..] == cmp);
     let cmp: &[int] = &[3, 4, 5];
     assert!(&x[2..] == cmp);
     let cmp: &[int] = &[1, 2, 3];
@@ -35,6 +36,7 @@ fn main() {
     {
         let cmp: &mut [int] = &mut [1, 2, 3, 4, 5];
         assert!(&mut x[] == cmp);
+        assert!(&mut x[..] == cmp);
     }
     {
         let cmp: &mut [int] = &mut [3, 4, 5];
@@ -53,6 +55,7 @@ fn main() {
     {
         let cmp: &mut [int] = &mut [1, 2, 3, 4, 5];
         assert!(&mut x[] == cmp);
+        assert!(&mut x[..] == cmp);
     }
     {
         let cmp: &mut [int] = &mut [3, 4, 5];