Skip to content

Commit 2e86929

Browse files
committed
Allow use of [_ ; n] syntax for fixed length and repeating arrays.
This does NOT break any existing programs because the `[_, ..n]` syntax is also supported.
1 parent cbe9fb4 commit 2e86929

File tree

122 files changed

+260
-266
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+260
-266
lines changed

src/librustc/util/ppaux.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ pub fn ty_to_string<'tcx>(cx: &ctxt<'tcx>, typ: &ty::TyS<'tcx>) -> String {
452452
ty_vec(t, sz) => {
453453
let inner_str = ty_to_string(cx, t);
454454
match sz {
455-
Some(n) => format!("[{}, ..{}]", inner_str, n),
455+
Some(n) => format!("[{}; {}]", inner_str, n),
456456
None => format!("[{}]", inner_str),
457457
}
458458
}

src/librustc_trans/trans/debuginfo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ impl<'tcx> TypeMap<'tcx> {
334334
// mut ptr (*mut) -> {*mut :pointee-uid:}
335335
// unique ptr (~) -> {~ :pointee-uid:}
336336
// @-ptr (@) -> {@ :pointee-uid:}
337-
// sized vec ([T, ..x]) -> {[:size:] :element-uid:}
337+
// sized vec ([T; x]) -> {[:size:] :element-uid:}
338338
// unsized vec ([T]) -> {[] :element-uid:}
339339
// trait (T) -> {trait_:svh: / :node-id:_<(:param-uid:),*> }
340340
// closure -> {<unsafe_> <once_> :store-sigil: |(:param-uid:),* <,_...>| -> \
@@ -3752,7 +3752,7 @@ fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
37523752

37533753
match optional_length {
37543754
Some(len) => {
3755-
output.push_str(format!(", ..{}", len).as_slice());
3755+
output.push_str(format!("; {}", len).as_slice());
37563756
}
37573757
None => { /* nothing to do */ }
37583758
};

src/libsyntax/parse/parser.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,7 @@ impl<'a> Parser<'a> {
15481548
self.expect(&token::OpenDelim(token::Bracket));
15491549
let t = self.parse_ty_sum();
15501550

1551-
// Parse the `, ..e` in `[ int, ..e ]`
1551+
// Parse the `; e` in `[ int; e ]`
15521552
// where `e` is a const expression
15531553
let t = match self.maybe_parse_fixed_vstore() {
15541554
None => TyVec(t),
@@ -1716,6 +1716,9 @@ impl<'a> Parser<'a> {
17161716
self.bump();
17171717
self.bump();
17181718
Some(self.parse_expr())
1719+
} else if self.check(&token::Semi) {
1720+
self.bump();
1721+
Some(self.parse_expr())
17191722
} else {
17201723
None
17211724
}
@@ -2262,6 +2265,12 @@ impl<'a> Parser<'a> {
22622265
let count = self.parse_expr();
22632266
self.expect(&token::CloseDelim(token::Bracket));
22642267
ex = ExprRepeat(first_expr, count);
2268+
} else if self.check(&token::Semi) {
2269+
// Repeating vector syntax: [ 0; 512 ]
2270+
self.bump();
2271+
let count = self.parse_expr();
2272+
self.expect(&token::CloseDelim(token::Bracket));
2273+
ex = ExprRepeat(first_expr, count);
22652274
} else if self.check(&token::Comma) {
22662275
// Vector with two or more elements.
22672276
self.bump();

src/libsyntax/print/pprust.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ impl<'a> State<'a> {
755755
ast::TyFixedLengthVec(ref ty, ref v) => {
756756
try!(word(&mut self.s, "["));
757757
try!(self.print_type(&**ty));
758-
try!(word(&mut self.s, ", .."));
758+
try!(word(&mut self.s, "; "));
759759
try!(self.print_expr(&**v));
760760
try!(word(&mut self.s, "]"));
761761
}
@@ -1531,8 +1531,7 @@ impl<'a> State<'a> {
15311531
try!(self.ibox(indent_unit));
15321532
try!(word(&mut self.s, "["));
15331533
try!(self.print_expr(&**element));
1534-
try!(word(&mut self.s, ","));
1535-
try!(word(&mut self.s, ".."));
1534+
try!(self.word_space(";"));
15361535
try!(self.print_expr(&**count));
15371536
try!(word(&mut self.s, "]"));
15381537
try!(self.end());

src/test/auxiliary/nested_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<T> Foo {
2828
pub struct Parser<T>;
2929
impl<T: std::iter::Iterator<char>> Parser<T> {
3030
fn in_doctype(&mut self) {
31-
static DOCTYPEPattern: [char, ..6] = ['O', 'C', 'T', 'Y', 'P', 'E'];
31+
static DOCTYPEPattern: [char; 6] = ['O', 'C', 'T', 'Y', 'P', 'E'];
3232
}
3333
}
3434

src/test/bench/noise.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@ fn gradient(orig: Vec2, grad: Vec2, p: Vec2) -> f32 {
3737
}
3838

3939
struct Noise2DContext {
40-
rgradients: [Vec2, ..256],
41-
permutations: [i32, ..256],
40+
rgradients: [Vec2; 256],
41+
permutations: [i32; 256],
4242
}
4343

4444
impl Noise2DContext {
4545
fn new() -> Noise2DContext {
4646
let mut rng = StdRng::new().unwrap();
4747

48-
let mut rgradients = [Vec2 { x: 0.0, y: 0.0 }, ..256];
48+
let mut rgradients = [Vec2 { x: 0.0, y: 0.0 }; 256];
4949
for x in rgradients.iter_mut() {
5050
*x = random_gradient(&mut rng);
5151
}
5252

53-
let mut permutations = [0i32, ..256];
53+
let mut permutations = [0i32; 256];
5454
for (i, x) in permutations.iter_mut().enumerate() {
5555
*x = i as i32;
5656
}
@@ -65,7 +65,7 @@ impl Noise2DContext {
6565
self.rgradients[(idx & 255) as uint]
6666
}
6767

68-
fn get_gradients(&self, x: f32, y: f32) -> ([Vec2, ..4], [Vec2, ..4]) {
68+
fn get_gradients(&self, x: f32, y: f32) -> ([Vec2; 4], [Vec2; 4]) {
6969
let x0f = x.floor();
7070
let y0f = y.floor();
7171
let x1f = x0f + 1.0;
@@ -102,7 +102,7 @@ impl Noise2DContext {
102102

103103
fn main() {
104104
let symbols = [' ', '░', '▒', '▓', '█', '█'];
105-
let mut pixels = [0f32, ..256*256];
105+
let mut pixels = [0f32; 256*256];
106106
let n2d = Noise2DContext::new();
107107

108108
for _ in range(0u, 100) {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ fn next_permutation(perm: &mut [i32], count: &mut [i32]) {
6464
}
6565

6666
struct P {
67-
p: [i32, .. 16],
67+
p: [i32; 16],
6868
}
6969

7070
impl Copy for P {}
7171

7272
struct Perm {
73-
cnt: [i32, .. 16],
74-
fact: [u32, .. 16],
73+
cnt: [i32; 16],
74+
fact: [u32; 16],
7575
n: u32,
7676
permcount: u32,
7777
perm: P,
@@ -81,21 +81,21 @@ impl Copy for Perm {}
8181

8282
impl Perm {
8383
fn new(n: u32) -> Perm {
84-
let mut fact = [1, .. 16];
84+
let mut fact = [1; 16];
8585
for i in range(1, n as uint + 1) {
8686
fact[i] = fact[i - 1] * i as u32;
8787
}
8888
Perm {
89-
cnt: [0, .. 16],
89+
cnt: [0; 16],
9090
fact: fact,
9191
n: n,
9292
permcount: 0,
93-
perm: P { p: [0, .. 16 ] }
93+
perm: P { p: [0; 16 ] }
9494
}
9595
}
9696

9797
fn get(&mut self, mut idx: i32) -> P {
98-
let mut pp = [0u8, .. 16];
98+
let mut pp = [0u8; 16];
9999
self.permcount = idx as u32;
100100
for (i, place) in self.perm.p.iter_mut().enumerate() {
101101
*place = i as i32 + 1;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const ALU: &'static str = "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTG\
6464

6565
const NULL_AMINO_ACID: AminoAcid = AminoAcid { c: ' ' as u8, p: 0.0 };
6666

67-
static IUB: [AminoAcid, ..15] = [
67+
static IUB: [AminoAcid;15] = [
6868
AminoAcid { c: 'a' as u8, p: 0.27 },
6969
AminoAcid { c: 'c' as u8, p: 0.12 },
7070
AminoAcid { c: 'g' as u8, p: 0.12 },
@@ -82,7 +82,7 @@ static IUB: [AminoAcid, ..15] = [
8282
AminoAcid { c: 'Y' as u8, p: 0.02 },
8383
];
8484

85-
static HOMO_SAPIENS: [AminoAcid, ..4] = [
85+
static HOMO_SAPIENS: [AminoAcid;4] = [
8686
AminoAcid { c: 'a' as u8, p: 0.3029549426680 },
8787
AminoAcid { c: 'c' as u8, p: 0.1979883004921 },
8888
AminoAcid { c: 'g' as u8, p: 0.1975473066391 },
@@ -148,8 +148,8 @@ impl<'a, W: Writer> RepeatFasta<'a, W> {
148148
}
149149
}
150150

151-
fn make_lookup(a: &[AminoAcid]) -> [AminoAcid, ..LOOKUP_SIZE] {
152-
let mut lookup = [ NULL_AMINO_ACID, ..LOOKUP_SIZE ];
151+
fn make_lookup(a: &[AminoAcid]) -> [AminoAcid;LOOKUP_SIZE] {
152+
let mut lookup = [ NULL_AMINO_ACID;LOOKUP_SIZE ];
153153
let mut j = 0;
154154
for (i, slot) in lookup.iter_mut().enumerate() {
155155
while a[j].p < (i as f32) {
@@ -162,7 +162,7 @@ fn make_lookup(a: &[AminoAcid]) -> [AminoAcid, ..LOOKUP_SIZE] {
162162

163163
struct RandomFasta<'a, W:'a> {
164164
seed: u32,
165-
lookup: [AminoAcid, ..LOOKUP_SIZE],
165+
lookup: [AminoAcid;LOOKUP_SIZE],
166166
out: &'a mut W,
167167
}
168168

@@ -193,7 +193,7 @@ impl<'a, W: Writer> RandomFasta<'a, W> {
193193
fn make(&mut self, n: uint) -> IoResult<()> {
194194
let lines = n / LINE_LEN;
195195
let chars_left = n % LINE_LEN;
196-
let mut buf = [0, ..LINE_LEN + 1];
196+
let mut buf = [0;LINE_LEN + 1];
197197

198198
for _ in range(0, lines) {
199199
for i in range(0u, LINE_LEN) {

src/test/bench/shootout-fasta.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ fn make_fasta<W: Writer, I: Iterator<u8>>(
8989
-> std::io::IoResult<()>
9090
{
9191
try!(wr.write(header.as_bytes()));
92-
let mut line = [0u8, .. LINE_LENGTH + 1];
92+
let mut line = [0u8; LINE_LENGTH + 1];
9393
while n > 0 {
9494
let nb = min(LINE_LENGTH, n);
9595
for i in range(0, nb) {

src/test/bench/shootout-k-nucleotide.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ use std::string::String;
4646
use std::slice;
4747
use std::sync::{Arc, Future};
4848

49-
static TABLE: [u8, ..4] = [ 'A' as u8, 'C' as u8, 'G' as u8, 'T' as u8 ];
49+
static TABLE: [u8;4] = [ 'A' as u8, 'C' as u8, 'G' as u8, 'T' as u8 ];
5050
static TABLE_SIZE: uint = 2 << 16;
5151

52-
static OCCURRENCES: [&'static str, ..5] = [
52+
static OCCURRENCES: [&'static str;5] = [
5353
"GGT",
5454
"GGTA",
5555
"GGTATT",

src/test/bench/shootout-nbody.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const SOLAR_MASS: f64 = 4.0 * PI * PI;
4545
const YEAR: f64 = 365.24;
4646
const N_BODIES: uint = 5;
4747

48-
static BODIES: [Planet, ..N_BODIES] = [
48+
static BODIES: [Planet;N_BODIES] = [
4949
// Sun
5050
Planet {
5151
x: 0.0, y: 0.0, z: 0.0,
@@ -102,7 +102,7 @@ struct Planet {
102102

103103
impl Copy for Planet {}
104104

105-
fn advance(bodies: &mut [Planet, ..N_BODIES], dt: f64, steps: int) {
105+
fn advance(bodies: &mut [Planet;N_BODIES], dt: f64, steps: int) {
106106
for _ in range(0, steps) {
107107
let mut b_slice = bodies.as_mut_slice();
108108
loop {
@@ -135,7 +135,7 @@ fn advance(bodies: &mut [Planet, ..N_BODIES], dt: f64, steps: int) {
135135
}
136136
}
137137

138-
fn energy(bodies: &[Planet, ..N_BODIES]) -> f64 {
138+
fn energy(bodies: &[Planet;N_BODIES]) -> f64 {
139139
let mut e = 0.0;
140140
let mut bodies = bodies.iter();
141141
loop {
@@ -155,7 +155,7 @@ fn energy(bodies: &[Planet, ..N_BODIES]) -> f64 {
155155
e
156156
}
157157

158-
fn offset_momentum(bodies: &mut [Planet, ..N_BODIES]) {
158+
fn offset_momentum(bodies: &mut [Planet;N_BODIES]) {
159159
let mut px = 0.0;
160160
let mut py = 0.0;
161161
let mut pz = 0.0;

src/test/bench/shootout-reverse-complement.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ use std::ptr::{copy_memory};
5050
use std::io::{IoResult, EndOfFile};
5151

5252
struct Tables {
53-
table8: [u8, ..1 << 8],
54-
table16: [u16, ..1 << 16]
53+
table8: [u8;1 << 8],
54+
table16: [u16;1 << 16]
5555
}
5656

5757
impl Tables {
5858
fn new() -> Tables {
59-
let mut table8 = [0, ..1 << 8];
59+
let mut table8 = [0;1 << 8];
6060
for (i, v) in table8.iter_mut().enumerate() {
6161
*v = Tables::computed_cpl8(i as u8);
6262
}
63-
let mut table16 = [0, ..1 << 16];
63+
let mut table16 = [0;1 << 16];
6464
for (i, v) in table16.iter_mut().enumerate() {
6565
*v = table8[i & 255] as u16 << 8 |
6666
table8[i >> 8] as u16;

src/test/bench/sudoku.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Sudoku {
4646
return Sudoku { grid: g }
4747
}
4848

49-
pub fn from_vec(vec: &[[u8, ..9], ..9]) -> Sudoku {
49+
pub fn from_vec(vec: &[[u8;9];9]) -> Sudoku {
5050
let g = Vec::from_fn(9u, |i| {
5151
Vec::from_fn(9u, |j| { vec[i][j] })
5252
});
@@ -198,7 +198,7 @@ impl Colors {
198198
}
199199
}
200200

201-
static DEFAULT_SUDOKU: [[u8, ..9], ..9] = [
201+
static DEFAULT_SUDOKU: [[u8;9];9] = [
202202
/* 0 1 2 3 4 5 6 7 8 */
203203
/* 0 */ [0u8, 4u8, 0u8, 6u8, 0u8, 0u8, 0u8, 3u8, 2u8],
204204
/* 1 */ [0u8, 0u8, 8u8, 0u8, 2u8, 0u8, 0u8, 0u8, 0u8],
@@ -212,7 +212,7 @@ static DEFAULT_SUDOKU: [[u8, ..9], ..9] = [
212212
];
213213

214214
#[cfg(test)]
215-
static DEFAULT_SOLUTION: [[u8, ..9], ..9] = [
215+
static DEFAULT_SOLUTION: [[u8;9];9] = [
216216
/* 0 1 2 3 4 5 6 7 8 */
217217
/* 0 */ [1u8, 4u8, 9u8, 6u8, 7u8, 5u8, 8u8, 3u8, 2u8],
218218
/* 1 */ [5u8, 3u8, 8u8, 1u8, 2u8, 9u8, 7u8, 4u8, 6u8],

src/test/compile-fail/better-expected.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
let x: [int ..3]; //~ ERROR expected one of `(`, `+`, `,`, `::`, or `]`, found `..`
12+
let x: [int 3]; //~ ERROR expected one of `(`, `+`, `,`, `::`, `;`, or `]`, found `3`
1313
}

src/test/compile-fail/borrowck-for-loop-correct-cmt-for-pattern.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// Issue #16205.
1212

1313
struct Foo {
14-
a: [Box<int>, ..3],
14+
a: [Box<int>; 3],
1515
}
1616

1717
fn main() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// Tests that we forbid coercion from `[T, ..n]` to `&[T]`
11+
// Tests that we forbid coercion from `[T; n]` to `&[T]`
1212

1313
fn main() {
14-
let _: &[int] = [0i]; //~ERROR: mismatched types: expected `&[int]`, found `[int, ..1]`
14+
let _: &[int] = [0i]; //~ERROR: mismatched types: expected `&[int]`, found `[int; 1]`
1515
}

src/test/compile-fail/const-cast-wrong-type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
static a: [u8, ..3] = ['h' as u8, 'i' as u8, 0 as u8];
11+
static a: [u8; 3] = ['h' as u8, 'i' as u8, 0 as u8];
1212
static b: *const i8 = &a as *const i8; //~ ERROR mismatched types
1313

1414
fn main() {

src/test/compile-fail/dst-bad-coerce1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ trait Bar {}
2020
pub fn main() {
2121
// With a vec of ints.
2222
let f1 = Fat { ptr: [1, 2, 3] };
23-
let f2: &Fat<[int, ..3]> = &f1;
23+
let f2: &Fat<[int; 3]> = &f1;
2424
let f3: &Fat<[uint]> = f2;
25-
//~^ ERROR mismatched types: expected `&Fat<[uint]>`, found `&Fat<[int, ..3]>`
25+
//~^ ERROR mismatched types: expected `&Fat<[uint]>`, found `&Fat<[int; 3]>`
2626

2727
// With a trait.
2828
let f1 = Fat { ptr: Foo };

src/test/compile-fail/dst-bad-coerce2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Bar for Foo {}
2121
pub fn main() {
2222
// With a vec of ints.
2323
let f1 = Fat { ptr: [1, 2, 3] };
24-
let f2: &Fat<[int, ..3]> = &f1;
24+
let f2: &Fat<[int; 3]> = &f1;
2525
let f3: &mut Fat<[int]> = f2; //~ ERROR mismatched types
2626

2727
// With a trait.

src/test/compile-fail/dst-bad-coerce3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Bar for Foo {}
2121
fn baz<'a>() {
2222
// With a vec of ints.
2323
let f1 = Fat { ptr: [1, 2, 3] };
24-
let f2: &Fat<[int, ..3]> = &f1; //~ ERROR `f1` does not live long enough
24+
let f2: &Fat<[int; 3]> = &f1; //~ ERROR `f1` does not live long enough
2525
let f3: &'a Fat<[int]> = f2;
2626

2727
// With a trait.

0 commit comments

Comments
 (0)