Skip to content

Commit

Permalink
rustc: disallow trailing parentheses for nullary enum variants
Browse files Browse the repository at this point in the history
Fixes #12560
  • Loading branch information
lbonn committed Mar 17, 2014
1 parent e4c91e6 commit 695114e
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/libsyntax/ext/mtwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn resolve_internal(id: Ident,
resolvedthis
}
}
IllegalCtxt() => fail!("expected resolvable context, got IllegalCtxt")
IllegalCtxt => fail!("expected resolvable context, got IllegalCtxt")
}
};
resolve_table.insert(key, resolved);
Expand Down
21 changes: 19 additions & 2 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,23 @@ impl<'a> Parser<'a> {
result
}

// parse a sequence parameter of enum variant. For consistency purposes,
// these should not be empty.
pub fn parse_enum_variant_seq<T>(
&mut self,
bra: &token::Token,
ket: &token::Token,
sep: SeqSep,
f: |&mut Parser| -> T)
-> Vec<T> {
let result = self.parse_unspanned_seq(bra, ket, sep, f);
if result.is_empty() {
self.span_err(self.last_span,
"nullary enum variants are written with no trailing `( )`");
}
result
}

// NB: Do not use this function unless you actually plan to place the
// spanned list in the AST.
pub fn parse_seq<T>(
Expand Down Expand Up @@ -3013,7 +3030,7 @@ impl<'a> Parser<'a> {
self.expect(&token::RPAREN);
pat = PatEnum(enum_path, None);
} else {
args = self.parse_unspanned_seq(
args = self.parse_enum_variant_seq(
&token::LPAREN,
&token::RPAREN,
seq_sep_trailing_disallowed(token::COMMA),
Expand Down Expand Up @@ -4431,7 +4448,7 @@ impl<'a> Parser<'a> {
kind = StructVariantKind(self.parse_struct_def());
} else if self.token == token::LPAREN {
all_nullary = false;
let arg_tys = self.parse_unspanned_seq(
let arg_tys = self.parse_enum_variant_seq(
&token::LPAREN,
&token::RPAREN,
seq_sep_trailing_disallowed(token::COMMA),
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/enums-pats-not-idents.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -12,5 +12,5 @@

fn main() {
// a bug in the parser is allowing this:
let a() = 13;
let a(1) = 13;
}
23 changes: 23 additions & 0 deletions src/test/compile-fail/issue-12560-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.

// For style and consistency reasons, non-parametrized enum variants must
// be used simply as `ident` instead of `ident ()`.
// This test-case covers enum declaration.

enum Foo {
Bar(), //~ ERROR nullary enum variants are written with no trailing `( )`
Baz(), //~ ERROR nullary enum variants are written with no trailing `( )`
Bazar
}

fn main() {
println!("{}", match Bar { Bar => 1, Baz => 2, Bazar => 3 })
}
27 changes: 27 additions & 0 deletions src/test/compile-fail/issue-12560-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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.

// For style and consistency reasons, non-parametrized enum variants must
// be used simply as `ident` instead of `ident ()`.
// This test-case covers enum matching.

enum Foo {
Bar,
Baz,
Bazar
}

fn main() {
println!("{}", match Bar {
Bar() => 1, //~ ERROR nullary enum variants are written with no trailing `( )`
Baz() => 2, //~ ERROR nullary enum variants are written with no trailing `( )`
Bazar => 3
})
}
4 changes: 2 additions & 2 deletions src/test/compile-fail/issue-5927.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -14,7 +14,7 @@

fn main() {
let z = match 3 {
x() => x
x(1) => x(1)
};
assert_eq!(z,3);
}

5 comments on commit 695114e

@bors
Copy link
Contributor

@bors bors commented on 695114e Mar 17, 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 lbonn@695114e

@bors
Copy link
Contributor

@bors bors commented on 695114e Mar 17, 2014

Choose a reason for hiding this comment

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

merging lbonn/rust/nullenum = 695114e into auto

@bors
Copy link
Contributor

@bors bors commented on 695114e Mar 17, 2014

Choose a reason for hiding this comment

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

lbonn/rust/nullenum = 695114e merged ok, testing candidate = af93684

@bors
Copy link
Contributor

@bors bors commented on 695114e Mar 17, 2014

Choose a reason for hiding this comment

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

fast-forwarding master to auto = af93684

Please sign in to comment.