Skip to content

Commit b8516de

Browse files
committed
auto merge of #10833 : sfackler/rust/mut-pat, r=brson
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
2 parents b8b16ae + 8240faf commit b8516de

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
@@ -2807,18 +2807,33 @@ impl Parser {
28072807
}
28082808

28092809
let lo1 = self.last_span.lo;
2810+
let bind_type = if self.eat_keyword(keywords::Mut) {
2811+
BindByValue(MutMutable)
2812+
} else if self.eat_keyword(keywords::Ref) {
2813+
BindByRef(self.parse_mutability())
2814+
} else {
2815+
BindByValue(MutImmutable)
2816+
};
2817+
28102818
let fieldname = self.parse_ident();
28112819
let hi1 = self.last_span.lo;
28122820
let fieldpath = ast_util::ident_to_path(mk_sp(lo1, hi1),
28132821
fieldname);
28142822
let subpat;
28152823
if *self.token == token::COLON {
2824+
match bind_type {
2825+
BindByRef(..) | BindByValue(MutMutable) =>
2826+
self.fatal(format!("unexpected `{}`",
2827+
self.this_token_to_str())),
2828+
_ => {}
2829+
}
2830+
28162831
self.bump();
28172832
subpat = self.parse_pat();
28182833
} else {
28192834
subpat = @ast::Pat {
28202835
id: ast::DUMMY_NODE_ID,
2821-
node: PatIdent(BindByValue(MutImmutable), fieldpath, None),
2836+
node: PatIdent(bind_type, fieldpath, None),
28222837
span: *self.last_span
28232838
};
28242839
}

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)