Skip to content

Commit 7022ede

Browse files
committed
make unique pointers inherit mutability from owner
1 parent 3bcd973 commit 7022ede

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

src/rustc/middle/borrowck/categorization.rs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ fn deref_kind(tcx: ty::ctxt, t: ty::t) -> deref_kind {
9797

9898
impl public_methods for borrowck_ctxt {
9999
fn cat_borrow_of_expr(expr: @ast::expr) -> cmt {
100-
// a borrowed expression must be either an @, ~, or a vec/@, vec/~
100+
// a borrowed expression must be either an @, ~, or a @vec, ~vec
101101
let expr_ty = ty::expr_ty(self.tcx, expr);
102102
alt ty::get(expr_ty).struct {
103103
ty::ty_evec(*) | ty::ty_estr(*) {
@@ -109,6 +109,12 @@ impl public_methods for borrowck_ctxt {
109109
self.cat_deref(expr, cmt, 0u, true).get()
110110
}
111111

112+
/*
113+
ty::ty_fn({proto, _}) {
114+
self.cat_call(expr, expr, proto)
115+
}
116+
*/
117+
112118
_ {
113119
self.tcx.sess.span_bug(
114120
expr.span,
@@ -341,9 +347,20 @@ impl public_methods for borrowck_ctxt {
341347
}
342348
};
343349

350+
// for unique ptrs, we inherit mutability from the
351+
// owning reference.
352+
let m = alt ptr {
353+
uniq_ptr => {
354+
self.inherited_mutability(base_cmt.mutbl, mt.mutbl)
355+
}
356+
gc_ptr | region_ptr | unsafe_ptr => {
357+
mt.mutbl
358+
}
359+
};
360+
344361
@{id:node.id(), span:node.span(),
345362
cat:cat_deref(base_cmt, derefs, ptr), lp:lp,
346-
mutbl:mt.mutbl, ty:mt.ty}
363+
mutbl:m, ty:mt.ty}
347364
}
348365

349366
deref_comp(comp) {
@@ -379,27 +396,38 @@ impl public_methods for borrowck_ctxt {
379396
_ => {none}
380397
};
381398

382-
// (b) the deref is explicit in the resulting cmt
399+
// (b) for unique ptrs, we inherit mutability from the
400+
// owning reference.
401+
let m = alt ptr {
402+
uniq_ptr => {
403+
self.inherited_mutability(base_cmt.mutbl, mt.mutbl)
404+
}
405+
gc_ptr | region_ptr | unsafe_ptr => {
406+
mt.mutbl
407+
}
408+
};
409+
410+
// (c) the deref is explicit in the resulting cmt
383411
let deref_cmt = @{id:expr.id, span:expr.span,
384412
cat:cat_deref(base_cmt, 0u, ptr), lp:deref_lp,
385-
mutbl:m_imm, ty:mt.ty};
413+
mutbl:m, ty:mt.ty};
386414

387-
comp(expr, deref_cmt, base_cmt.ty, mt)
415+
comp(expr, deref_cmt, base_cmt.ty, m, mt.ty)
388416
}
389417

390418
deref_comp(_) {
391419
// fixed-length vectors have no deref
392-
comp(expr, base_cmt, base_cmt.ty, mt)
420+
comp(expr, base_cmt, base_cmt.ty, mt.mutbl, mt.ty)
393421
}
394422
};
395423

396424
fn comp(expr: @ast::expr, of_cmt: cmt,
397-
vect: ty::t, mt: ty::mt) -> cmt {
398-
let comp = comp_index(vect, mt.mutbl);
425+
vect: ty::t, mutbl: ast::mutability, ty: ty::t) -> cmt {
426+
let comp = comp_index(vect, mutbl);
399427
let index_lp = of_cmt.lp.map(|lp| @lp_comp(lp, comp) );
400428
@{id:expr.id, span:expr.span,
401429
cat:cat_comp(of_cmt, comp), lp:index_lp,
402-
mutbl:mt.mutbl, ty:mt.ty}
430+
mutbl:mutbl, ty:ty}
403431
}
404432
}
405433

0 commit comments

Comments
 (0)