diff --git a/src/libsyntax/ext/mtwt.rs b/src/libsyntax/ext/mtwt.rs index b7fad22a7ad..909f3eaf361 100644 --- a/src/libsyntax/ext/mtwt.rs +++ b/src/libsyntax/ext/mtwt.rs @@ -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); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 27c86956499..e1a02d5240f 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -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>( @@ -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), @@ -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), diff --git a/src/test/compile-fail/enums-pats-not-idents.rs b/src/test/compile-fail/enums-pats-not-idents.rs index 9eb98341112..faf672415bd 100644 --- a/src/test/compile-fail/enums-pats-not-idents.rs +++ b/src/test/compile-fail/enums-pats-not-idents.rs @@ -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. // @@ -12,5 +12,5 @@ fn main() { // a bug in the parser is allowing this: - let a() = 13; + let a(1) = 13; } diff --git a/src/test/compile-fail/issue-12560-1.rs b/src/test/compile-fail/issue-12560-1.rs new file mode 100644 index 00000000000..ea2043e6703 --- /dev/null +++ b/src/test/compile-fail/issue-12560-1.rs @@ -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 }) +} diff --git a/src/test/compile-fail/issue-12560-2.rs b/src/test/compile-fail/issue-12560-2.rs new file mode 100644 index 00000000000..13829d73aad --- /dev/null +++ b/src/test/compile-fail/issue-12560-2.rs @@ -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 + }) +} diff --git a/src/test/compile-fail/issue-5927.rs b/src/test/compile-fail/issue-5927.rs index a1b4ee7aa34..0359248b36a 100644 --- a/src/test/compile-fail/issue-5927.rs +++ b/src/test/compile-fail/issue-5927.rs @@ -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. // @@ -14,7 +14,7 @@ fn main() { let z = match 3 { - x() => x + x(1) => x(1) }; assert_eq!(z,3); }