Skip to content

Commit 35e6c02

Browse files
committed
Use '..' as multi-field wildcard in enums and structs.
1 parent 3d569df commit 35e6c02

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

src/libsyntax/parse/obsolete.rs

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ pub enum ObsoleteSyntax {
3939
ObsoleteConstPointer,
4040
ObsoleteEmptyImpl,
4141
ObsoleteLoopAsContinue,
42+
ObsoleteEnumWildcard,
43+
ObsoleteStructWildcard
4244
}
4345

4446
impl to_bytes::IterBytes for ObsoleteSyntax {
@@ -113,6 +115,14 @@ impl ParserObsoleteMethods for Parser {
113115
"`loop` is now only used for loops and `continue` is used for \
114116
skipping iterations"
115117
),
118+
ObsoleteEnumWildcard => (
119+
"enum wildcard",
120+
"use `..` instead of `*` for matching all enum fields"
121+
),
122+
ObsoleteStructWildcard => (
123+
"struct wildcard",
124+
"use `..` instead of `_` for matching trailing struct fields"
125+
),
116126
};
117127

118128
self.report(sp, kind, kind_str, desc);

src/libsyntax/parse/parser.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -2755,7 +2755,12 @@ impl Parser {
27552755
if first { first = false; }
27562756
else { self.expect(&token::COMMA); }
27572757

2758+
etc = *self.token == token::UNDERSCORE || *self.token == token::DOTDOT;
27582759
if *self.token == token::UNDERSCORE {
2760+
// FIXME #5830 activate after snapshot
2761+
// self.obsolete(*self.span, ObsoleteStructWildcard);
2762+
}
2763+
if etc {
27592764
self.bump();
27602765
if *self.token != token::RBRACE {
27612766
self.fatal(
@@ -3016,9 +3021,19 @@ impl Parser {
30163021
_ => false,
30173022
}
30183023
};
3019-
if is_star {
3024+
let is_dotdot = do self.look_ahead(1) |t| {
3025+
match *t {
3026+
token::DOTDOT => true,
3027+
_ => false,
3028+
}
3029+
};
3030+
if is_star | is_dotdot {
30203031
// This is a "top constructor only" pat
30213032
self.bump();
3033+
if is_star {
3034+
// FIXME #5830 activate after snapshot
3035+
// self.obsolete(*self.span, ObsoleteEnumWildcard);
3036+
}
30223037
self.bump();
30233038
self.expect(&token::RPAREN);
30243039
pat = PatEnum(enum_path, None);
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2012 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+
struct Foo(int, int, int, int);
12+
struct Bar{a: int, b: int, c: int, d: int}
13+
14+
pub fn main() {
15+
let Foo(..) = Foo(5, 5, 5, 5);
16+
let Foo(*) = Foo(5, 5, 5, 5);
17+
let Bar{..} = Bar{a: 5, b: 5, c: 5, d: 5};
18+
let Bar{_} = Bar{a: 5, b: 5, c: 5, d: 5};
19+
//let (..) = (5, 5, 5, 5);
20+
//let Foo(a, b, ..) = Foo(5, 5, 5, 5);
21+
//let Foo(.., d) = Foo(5, 5, 5, 5);
22+
//let (a, b, ..) = (5, 5, 5, 5);
23+
//let (.., c, d) = (5, 5, 5, 5);
24+
let Bar{b: b, ..} = Bar{a: 5, b: 5, c: 5, d: 5};
25+
let Bar{b: b, _} = Bar{a: 5, b: 5, c: 5, d: 5};
26+
/*match [5, 5, 5, 5] {
27+
[a, ..] => { }
28+
}*/
29+
/*match [5, 5, 5, 5] {
30+
[.., b] => { }
31+
}*/
32+
/*match [5, 5, 5, 5] {
33+
[a, .., b] => { }
34+
}*/
35+
match [5, 5, 5] {
36+
[a, .._] => { }
37+
}
38+
match [5, 5, 5] {
39+
[.._, a] => { }
40+
}
41+
match [5, 5, 5] {
42+
[a, .._, b] => { }
43+
}
44+
}

0 commit comments

Comments
 (0)