Skip to content

Commit c7a5110

Browse files
edwardwalexcrichton
authored andcommitted
---
yaml --- r: 149539 b: refs/heads/try2 c: 4690ab0 h: refs/heads/master i: 149537: 6a17f9d 149535: 17cf438 v: v3
1 parent ad8430e commit c7a5110

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 6757053cffb585249105fbd76f83a2fe7501219b
8+
refs/heads/try2: 4690ab0ea84d4381ddca724a51e4a36760149fde
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/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)