From 903033bb03d05b66cbbf79a58fc9989887e35b65 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Mon, 4 Jun 2012 16:21:14 -0700 Subject: [PATCH] handle fixed-length vecs in borrowck categorization --- src/rustc/middle/borrowck.rs | 10 +++-- src/rustc/middle/borrowck/categorization.rs | 45 +++++++++++++-------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/rustc/middle/borrowck.rs b/src/rustc/middle/borrowck.rs index 261ee3ea802..2b52cd675a6 100644 --- a/src/rustc/middle/borrowck.rs +++ b/src/rustc/middle/borrowck.rs @@ -253,9 +253,13 @@ enum ptr_kind {uniq_ptr, gc_ptr, region_ptr, unsafe_ptr} // I am coining the term "components" to mean "pieces of a data // structure accessible without a dereference": -enum comp_kind {comp_tuple, comp_res, comp_variant, - comp_field(str, ast::mutability), - comp_index(ty::t, ast::mutability)} +enum comp_kind { + comp_tuple, comp_res, comp_variant, + comp_field(str, // name of field + ast::mutability), // declared mutability of field + comp_index(ty::t, // type of vec/str/etc being deref'd + ast::mutability) // mutability of vec content +} // We pun on *T to mean both actual deref of a ptr as well // as accessing of components: diff --git a/src/rustc/middle/borrowck/categorization.rs b/src/rustc/middle/borrowck/categorization.rs index 31763bb11c2..c7103b6899c 100644 --- a/src/rustc/middle/borrowck/categorization.rs +++ b/src/rustc/middle/borrowck/categorization.rs @@ -67,6 +67,14 @@ fn opt_deref_kind(t: ty::t) -> option { some(deref_comp(comp_res)) } + ty::ty_evec(mt, ty::vstore_fixed(_)) { + some(deref_comp(comp_index(t, mt.mutbl))) + } + + ty::ty_estr(ty::vstore_fixed(_)) { + some(deref_comp(comp_index(t, m_imm))) + } + _ { none } @@ -344,26 +352,31 @@ impl public_methods for borrowck_ctxt { } }; - let ptr = alt deref_kind(self.tcx, base_cmt.ty) { - deref_ptr(ptr) { ptr } + ret alt deref_kind(self.tcx, base_cmt.ty) { + deref_ptr(ptr) { + // make deref of vectors explicit, as explained in the comment at + // the head of this section + let deref_lp = base_cmt.lp.map { |lp| @lp_deref(lp, ptr) }; + let deref_cmt = @{id:expr.id, span:expr.span, + cat:cat_deref(base_cmt, 0u, ptr), lp:deref_lp, + mutbl:m_imm, ty:mt.ty}; + comp(expr, deref_cmt, base_cmt.ty, mt) + } + deref_comp(_) { - self.tcx.sess.span_bug( - expr.span, - "Deref of indexable type yielded comp kind"); + // fixed-length vectors have no deref + comp(expr, base_cmt, base_cmt.ty, mt) } }; - // make deref of vectors explicit, as explained in the comment at - // the head of this section - let deref_lp = base_cmt.lp.map { |lp| @lp_deref(lp, ptr) }; - let deref_cmt = @{id:expr.id, span:expr.span, - cat:cat_deref(base_cmt, 0u, ptr), lp:deref_lp, - mutbl:m_imm, ty:mt.ty}; - let comp = comp_index(base_cmt.ty, mt.mutbl); - let index_lp = deref_lp.map { |lp| @lp_comp(lp, comp) }; - @{id:expr.id, span:expr.span, - cat:cat_comp(deref_cmt, comp), lp:index_lp, - mutbl:mt.mutbl, ty:mt.ty} + fn comp(expr: @ast::expr, of_cmt: cmt, + vect: ty::t, mt: ty::mt) -> cmt { + let comp = comp_index(vect, mt.mutbl); + let index_lp = of_cmt.lp.map { |lp| @lp_comp(lp, comp) }; + @{id:expr.id, span:expr.span, + cat:cat_comp(of_cmt, comp), lp:index_lp, + mutbl:mt.mutbl, ty:mt.ty} + } } fn cat_tuple_elt(elt: N, cmt: cmt) -> cmt {