Skip to content

Commit 8240faf

Browse files
committed
Allow ref and mut modifiers for short form field patterns
Previously, if you wanted to bind a field mutably or by ref, you had to do something like Foo { x: ref mut x }. You can now just do Foo { ref mut x }. Closes #6137
1 parent 9fc4806 commit 8240faf

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

Diff for: src/libsyntax/parse/parser.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -2814,18 +2814,33 @@ impl Parser {
28142814
}
28152815

28162816
let lo1 = self.last_span.lo;
2817+
let bind_type = if self.eat_keyword(keywords::Mut) {
2818+
BindByValue(MutMutable)
2819+
} else if self.eat_keyword(keywords::Ref) {
2820+
BindByRef(self.parse_mutability())
2821+
} else {
2822+
BindByValue(MutImmutable)
2823+
};
2824+
28172825
let fieldname = self.parse_ident();
28182826
let hi1 = self.last_span.lo;
28192827
let fieldpath = ast_util::ident_to_path(mk_sp(lo1, hi1),
28202828
fieldname);
28212829
let subpat;
28222830
if *self.token == token::COLON {
2831+
match bind_type {
2832+
BindByRef(..) | BindByValue(MutMutable) =>
2833+
self.fatal(format!("unexpected `{}`",
2834+
self.this_token_to_str())),
2835+
_ => {}
2836+
}
2837+
28232838
self.bump();
28242839
subpat = self.parse_pat();
28252840
} else {
28262841
subpat = @ast::Pat {
28272842
id: ast::DUMMY_NODE_ID,
2828-
node: PatIdent(BindByValue(MutImmutable), fieldpath, None),
2843+
node: PatIdent(bind_type, fieldpath, None),
28292844
span: *self.last_span
28302845
};
28312846
}

Diff for: src/test/compile-fail/bind-struct-early-modifiers.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2013 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+
fn main() {
12+
struct Foo { x: int }
13+
match Foo { x: 10 } {
14+
Foo { ref x: ref x } => {}, //~ ERROR unexpected `:`
15+
_ => {}
16+
}
17+
}

Diff for: src/test/run-pass/bind-field-short-with-modifiers.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2013 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+
pub fn main() {
12+
struct Foo { x: int, y: int }
13+
let mut f = Foo { x: 10, y: 0 };
14+
match f {
15+
Foo { ref mut x, .. } => *x = 11,
16+
}
17+
match f {
18+
Foo { ref x, ref y } => {
19+
assert_eq!(f.x, 11);
20+
assert_eq!(f.y, 0);
21+
}
22+
}
23+
match f {
24+
Foo { mut x, y: ref mut y } => {
25+
x = 12;
26+
*y = 1;
27+
}
28+
}
29+
assert_eq!(f.x, 11);
30+
assert_eq!(f.y, 1);
31+
}

0 commit comments

Comments
 (0)