auto merge of #12935 : lbonn/rust/nullenum, r=alexcrichton

Fix for #12560
This commit is contained in:
bors 2014-03-17 11:57:08 -07:00
commit af9368452d
6 changed files with 74 additions and 7 deletions

View File

@ -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);

View File

@ -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),

View File

@ -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;
}

View File

@ -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 })
}

View File

@ -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
})
}

View File

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