From 695114ea2c1ae147dcf1c3ac690a75ecf928b8b2 Mon Sep 17 00:00:00 2001 From: Laurent Bonnans Date: Sun, 16 Mar 2014 16:07:39 +0100 Subject: [PATCH] rustc: disallow trailing parentheses for nullary enum variants Fixes #12560 --- src/libsyntax/ext/mtwt.rs | 2 +- src/libsyntax/parse/parser.rs | 21 +++++++++++++-- .../compile-fail/enums-pats-not-idents.rs | 4 +-- src/test/compile-fail/issue-12560-1.rs | 23 ++++++++++++++++ src/test/compile-fail/issue-12560-2.rs | 27 +++++++++++++++++++ src/test/compile-fail/issue-5927.rs | 4 +-- 6 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 src/test/compile-fail/issue-12560-1.rs create mode 100644 src/test/compile-fail/issue-12560-2.rs diff --git a/src/libsyntax/ext/mtwt.rs b/src/libsyntax/ext/mtwt.rs index b7fad22a7ad94..909f3eaf361e0 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 27c86956499ce..e1a02d5240fb2 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( + &mut self, + bra: &token::Token, + ket: &token::Token, + sep: SeqSep, + f: |&mut Parser| -> T) + -> Vec { + 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( @@ -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 9eb98341112f2..faf672415bdfa 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 0000000000000..ea2043e6703c2 --- /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 or the MIT license +// , 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 0000000000000..13829d73aadaa --- /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 or the MIT license +// , 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 a1b4ee7aa3445..0359248b36a49 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); }