Skip to content

Commit 66b8ad5

Browse files
committed
borrowck: Integrate AutoBorrowObj into borrowck / mem_categorization
Also cleanup the treatment of mutability in mem_categorization, which still included the concept of interior mutability. At some point, we should refactor the types to exclude the possibility of interior mutability rather than just ignoring the mutability value in those cases.
1 parent 1bceb98 commit 66b8ad5

File tree

6 files changed

+65
-52
lines changed

6 files changed

+65
-52
lines changed

src/librustc/middle/borrowck/check_loans.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ impl<'self> CheckLoanCtxt<'self> {
362362
}
363363

364364
mc::cat_discr(b, _) |
365-
mc::cat_deref(b, _, mc::uniq_ptr(*)) => {
365+
mc::cat_deref(b, _, mc::uniq_ptr) => {
366366
assert_eq!(cmt.mutbl, mc::McInherited);
367367
cmt = b;
368368
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ fn check_is_legal_to_move_from(bccx: @BorrowckCtxt,
173173
}
174174
}
175175

176-
mc::cat_deref(b, _, mc::uniq_ptr(*)) |
176+
mc::cat_deref(b, _, mc::uniq_ptr) |
177177
mc::cat_discr(b, _) => {
178178
check_is_legal_to_move_from(bccx, cmt0, b)
179179
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl GuaranteeLifetimeContext {
7474
mc::cat_arg(*) | // L-Local
7575
mc::cat_self(*) | // L-Local
7676
mc::cat_deref(_, _, mc::region_ptr(*)) | // L-Deref-Borrowed
77-
mc::cat_deref(_, _, mc::unsafe_ptr) => {
77+
mc::cat_deref(_, _, mc::unsafe_ptr(*)) => {
7878
let scope = self.scope(cmt);
7979
self.check_scope(scope)
8080
}
@@ -108,7 +108,7 @@ impl GuaranteeLifetimeContext {
108108
}
109109

110110
mc::cat_downcast(base) |
111-
mc::cat_deref(base, _, mc::uniq_ptr(*)) | // L-Deref-Send
111+
mc::cat_deref(base, _, mc::uniq_ptr) | // L-Deref-Send
112112
mc::cat_interior(base, _) => { // L-Field
113113
self.check(base, discr_scope)
114114
}
@@ -347,7 +347,7 @@ impl GuaranteeLifetimeContext {
347347
r
348348
}
349349
mc::cat_downcast(cmt) |
350-
mc::cat_deref(cmt, _, mc::uniq_ptr(*)) |
350+
mc::cat_deref(cmt, _, mc::uniq_ptr) |
351351
mc::cat_deref(cmt, _, mc::gc_ptr(*)) |
352352
mc::cat_interior(cmt, _) |
353353
mc::cat_stack_upvar(cmt) |

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,21 @@ impl GatherLoanCtxt {
352352
r)
353353
}
354354
ty::AutoBorrowFn(r) => {
355-
let cmt_deref = mcx.cat_deref_fn(expr, cmt, 0);
355+
let cmt_deref = mcx.cat_deref_fn_or_obj(expr, cmt, 0);
356356
self.guarantee_valid(expr.id,
357357
expr.span,
358358
cmt_deref,
359359
m_imm,
360360
r)
361361
}
362+
ty::AutoBorrowObj(r, m) => {
363+
let cmt_deref = mcx.cat_deref_fn_or_obj(expr, cmt, 0);
364+
self.guarantee_valid(expr.id,
365+
expr.span,
366+
cmt_deref,
367+
m,
368+
r)
369+
}
362370
ty::AutoUnsafe(_) => {}
363371
}
364372
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl RestrictionsContext {
101101
self.extend(result, cmt.mutbl, LpInterior(i), restrictions)
102102
}
103103

104-
mc::cat_deref(cmt_base, _, mc::uniq_ptr(*)) => {
104+
mc::cat_deref(cmt_base, _, mc::uniq_ptr) => {
105105
// R-Deref-Send-Pointer
106106
//
107107
// When we borrow the interior of an owned pointer, we
@@ -194,7 +194,7 @@ impl RestrictionsContext {
194194
}
195195
}
196196

197-
mc::cat_deref(_, _, mc::unsafe_ptr) => {
197+
mc::cat_deref(_, _, mc::unsafe_ptr(*)) => {
198198
// We are very trusting when working with unsafe pointers.
199199
Safe
200200
}

src/librustc/middle/mem_categorization.rs

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ pub struct CopiedUpvar {
8484
// different kinds of pointers:
8585
#[deriving(Eq)]
8686
pub enum ptr_kind {
87-
uniq_ptr(ast::mutability),
87+
uniq_ptr,
8888
gc_ptr(ast::mutability),
8989
region_ptr(ast::mutability, ty::Region),
90-
unsafe_ptr
90+
unsafe_ptr(ast::mutability)
9191
}
9292

9393
// We use the term "interior" to mean "something reachable from the
@@ -156,39 +156,45 @@ pub enum deref_kind {
156156
// pointer adjustment).
157157
pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
158158
match ty::get(t).sty {
159-
ty::ty_uniq(mt) => {
160-
Some(deref_ptr(uniq_ptr(mt.mutbl)))
161-
}
162-
159+
ty::ty_uniq(_) |
160+
ty::ty_trait(_, _, ty::UniqTraitStore, _, _) |
163161
ty::ty_evec(_, ty::vstore_uniq) |
164162
ty::ty_estr(ty::vstore_uniq) |
165163
ty::ty_closure(ty::ClosureTy {sigil: ast::OwnedSigil, _}) => {
166-
Some(deref_ptr(uniq_ptr(m_imm)))
164+
Some(deref_ptr(uniq_ptr))
167165
}
168166

169167
ty::ty_rptr(r, mt) |
170168
ty::ty_evec(mt, ty::vstore_slice(r)) => {
171169
Some(deref_ptr(region_ptr(mt.mutbl, r)))
172170
}
173171

172+
ty::ty_trait(_, _, ty::RegionTraitStore(r), m, _) => {
173+
Some(deref_ptr(region_ptr(m, r)))
174+
}
175+
174176
ty::ty_estr(ty::vstore_slice(r)) |
175177
ty::ty_closure(ty::ClosureTy {sigil: ast::BorrowedSigil,
176178
region: r, _}) => {
177179
Some(deref_ptr(region_ptr(ast::m_imm, r)))
178180
}
179181

180-
ty::ty_box(mt) |
181-
ty::ty_evec(mt, ty::vstore_box) => {
182+
ty::ty_box(ref mt) |
183+
ty::ty_evec(ref mt, ty::vstore_box) => {
182184
Some(deref_ptr(gc_ptr(mt.mutbl)))
183185
}
184186

187+
ty::ty_trait(_, _, ty::BoxTraitStore, m, _) => {
188+
Some(deref_ptr(gc_ptr(m)))
189+
}
190+
185191
ty::ty_estr(ty::vstore_box) |
186192
ty::ty_closure(ty::ClosureTy {sigil: ast::ManagedSigil, _}) => {
187193
Some(deref_ptr(gc_ptr(ast::m_imm)))
188194
}
189195

190-
ty::ty_ptr(*) => {
191-
Some(deref_ptr(unsafe_ptr))
196+
ty::ty_ptr(ref mt) => {
197+
Some(deref_ptr(unsafe_ptr(mt.mutbl)))
192198
}
193199

194200
ty::ty_enum(*) |
@@ -631,20 +637,19 @@ impl mem_categorization_ctxt {
631637
}
632638
}
633639

634-
pub fn cat_deref_fn<N:ast_node>(&self,
635-
node: N,
636-
base_cmt: cmt,
637-
deref_cnt: uint)
638-
-> cmt {
640+
pub fn cat_deref_fn_or_obj<N:ast_node>(&self,
641+
node: N,
642+
base_cmt: cmt,
643+
deref_cnt: uint)
644+
-> cmt {
639645
// Bit of a hack: the "dereference" of a function pointer like
640646
// `@fn()` is a mere logical concept. We interpret it as
641647
// dereferencing the environment pointer; of course, we don't
642648
// know what type lies at the other end, so we just call it
643649
// `()` (the empty tuple).
644650

645-
let mt = ty::mt {ty: ty::mk_tup(self.tcx, ~[]),
646-
mutbl: m_imm};
647-
return self.cat_deref_common(node, base_cmt, deref_cnt, mt);
651+
let opaque_ty = ty::mk_tup(self.tcx, ~[]);
652+
return self.cat_deref_common(node, base_cmt, deref_cnt, opaque_ty);
648653
}
649654

650655
pub fn cat_deref<N:ast_node>(&self,
@@ -662,25 +667,25 @@ impl mem_categorization_ctxt {
662667
}
663668
};
664669

665-
return self.cat_deref_common(node, base_cmt, deref_cnt, mt);
670+
return self.cat_deref_common(node, base_cmt, deref_cnt, mt.ty);
666671
}
667672

668673
pub fn cat_deref_common<N:ast_node>(&self,
669674
node: N,
670675
base_cmt: cmt,
671676
deref_cnt: uint,
672-
mt: ty::mt)
677+
deref_ty: ty::t)
673678
-> cmt {
674679
match deref_kind(self.tcx, base_cmt.ty) {
675680
deref_ptr(ptr) => {
676681
// for unique ptrs, we inherit mutability from the
677682
// owning reference.
678683
let m = match ptr {
679-
uniq_ptr(*) => {
680-
self.inherited_mutability(base_cmt.mutbl, mt.mutbl)
684+
uniq_ptr => {
685+
base_cmt.mutbl.inherit()
681686
}
682-
gc_ptr(*) | region_ptr(_, _) | unsafe_ptr => {
683-
MutabilityCategory::from_mutbl(mt.mutbl)
687+
gc_ptr(m) | region_ptr(m, _) | unsafe_ptr(m) => {
688+
MutabilityCategory::from_mutbl(m)
684689
}
685690
};
686691

@@ -689,18 +694,18 @@ impl mem_categorization_ctxt {
689694
span:node.span(),
690695
cat:cat_deref(base_cmt, deref_cnt, ptr),
691696
mutbl:m,
692-
ty:mt.ty
697+
ty:deref_ty
693698
}
694699
}
695700

696701
deref_interior(interior) => {
697-
let m = self.inherited_mutability(base_cmt.mutbl, mt.mutbl);
702+
let m = base_cmt.mutbl.inherit();
698703
@cmt_ {
699704
id:node.id(),
700705
span:node.span(),
701706
cat:cat_interior(base_cmt, interior),
702707
mutbl:m,
703-
ty:mt.ty
708+
ty:deref_ty
704709
}
705710
}
706711
}
@@ -742,8 +747,8 @@ impl mem_categorization_ctxt {
742747
//! - `derefs`: the deref number to be used for
743748
//! the implicit index deref, if any (see above)
744749
745-
let mt = match ty::index(base_cmt.ty) {
746-
Some(mt) => mt,
750+
let element_ty = match ty::index(base_cmt.ty) {
751+
Some(ref mt) => mt.ty,
747752
None => {
748753
self.tcx.sess.span_bug(
749754
elt.span(),
@@ -757,11 +762,11 @@ impl mem_categorization_ctxt {
757762
// for unique ptrs, we inherit mutability from the
758763
// owning reference.
759764
let m = match ptr {
760-
uniq_ptr(*) => {
761-
self.inherited_mutability(base_cmt.mutbl, mt.mutbl)
765+
uniq_ptr => {
766+
base_cmt.mutbl.inherit()
762767
}
763-
gc_ptr(_) | region_ptr(_, _) | unsafe_ptr => {
764-
MutabilityCategory::from_mutbl(mt.mutbl)
768+
gc_ptr(m) | region_ptr(m, _) | unsafe_ptr(m) => {
769+
MutabilityCategory::from_mutbl(m)
765770
}
766771
};
767772

@@ -771,31 +776,31 @@ impl mem_categorization_ctxt {
771776
span:elt.span(),
772777
cat:cat_deref(base_cmt, derefs, ptr),
773778
mutbl:m,
774-
ty:mt.ty
779+
ty:element_ty
775780
};
776781

777-
interior(elt, deref_cmt, base_cmt.ty, m, mt)
782+
interior(elt, deref_cmt, base_cmt.ty, m, element_ty)
778783
}
779784

780785
deref_interior(_) => {
781786
// fixed-length vectors have no deref
782-
let m = self.inherited_mutability(base_cmt.mutbl, mt.mutbl);
783-
interior(elt, base_cmt, base_cmt.ty, m, mt)
787+
let m = base_cmt.mutbl.inherit();
788+
interior(elt, base_cmt, base_cmt.ty, m, element_ty)
784789
}
785790
};
786791

787792
fn interior<N: ast_node>(elt: N,
788793
of_cmt: cmt,
789794
vec_ty: ty::t,
790795
mutbl: MutabilityCategory,
791-
mt: ty::mt) -> cmt
796+
element_ty: ty::t) -> cmt
792797
{
793798
@cmt_ {
794799
id:elt.id(),
795800
span:elt.span(),
796801
cat:cat_interior(of_cmt, InteriorElement(element_kind(vec_ty))),
797802
mutbl:mutbl,
798-
ty:mt.ty
803+
ty:element_ty
799804
}
800805
}
801806
}
@@ -1130,7 +1135,7 @@ impl cmt_ {
11301135
cat_stack_upvar(b) |
11311136
cat_discr(b, _) |
11321137
cat_interior(b, _) |
1133-
cat_deref(b, _, uniq_ptr(*)) => {
1138+
cat_deref(b, _, uniq_ptr) => {
11341139
b.guarantor()
11351140
}
11361141
}
@@ -1177,7 +1182,7 @@ impl cmt_ {
11771182

11781183
cat_downcast(b) |
11791184
cat_stack_upvar(b) |
1180-
cat_deref(b, _, uniq_ptr(*)) |
1185+
cat_deref(b, _, uniq_ptr) |
11811186
cat_interior(b, _) |
11821187
cat_discr(b, _) => {
11831188
b.freely_aliasable()
@@ -1230,10 +1235,10 @@ impl Repr for categorization {
12301235

12311236
pub fn ptr_sigil(ptr: ptr_kind) -> ~str {
12321237
match ptr {
1233-
uniq_ptr(_) => ~"~",
1238+
uniq_ptr => ~"~",
12341239
gc_ptr(_) => ~"@",
12351240
region_ptr(_, _) => ~"&",
1236-
unsafe_ptr => ~"*"
1241+
unsafe_ptr(_) => ~"*"
12371242
}
12381243
}
12391244

0 commit comments

Comments
 (0)