Skip to content

Commit 4690ab0

Browse files
edwardwalexcrichton
authored andcommitted
Match binding is assignment
In its first pass, namely gather_loans, the borrow checker tracks the initialization sites among other things it does. It does so for let bindings with initializers but not for bindings in match arms, which are effectively also assignments. This patch does that for borrow checker. Closes #12452.
1 parent 6757053 commit 4690ab0

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

src/librustc/middle/borrowck/gather_loans/mod.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,8 @@ fn gather_loans_in_local(this: &mut GatherLoanCtxt,
160160
})
161161
}
162162
Some(init) => {
163-
// Variable declarations with initializers are considered "assigns":
164-
let tcx = this.bccx.tcx;
165-
pat_util::pat_bindings(tcx.def_map, local.pat, |_, id, span, _| {
166-
gather_moves::gather_assignment(this.bccx,
167-
&this.move_data,
168-
id,
169-
span,
170-
@LpVar(id),
171-
id);
172-
});
163+
// Variable declarations with initializers are considered "assigns",
164+
// which is handled by `gather_pat`:
173165
let init_cmt = this.bccx.cat_expr(init);
174166
this.gather_pat(init_cmt, local.pat, None);
175167
}
@@ -811,6 +803,17 @@ impl<'a> GatherLoanCtxt<'a> {
811803
self.bccx.cat_pattern(discr_cmt, root_pat, |cmt, pat| {
812804
match pat.node {
813805
ast::PatIdent(bm, _, _) if self.pat_is_binding(pat) => {
806+
// Each match binding is effectively an assignment.
807+
let tcx = self.bccx.tcx;
808+
pat_util::pat_bindings(tcx.def_map, pat, |_, id, span, _| {
809+
gather_moves::gather_assignment(self.bccx,
810+
&self.move_data,
811+
id,
812+
span,
813+
@LpVar(id),
814+
id);
815+
});
816+
814817
match bm {
815818
ast::BindByRef(mutbl) => {
816819
// ref x or ref x @ p --- creates a ptr which must
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2012-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+
// Test that immutable pattern bindings cannot be reassigned.
12+
13+
enum E {
14+
Foo(int)
15+
}
16+
17+
struct S {
18+
bar: int,
19+
}
20+
21+
pub fn main() {
22+
match 1i {
23+
x => {
24+
x += 1; //~ ERROR re-assignment of immutable variable `x`
25+
}
26+
}
27+
28+
match Foo(1) {
29+
Foo(x) => {
30+
x += 1; //~ ERROR re-assignment of immutable variable `x`
31+
}
32+
}
33+
34+
match S { bar: 1 } {
35+
S { bar: x } => {
36+
x += 1; //~ ERROR re-assignment of immutable variable `x`
37+
}
38+
}
39+
40+
match (1i,) {
41+
(x,) => {
42+
x += 1; //~ ERROR re-assignment of immutable variable `x`
43+
}
44+
}
45+
46+
match [1,2,3] {
47+
[x,_,_] => {
48+
x += 1; //~ ERROR re-assignment of immutable variable `x`
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)