Skip to content
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

Disallow trailing parentheses for nullary enum variants #12935

Merged
merged 1 commit into from
Mar 17, 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/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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please add a description about what this test (and the other one) are supposed to test?

// 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);
}