Skip to content

Commit 715f9a5

Browse files
committed
Auto merge of rust-lang#21947 - bluss:full-range-syntax, r=brson
Implement step 1 of rust-lang/rfcs#702 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.
2 parents 706be5b + 7d527fa commit 715f9a5

18 files changed

+51
-51
lines changed

src/libsyntax/parse/parser.rs

+14-19
Original file line numberDiff line numberDiff line change
@@ -2527,16 +2527,7 @@ impl<'a> Parser<'a> {
25272527
let bracket_pos = self.span.lo;
25282528
self.bump();
25292529

2530-
let mut found_dotdot = false;
2531-
if self.token == token::DotDot &&
2532-
self.look_ahead(1, |t| t == &token::CloseDelim(token::Bracket)) {
2533-
// Using expr[..], which is a mistake, should be expr[]
2534-
self.bump();
2535-
self.bump();
2536-
found_dotdot = true;
2537-
}
2538-
2539-
if found_dotdot || self.eat(&token::CloseDelim(token::Bracket)) {
2530+
if self.eat(&token::CloseDelim(token::Bracket)) {
25402531
// No expression, expand to a RangeFull
25412532
// FIXME(#20516) It would be better to use a lang item or
25422533
// something for RangeFull.
@@ -2560,7 +2551,11 @@ impl<'a> Parser<'a> {
25602551
let range = ExprStruct(path, vec![], None);
25612552
let ix = self.mk_expr(bracket_pos, hi, range);
25622553
let index = self.mk_index(e, ix);
2563-
e = self.mk_expr(lo, hi, index)
2554+
e = self.mk_expr(lo, hi, index);
2555+
// Enable after snapshot.
2556+
// self.span_warn(e.span, "deprecated slicing syntax: `[]`");
2557+
// self.span_note(e.span,
2558+
// "use `&expr[..]` to construct a slice of the whole of expr");
25642559
} else {
25652560
let ix = self.parse_expr();
25662561
hi = self.span.hi;
@@ -2569,11 +2564,6 @@ impl<'a> Parser<'a> {
25692564
e = self.mk_expr(lo, hi, index)
25702565
}
25712566

2572-
if found_dotdot {
2573-
self.span_err(e.span, "incorrect slicing expression: `[..]`");
2574-
self.span_note(e.span,
2575-
"use `&expr[]` to construct a slice of the whole of expr");
2576-
}
25772567
}
25782568
_ => return e
25792569
}
@@ -2934,9 +2924,14 @@ impl<'a> Parser<'a> {
29342924
// with the postfix-form 'expr..'
29352925
let lo = self.span.lo;
29362926
self.bump();
2937-
let rhs = self.parse_binops();
2938-
let hi = rhs.span.hi;
2939-
let ex = self.mk_range(None, Some(rhs));
2927+
let opt_end = if self.is_at_start_of_range_notation_rhs() {
2928+
let end = self.parse_binops();
2929+
Some(end)
2930+
} else {
2931+
None
2932+
};
2933+
let hi = self.span.hi;
2934+
let ex = self.mk_range(None, opt_end);
29402935
self.mk_expr(lo, hi, ex)
29412936
}
29422937
_ => {

src/libsyntax/print/pprust.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1811,9 +1811,7 @@ impl<'a> State<'a> {
18111811
if let &Some(ref e) = start {
18121812
try!(self.print_expr(&**e));
18131813
}
1814-
if start.is_some() || end.is_some() {
1815-
try!(word(&mut self.s, ".."));
1816-
}
1814+
try!(word(&mut self.s, ".."));
18171815
if let &Some(ref e) = end {
18181816
try!(self.print_expr(&**e));
18191817
}

src/test/bench/shootout-fasta-redux.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl<'a, W: Writer> RandomFasta<'a, W> {
180180

181181
fn nextc(&mut self) -> u8 {
182182
let r = self.rng(1.0);
183-
for a in &self.lookup[] {
183+
for a in &self.lookup[..] {
184184
if a.p >= r {
185185
return a.c;
186186
}

src/test/compile-fail/packed-struct-generic-transmute.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ fn main() {
3232
let foo = Foo { bar: [1u8, 2, 3, 4, 5], baz: 10i32 };
3333
unsafe {
3434
let oof: Oof<[u8; 5], i32> = mem::transmute(foo);
35-
println!("{:?} {:?}", &oof.rab[], oof.zab);
35+
println!("{:?} {:?}", &oof.rab[..], oof.zab);
3636
}
3737
}

src/test/compile-fail/slice-1.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// Test slicing expr[..] is an error and gives a helpful error message.
11+
// Test slicing &expr[] is deprecated and gives a helpful error message.
12+
//
13+
// ignore-test
1214

1315
struct Foo;
1416

1517
fn main() {
1618
let x = Foo;
17-
&x[..]; //~ ERROR incorrect slicing expression: `[..]`
18-
//~^ NOTE use `&expr[]` to construct a slice of the whole of expr
19+
&x[]; //~ WARNING deprecated slicing syntax: `[]`
20+
//~^ NOTE use `&expr[..]` to construct a slice of the whole of expr
21+
//~^^ ERROR cannot index a value of type `Foo`
1922
}

src/test/compile-fail/slice-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct Foo;
1414

1515
fn main() {
1616
let x = Foo;
17-
&x[]; //~ ERROR cannot index a value of type `Foo`
17+
&x[..]; //~ ERROR cannot index a value of type `Foo`
1818
&x[Foo..]; //~ ERROR cannot index a value of type `Foo`
1919
&x[..Foo]; //~ ERROR cannot index a value of type `Foo`
2020
&x[Foo..Foo]; //~ ERROR cannot index a value of type `Foo`

src/test/run-pass/auto-encode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn test_rbml<'a, 'b, A:
3535
let mut rbml_w = EBwriter::Encoder::new(&mut wr);
3636
a1.encode(&mut rbml_w);
3737

38-
let d: serialize::rbml::Doc<'a> = EBDoc::new(&wr[]);
38+
let d: serialize::rbml::Doc<'a> = EBDoc::new(&wr);
3939
let mut decoder: EBReader::Decoder<'a> = EBreader::Decoder::new(d);
4040
let a2: A = Decodable::decode(&mut decoder);
4141
assert!(*a1 == a2);

src/test/run-pass/coerce-overloaded-autoderef.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ fn use_slice(_: &[u8]) {}
4747
fn use_slice_mut(_: &mut [u8]) {}
4848

4949
fn use_vec(mut v: Vec<u8>) {
50-
use_slice_mut(&mut v[]); // what you have to write today
51-
use_slice_mut(&mut v); // what you'd be able to write
50+
use_slice_mut(&mut v[..]); // what you have to write today
51+
use_slice_mut(&mut v); // what you'd be able to write
5252
use_slice_mut(&mut &mut &mut v);
5353

54-
use_slice(&v[]); // what you have to write today
54+
use_slice(&v[..]); // what you have to write today
5555
use_slice(&v); // what you'd be able to write
5656
use_slice(&&&&&&v);
5757
use_slice(&mut &&&&&v);
5858
use_slice(&&&mut &&&v);
5959
}
6060

6161
fn use_vec_ref(v: &Vec<u8>) {
62-
use_slice(&v[]); // what you have to write today
62+
use_slice(&v[..]); // what you have to write today
6363
use_slice(v); // what you'd be able to write
6464
use_slice(&&&&&&v);
6565
use_slice(&mut &&&&&v);

src/test/run-pass/deriving-encodable-decodable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn roundtrip<'a, T: Rand + Eq + Encodable<Encoder<'a>> +
5959
let mut w = Vec::new();
6060
let mut e = Encoder::new(&mut w);
6161
obj.encode(&mut e);
62-
let doc = rbml::Doc::new(&w[]);
62+
let doc = rbml::Doc::new(&w);
6363
let mut dec = Decoder::new(doc);
6464
let obj2 = Decodable::decode(&mut dec);
6565
assert!(obj == obj2);

src/test/run-pass/foreach-external-iterators-break.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
pub fn main() {
1212
let x = [1; 100];
1313
let mut y = 0;
14-
for i in &x[] {
14+
for i in &x[..] {
1515
if y > 10 {
1616
break;
1717
}

src/test/run-pass/foreach-external-iterators-nested.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ pub fn main() {
1313
let y = [2; 100];
1414
let mut p = 0;
1515
let mut q = 0;
16-
for i in &x[] {
17-
for j in &y[] {
16+
for i in &x[..] {
17+
for j in &y[..] {
1818
p += *j;
1919
}
2020
q += *i + p;

src/test/run-pass/foreach-external-iterators.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
pub fn main() {
1212
let x = [1; 100];
1313
let mut y = 0;
14-
for i in &x[] {
14+
for i in &x[..] {
1515
y += *i
1616
}
1717
assert!(y == 100);

src/test/run-pass/issue-8898.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ pub fn main() {
1818
let x = [(), ()];
1919
let slice = &x[..1];
2020

21-
assert_repr_eq(&abc[], "[1, 2, 3]".to_string());
22-
assert_repr_eq(&tf[], "[true, false]".to_string());
23-
assert_repr_eq(&x[], "[(), ()]".to_string());
21+
assert_repr_eq(&abc[..], "[1, 2, 3]".to_string());
22+
assert_repr_eq(&tf[..], "[true, false]".to_string());
23+
assert_repr_eq(&x[..], "[(), ()]".to_string());
2424
assert_repr_eq(slice, "[()]".to_string());
25-
assert_repr_eq(&x[], "[(), ()]".to_string());
25+
assert_repr_eq(&x[..], "[(), ()]".to_string());
2626
}

src/test/run-pass/range.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fn foo() -> int { 42 }
1414

1515
// Test that range syntax works in return statements
1616
fn return_range_to() -> ::std::ops::RangeTo<i32> { return ..1; }
17+
fn return_full_range() -> ::std::ops::RangeFull { return ..; }
1718

1819
pub fn main() {
1920
let mut count = 0;

src/test/run-pass/ranges-precedence.rs

+3
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,8 @@ fn main() {
5555

5656
let x = [1]..[2];
5757
assert!(x == (([1])..([2])));
58+
59+
let y = ..;
60+
assert!(y == (..));
5861
}
5962

src/test/run-pass/repeated-vector-syntax.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ pub fn main() {
1313
let y = [ 0; 1 ];
1414

1515
print!("[");
16-
for xi in &x[] {
17-
print!("{:?}, ", &xi[]);
16+
for xi in &x[..] {
17+
print!("{:?}, ", &xi[..]);
1818
}
1919
println!("]");
20-
println!("{:?}", &y[]);
20+
println!("{:?}", &y[..]);
2121
}

src/test/run-pass/slice-2.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
fn main() {
1414
let x: &[int] = &[1, 2, 3, 4, 5];
1515
let cmp: &[int] = &[1, 2, 3, 4, 5];
16-
assert!(&x[] == cmp);
16+
assert!(&x[..] == cmp);
1717
let cmp: &[int] = &[3, 4, 5];
1818
assert!(&x[2..] == cmp);
1919
let cmp: &[int] = &[1, 2, 3];
@@ -23,7 +23,7 @@ fn main() {
2323

2424
let x: Vec<int> = vec![1, 2, 3, 4, 5];
2525
let cmp: &[int] = &[1, 2, 3, 4, 5];
26-
assert!(&x[] == cmp);
26+
assert!(&x[..] == cmp);
2727
let cmp: &[int] = &[3, 4, 5];
2828
assert!(&x[2..] == cmp);
2929
let cmp: &[int] = &[1, 2, 3];
@@ -34,7 +34,7 @@ fn main() {
3434
let x: &mut [int] = &mut [1, 2, 3, 4, 5];
3535
{
3636
let cmp: &mut [int] = &mut [1, 2, 3, 4, 5];
37-
assert!(&mut x[] == cmp);
37+
assert!(&mut x[..] == cmp);
3838
}
3939
{
4040
let cmp: &mut [int] = &mut [3, 4, 5];
@@ -52,7 +52,7 @@ fn main() {
5252
let mut x: Vec<int> = vec![1, 2, 3, 4, 5];
5353
{
5454
let cmp: &mut [int] = &mut [1, 2, 3, 4, 5];
55-
assert!(&mut x[] == cmp);
55+
assert!(&mut x[..] == cmp);
5656
}
5757
{
5858
let cmp: &mut [int] = &mut [3, 4, 5];

src/test/run-pass/slice.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ impl IndexMut<RangeFull> for Foo {
8080

8181
fn main() {
8282
let mut x = Foo;
83-
&x[];
83+
&x[..];
8484
&x[Foo..];
8585
&x[..Foo];
8686
&x[Foo..Foo];
87-
&mut x[];
87+
&mut x[..];
8888
&mut x[Foo..];
8989
&mut x[..Foo];
9090
&mut x[Foo..Foo];

0 commit comments

Comments
 (0)