Skip to content

Commit

Permalink
Allow trailing commas in array patterns and attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ftxqxd committed Nov 30, 2014
1 parent 8d8f41b commit f5715f7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/libsyntax/parse/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl<'a> ParserAttr for Parser<'a> {
fn parse_meta_seq(&mut self) -> Vec<P<ast::MetaItem>> {
self.parse_seq(&token::OpenDelim(token::Paren),
&token::CloseDelim(token::Paren),
seq_sep_trailing_disallowed(token::Comma),
seq_sep_trailing_allowed(token::Comma),
|p| p.parse_meta_item()).node
}

Expand Down
7 changes: 1 addition & 6 deletions src/libsyntax/parse/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,13 @@ pub struct SeqSep {
pub trailing_sep_allowed: bool
}

pub fn seq_sep_trailing_disallowed(t: token::Token) -> SeqSep {
SeqSep {
sep: Some(t),
trailing_sep_allowed: false,
}
}
pub fn seq_sep_trailing_allowed(t: token::Token) -> SeqSep {
SeqSep {
sep: Some(t),
trailing_sep_allowed: true,
}
}

pub fn seq_sep_none() -> SeqSep {
SeqSep {
sep: None,
Expand Down
5 changes: 5 additions & 0 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3129,6 +3129,11 @@ impl<'a> Parser<'a> {
first = false;
} else {
self.expect(&token::Comma);

if self.token == token::CloseDelim(token::Bracket)
&& (before_slice || after.len() != 0) {
break
}
}

if before_slice {
Expand Down
13 changes: 13 additions & 0 deletions src/test/compile-fail/trailing-comma-array-repeat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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 [_, ..,] = [(), ()]; //~ ERROR unexpected token: `]`
}
6 changes: 6 additions & 0 deletions src/test/run-pass/trailing-comma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(advanced_slice_patterns,)]

fn f<T,>(_: T,) {}

struct Foo<T,>;
Expand All @@ -24,9 +26,13 @@ enum Baz {
Qux(int,),
}

#[allow(unused,)]
pub fn main() {
f::<int,>(0i,);
let (_, _,) = (1i, 1i,);
let [_, _,] = [1i, 1,];
let [_, _, .., _,] = [1i, 1, 1, 1,];
let [_, _, _.., _,] = [1i, 1, 1, 1,];

let x: Foo<int,> = Foo::<int,>;

Expand Down

4 comments on commit f5715f7

@bors
Copy link
Contributor

@bors bors commented on f5715f7 Nov 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at ftxqxd@f5715f7

@bors
Copy link
Contributor

@bors bors commented on f5715f7 Nov 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging P1start/rust/more-trailing-commas = f5715f7 into auto

@bors
Copy link
Contributor

@bors bors commented on f5715f7 Nov 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1start/rust/more-trailing-commas = f5715f7 merged ok, testing candidate = cce4d75e

Please sign in to comment.