Skip to content

Allow use of [_ ; n] syntax for fixed length and repeating arrays. #20057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ pub fn ty_to_string<'tcx>(cx: &ctxt<'tcx>, typ: &ty::TyS<'tcx>) -> String {
ty_vec(t, sz) => {
let inner_str = ty_to_string(cx, t);
match sz {
Some(n) => format!("[{}, ..{}]", inner_str, n),
Some(n) => format!("[{}; {}]", inner_str, n),
None => format!("[{}]", inner_str),
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/trans/debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ impl<'tcx> TypeMap<'tcx> {
// mut ptr (*mut) -> {*mut :pointee-uid:}
// unique ptr (~) -> {~ :pointee-uid:}
// @-ptr (@) -> {@ :pointee-uid:}
// sized vec ([T, ..x]) -> {[:size:] :element-uid:}
// sized vec ([T; x]) -> {[:size:] :element-uid:}
// unsized vec ([T]) -> {[] :element-uid:}
// trait (T) -> {trait_:svh: / :node-id:_<(:param-uid:),*> }
// closure -> {<unsafe_> <once_> :store-sigil: |(:param-uid:),* <,_...>| -> \
Expand Down Expand Up @@ -3752,7 +3752,7 @@ fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,

match optional_length {
Some(len) => {
output.push_str(format!(", ..{}", len).as_slice());
output.push_str(format!("; {}", len).as_slice());
}
None => { /* nothing to do */ }
};
Expand Down
11 changes: 10 additions & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ impl<'a> Parser<'a> {
self.expect(&token::OpenDelim(token::Bracket));
let t = self.parse_ty_sum();

// Parse the `, ..e` in `[ int, ..e ]`
// Parse the `; e` in `[ int; e ]`
// where `e` is a const expression
let t = match self.maybe_parse_fixed_vstore() {
None => TyVec(t),
Expand Down Expand Up @@ -1716,6 +1716,9 @@ impl<'a> Parser<'a> {
self.bump();
self.bump();
Some(self.parse_expr())
} else if self.check(&token::Semi) {
self.bump();
Some(self.parse_expr())
} else {
None
}
Expand Down Expand Up @@ -2262,6 +2265,12 @@ impl<'a> Parser<'a> {
let count = self.parse_expr();
self.expect(&token::CloseDelim(token::Bracket));
ex = ExprRepeat(first_expr, count);
} else if self.check(&token::Semi) {
// Repeating vector syntax: [ 0; 512 ]
self.bump();
let count = self.parse_expr();
self.expect(&token::CloseDelim(token::Bracket));
ex = ExprRepeat(first_expr, count);
} else if self.check(&token::Comma) {
// Vector with two or more elements.
self.bump();
Expand Down
5 changes: 2 additions & 3 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ impl<'a> State<'a> {
ast::TyFixedLengthVec(ref ty, ref v) => {
try!(word(&mut self.s, "["));
try!(self.print_type(&**ty));
try!(word(&mut self.s, ", .."));
try!(word(&mut self.s, "; "));
try!(self.print_expr(&**v));
try!(word(&mut self.s, "]"));
}
Expand Down Expand Up @@ -1531,8 +1531,7 @@ impl<'a> State<'a> {
try!(self.ibox(indent_unit));
try!(word(&mut self.s, "["));
try!(self.print_expr(&**element));
try!(word(&mut self.s, ","));
try!(word(&mut self.s, ".."));
try!(self.word_space(";"));
try!(self.print_expr(&**count));
try!(word(&mut self.s, "]"));
try!(self.end());
Expand Down
2 changes: 1 addition & 1 deletion src/test/auxiliary/nested_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<T> Foo {
pub struct Parser<T>;
impl<T: std::iter::Iterator<char>> Parser<T> {
fn in_doctype(&mut self) {
static DOCTYPEPattern: [char, ..6] = ['O', 'C', 'T', 'Y', 'P', 'E'];
static DOCTYPEPattern: [char; 6] = ['O', 'C', 'T', 'Y', 'P', 'E'];
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/test/bench/noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ fn gradient(orig: Vec2, grad: Vec2, p: Vec2) -> f32 {
}

struct Noise2DContext {
rgradients: [Vec2, ..256],
permutations: [i32, ..256],
rgradients: [Vec2; 256],
permutations: [i32; 256],
}

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

let mut rgradients = [Vec2 { x: 0.0, y: 0.0 }, ..256];
let mut rgradients = [Vec2 { x: 0.0, y: 0.0 }; 256];
for x in rgradients.iter_mut() {
*x = random_gradient(&mut rng);
}

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

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

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

for _ in range(0u, 100) {
Expand Down
14 changes: 7 additions & 7 deletions src/test/bench/shootout-fannkuch-redux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ fn next_permutation(perm: &mut [i32], count: &mut [i32]) {
}

struct P {
p: [i32, .. 16],
p: [i32; 16],
}

impl Copy for P {}

struct Perm {
cnt: [i32, .. 16],
fact: [u32, .. 16],
cnt: [i32; 16],
fact: [u32; 16],
n: u32,
permcount: u32,
perm: P,
Expand All @@ -81,21 +81,21 @@ impl Copy for Perm {}

impl Perm {
fn new(n: u32) -> Perm {
let mut fact = [1, .. 16];
let mut fact = [1; 16];
for i in range(1, n as uint + 1) {
fact[i] = fact[i - 1] * i as u32;
}
Perm {
cnt: [0, .. 16],
cnt: [0; 16],
fact: fact,
n: n,
permcount: 0,
perm: P { p: [0, .. 16 ] }
perm: P { p: [0; 16 ] }
}
}

fn get(&mut self, mut idx: i32) -> P {
let mut pp = [0u8, .. 16];
let mut pp = [0u8; 16];
self.permcount = idx as u32;
for (i, place) in self.perm.p.iter_mut().enumerate() {
*place = i as i32 + 1;
Expand Down
12 changes: 6 additions & 6 deletions src/test/bench/shootout-fasta-redux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const ALU: &'static str = "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTG\

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

static IUB: [AminoAcid, ..15] = [
static IUB: [AminoAcid;15] = [
AminoAcid { c: 'a' as u8, p: 0.27 },
AminoAcid { c: 'c' as u8, p: 0.12 },
AminoAcid { c: 'g' as u8, p: 0.12 },
Expand All @@ -82,7 +82,7 @@ static IUB: [AminoAcid, ..15] = [
AminoAcid { c: 'Y' as u8, p: 0.02 },
];

static HOMO_SAPIENS: [AminoAcid, ..4] = [
static HOMO_SAPIENS: [AminoAcid;4] = [
AminoAcid { c: 'a' as u8, p: 0.3029549426680 },
AminoAcid { c: 'c' as u8, p: 0.1979883004921 },
AminoAcid { c: 'g' as u8, p: 0.1975473066391 },
Expand Down Expand Up @@ -148,8 +148,8 @@ impl<'a, W: Writer> RepeatFasta<'a, W> {
}
}

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

struct RandomFasta<'a, W:'a> {
seed: u32,
lookup: [AminoAcid, ..LOOKUP_SIZE],
lookup: [AminoAcid;LOOKUP_SIZE],
out: &'a mut W,
}

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

for _ in range(0, lines) {
for i in range(0u, LINE_LEN) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/bench/shootout-fasta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn make_fasta<W: Writer, I: Iterator<u8>>(
-> std::io::IoResult<()>
{
try!(wr.write(header.as_bytes()));
let mut line = [0u8, .. LINE_LENGTH + 1];
let mut line = [0u8; LINE_LENGTH + 1];
while n > 0 {
let nb = min(LINE_LENGTH, n);
for i in range(0, nb) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/bench/shootout-k-nucleotide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ use std::string::String;
use std::slice;
use std::sync::{Arc, Future};

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

static OCCURRENCES: [&'static str, ..5] = [
static OCCURRENCES: [&'static str;5] = [
"GGT",
"GGTA",
"GGTATT",
Expand Down
8 changes: 4 additions & 4 deletions src/test/bench/shootout-nbody.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const SOLAR_MASS: f64 = 4.0 * PI * PI;
const YEAR: f64 = 365.24;
const N_BODIES: uint = 5;

static BODIES: [Planet, ..N_BODIES] = [
static BODIES: [Planet;N_BODIES] = [
// Sun
Planet {
x: 0.0, y: 0.0, z: 0.0,
Expand Down Expand Up @@ -102,7 +102,7 @@ struct Planet {

impl Copy for Planet {}

fn advance(bodies: &mut [Planet, ..N_BODIES], dt: f64, steps: int) {
fn advance(bodies: &mut [Planet;N_BODIES], dt: f64, steps: int) {
for _ in range(0, steps) {
let mut b_slice = bodies.as_mut_slice();
loop {
Expand Down Expand Up @@ -135,7 +135,7 @@ fn advance(bodies: &mut [Planet, ..N_BODIES], dt: f64, steps: int) {
}
}

fn energy(bodies: &[Planet, ..N_BODIES]) -> f64 {
fn energy(bodies: &[Planet;N_BODIES]) -> f64 {
let mut e = 0.0;
let mut bodies = bodies.iter();
loop {
Expand All @@ -155,7 +155,7 @@ fn energy(bodies: &[Planet, ..N_BODIES]) -> f64 {
e
}

fn offset_momentum(bodies: &mut [Planet, ..N_BODIES]) {
fn offset_momentum(bodies: &mut [Planet;N_BODIES]) {
let mut px = 0.0;
let mut py = 0.0;
let mut pz = 0.0;
Expand Down
8 changes: 4 additions & 4 deletions src/test/bench/shootout-reverse-complement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ use std::ptr::{copy_memory};
use std::io::{IoResult, EndOfFile};

struct Tables {
table8: [u8, ..1 << 8],
table16: [u16, ..1 << 16]
table8: [u8;1 << 8],
table16: [u16;1 << 16]
}

impl Tables {
fn new() -> Tables {
let mut table8 = [0, ..1 << 8];
let mut table8 = [0;1 << 8];
for (i, v) in table8.iter_mut().enumerate() {
*v = Tables::computed_cpl8(i as u8);
}
let mut table16 = [0, ..1 << 16];
let mut table16 = [0;1 << 16];
for (i, v) in table16.iter_mut().enumerate() {
*v = table8[i & 255] as u16 << 8 |
table8[i >> 8] as u16;
Expand Down
6 changes: 3 additions & 3 deletions src/test/bench/sudoku.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Sudoku {
return Sudoku { grid: g }
}

pub fn from_vec(vec: &[[u8, ..9], ..9]) -> Sudoku {
pub fn from_vec(vec: &[[u8;9];9]) -> Sudoku {
let g = Vec::from_fn(9u, |i| {
Vec::from_fn(9u, |j| { vec[i][j] })
});
Expand Down Expand Up @@ -198,7 +198,7 @@ impl Colors {
}
}

static DEFAULT_SUDOKU: [[u8, ..9], ..9] = [
static DEFAULT_SUDOKU: [[u8;9];9] = [
/* 0 1 2 3 4 5 6 7 8 */
/* 0 */ [0u8, 4u8, 0u8, 6u8, 0u8, 0u8, 0u8, 3u8, 2u8],
/* 1 */ [0u8, 0u8, 8u8, 0u8, 2u8, 0u8, 0u8, 0u8, 0u8],
Expand All @@ -212,7 +212,7 @@ static DEFAULT_SUDOKU: [[u8, ..9], ..9] = [
];

#[cfg(test)]
static DEFAULT_SOLUTION: [[u8, ..9], ..9] = [
static DEFAULT_SOLUTION: [[u8;9];9] = [
/* 0 1 2 3 4 5 6 7 8 */
/* 0 */ [1u8, 4u8, 9u8, 6u8, 7u8, 5u8, 8u8, 3u8, 2u8],
/* 1 */ [5u8, 3u8, 8u8, 1u8, 2u8, 9u8, 7u8, 4u8, 6u8],
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/better-expected.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
// except according to those terms.

fn main() {
let x: [int ..3]; //~ ERROR expected one of `(`, `+`, `,`, `::`, or `]`, found `..`
let x: [int 3]; //~ ERROR expected one of `(`, `+`, `,`, `::`, `;`, or `]`, found `3`
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// Issue #16205.

struct Foo {
a: [Box<int>, ..3],
a: [Box<int>; 3],
}

fn main() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/coercion-slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

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

fn main() {
let _: &[int] = [0i]; //~ERROR: mismatched types: expected `&[int]`, found `[int, ..1]`
let _: &[int] = [0i]; //~ERROR: mismatched types: expected `&[int]`, found `[int; 1]`
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/const-cast-wrong-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

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

fn main() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/dst-bad-coerce1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ trait Bar {}
pub fn main() {
// With a vec of ints.
let f1 = Fat { ptr: [1, 2, 3] };
let f2: &Fat<[int, ..3]> = &f1;
let f2: &Fat<[int; 3]> = &f1;
let f3: &Fat<[uint]> = f2;
//~^ ERROR mismatched types: expected `&Fat<[uint]>`, found `&Fat<[int, ..3]>`
//~^ ERROR mismatched types: expected `&Fat<[uint]>`, found `&Fat<[int; 3]>`

// With a trait.
let f1 = Fat { ptr: Foo };
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/dst-bad-coerce2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Bar for Foo {}
pub fn main() {
// With a vec of ints.
let f1 = Fat { ptr: [1, 2, 3] };
let f2: &Fat<[int, ..3]> = &f1;
let f2: &Fat<[int; 3]> = &f1;
let f3: &mut Fat<[int]> = f2; //~ ERROR mismatched types

// With a trait.
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/dst-bad-coerce3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Bar for Foo {}
fn baz<'a>() {
// With a vec of ints.
let f1 = Fat { ptr: [1, 2, 3] };
let f2: &Fat<[int, ..3]> = &f1; //~ ERROR `f1` does not live long enough
let f2: &Fat<[int; 3]> = &f1; //~ ERROR `f1` does not live long enough
let f3: &'a Fat<[int]> = f2;

// With a trait.
Expand Down
Loading