Skip to content

Commit 4d9d889

Browse files
committed
Don't allow assignment to mutable-wha?
1 parent 4543333 commit 4d9d889

File tree

6 files changed

+73
-9
lines changed

6 files changed

+73
-9
lines changed

src/comp/middle/mut.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ fn expr_root(tcx: ty::ctxt, ex: @expr, autoderef: bool) ->
4949
alt copy ex.node {
5050
expr_field(base, ident) {
5151
let auto_unbox = maybe_auto_unbox(tcx, ty::expr_ty(tcx, base));
52-
let mut = false;
52+
let is_mut = false;
5353
alt ty::struct(tcx, auto_unbox.t) {
5454
ty::ty_rec(fields) {
5555
for fld: ty::field in fields {
5656
if str::eq(ident, fld.ident) {
57-
mut = fld.mt.mut != imm;
57+
is_mut = fld.mt.mut == mut;
5858
break;
5959
}
6060
}
6161
}
6262
ty::ty_obj(_) { }
6363
}
64-
ds += [@{mut: mut, kind: field, outer_t: auto_unbox.t}];
64+
ds += [@{mut: is_mut, kind: field, outer_t: auto_unbox.t}];
6565
ds += auto_unbox.ds;
6666
ex = base;
6767
}
@@ -70,7 +70,7 @@ fn expr_root(tcx: ty::ctxt, ex: @expr, autoderef: bool) ->
7070
alt ty::struct(tcx, auto_unbox.t) {
7171
ty::ty_vec(mt) {
7272
ds +=
73-
[@{mut: mt.mut != imm,
73+
[@{mut: mt.mut == mut,
7474
kind: index,
7575
outer_t: auto_unbox.t}];
7676
}
@@ -84,15 +84,15 @@ fn expr_root(tcx: ty::ctxt, ex: @expr, autoderef: bool) ->
8484
expr_unary(op, base) {
8585
if op == deref {
8686
let base_t = ty::expr_ty(tcx, base);
87-
let mut = false;
87+
let is_mut = false;
8888
alt ty::struct(tcx, base_t) {
89-
ty::ty_box(mt) { mut = mt.mut != imm; }
90-
ty::ty_uniq(mt) { mut = mt.mut != imm; }
89+
ty::ty_box(mt) { is_mut = mt.mut == mut; }
90+
ty::ty_uniq(mt) { is_mut = mt.mut == mut; }
9191
ty::ty_res(_, _, _) { }
9292
ty::ty_tag(_, _) { }
93-
ty::ty_ptr(mt) { mut = mt.mut != imm; }
93+
ty::ty_ptr(mt) { is_mut = mt.mut == mut; }
9494
}
95-
ds += [@{mut: mut, kind: unbox, outer_t: base_t}];
95+
ds += [@{mut: is_mut, kind: unbox, outer_t: base_t}];
9696
ex = base;
9797
} else { break; }
9898
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// error-pattern: assigning to immutable box
2+
3+
fn main() {
4+
fn f(&&v: @mutable? int) {
5+
// This shouldn't be possible
6+
*v = 1
7+
}
8+
9+
let v = @0;
10+
11+
f(v);
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// error-pattern: assigning to immutable field
2+
3+
fn main() {
4+
fn f(&&v: {mutable? field: int}) {
5+
// This shouldn't be possible
6+
v.field = 1
7+
}
8+
9+
let v = {field: 0};
10+
11+
f(v);
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// error-pattern: assigning to immutable box
2+
3+
use std;
4+
5+
fn main() {
6+
unsafe fn f(&&v: *mutable? int) {
7+
// This shouldn't be possible
8+
*v = 1
9+
}
10+
11+
unsafe {
12+
let a = 0;
13+
let v = std::ptr::addr_of(a);
14+
f(v);
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// error-pattern: assigning to immutable box
2+
3+
fn main() {
4+
fn f(&&v: ~mutable? int) {
5+
// This shouldn't be possible
6+
*v = 1
7+
}
8+
9+
let v = ~0;
10+
11+
f(v);
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// error-pattern: assigning to immutable vec content
2+
3+
fn main() {
4+
fn f(&&v: [mutable? int]) {
5+
// This shouldn't be possible
6+
v[0] = 1
7+
}
8+
9+
let v = [0];
10+
11+
f(v);
12+
}

0 commit comments

Comments
 (0)