Skip to content

Commit af93684

Browse files
committed
auto merge of #12935 : lbonn/rust/nullenum, r=alexcrichton
Fix for #12560
2 parents 0a181a8 + 695114e commit af93684

File tree

6 files changed

+74
-7
lines changed

6 files changed

+74
-7
lines changed

src/libsyntax/ext/mtwt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ fn resolve_internal(id: Ident,
198198
resolvedthis
199199
}
200200
}
201-
IllegalCtxt() => fail!("expected resolvable context, got IllegalCtxt")
201+
IllegalCtxt => fail!("expected resolvable context, got IllegalCtxt")
202202
}
203203
};
204204
resolve_table.insert(key, resolved);

src/libsyntax/parse/parser.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,23 @@ impl<'a> Parser<'a> {
713713
result
714714
}
715715

716+
// parse a sequence parameter of enum variant. For consistency purposes,
717+
// these should not be empty.
718+
pub fn parse_enum_variant_seq<T>(
719+
&mut self,
720+
bra: &token::Token,
721+
ket: &token::Token,
722+
sep: SeqSep,
723+
f: |&mut Parser| -> T)
724+
-> Vec<T> {
725+
let result = self.parse_unspanned_seq(bra, ket, sep, f);
726+
if result.is_empty() {
727+
self.span_err(self.last_span,
728+
"nullary enum variants are written with no trailing `( )`");
729+
}
730+
result
731+
}
732+
716733
// NB: Do not use this function unless you actually plan to place the
717734
// spanned list in the AST.
718735
pub fn parse_seq<T>(
@@ -3013,7 +3030,7 @@ impl<'a> Parser<'a> {
30133030
self.expect(&token::RPAREN);
30143031
pat = PatEnum(enum_path, None);
30153032
} else {
3016-
args = self.parse_unspanned_seq(
3033+
args = self.parse_enum_variant_seq(
30173034
&token::LPAREN,
30183035
&token::RPAREN,
30193036
seq_sep_trailing_disallowed(token::COMMA),
@@ -4431,7 +4448,7 @@ impl<'a> Parser<'a> {
44314448
kind = StructVariantKind(self.parse_struct_def());
44324449
} else if self.token == token::LPAREN {
44334450
all_nullary = false;
4434-
let arg_tys = self.parse_unspanned_seq(
4451+
let arg_tys = self.parse_enum_variant_seq(
44354452
&token::LPAREN,
44364453
&token::RPAREN,
44374454
seq_sep_trailing_disallowed(token::COMMA),

src/test/compile-fail/enums-pats-not-idents.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -12,5 +12,5 @@
1212

1313
fn main() {
1414
// a bug in the parser is allowing this:
15-
let a() = 13;
15+
let a(1) = 13;
1616
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// For style and consistency reasons, non-parametrized enum variants must
12+
// be used simply as `ident` instead of `ident ()`.
13+
// This test-case covers enum declaration.
14+
15+
enum Foo {
16+
Bar(), //~ ERROR nullary enum variants are written with no trailing `( )`
17+
Baz(), //~ ERROR nullary enum variants are written with no trailing `( )`
18+
Bazar
19+
}
20+
21+
fn main() {
22+
println!("{}", match Bar { Bar => 1, Baz => 2, Bazar => 3 })
23+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// For style and consistency reasons, non-parametrized enum variants must
12+
// be used simply as `ident` instead of `ident ()`.
13+
// This test-case covers enum matching.
14+
15+
enum Foo {
16+
Bar,
17+
Baz,
18+
Bazar
19+
}
20+
21+
fn main() {
22+
println!("{}", match Bar {
23+
Bar() => 1, //~ ERROR nullary enum variants are written with no trailing `( )`
24+
Baz() => 2, //~ ERROR nullary enum variants are written with no trailing `( )`
25+
Bazar => 3
26+
})
27+
}

src/test/compile-fail/issue-5927.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -14,7 +14,7 @@
1414

1515
fn main() {
1616
let z = match 3 {
17-
x() => x
17+
x(1) => x(1)
1818
};
1919
assert_eq!(z,3);
2020
}

0 commit comments

Comments
 (0)