Skip to content

Commit 3789960

Browse files
committed
store into the borrowings table
1 parent 4d73185 commit 3789960

File tree

3 files changed

+61
-59
lines changed

3 files changed

+61
-59
lines changed

src/rustc/middle/infer.rs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import std::smallintmap;
22
import std::smallintmap::smallintmap;
33
import std::smallintmap::map;
4+
import std::map::hashmap;
45
import middle::ty;
56
import middle::ty::{ty_vid, region_vid, vid};
67
import syntax::ast;
@@ -77,11 +78,12 @@ fn mk_eqty(cx: infer_ctxt, a: ty::t, b: ty::t) -> ures {
7778
indent {|| cx.commit {|| cx.eq_tys(a, b) } }.to_ures()
7879
}
7980

80-
fn mk_assignty(cx: infer_ctxt, encl_node_id: ast::node_id,
81+
fn mk_assignty(cx: infer_ctxt, a_node_id: ast::node_id,
8182
a: ty::t, b: ty::t) -> ures {
82-
#debug["mk_assignty(%s <: %s)", a.to_str(cx), b.to_str(cx)];
83+
#debug["mk_assignty(%? / %s <: %s)",
84+
a_node_id, a.to_str(cx), b.to_str(cx)];
8385
indent {|| cx.commit {||
84-
cx.assign_tys(encl_node_id, a, b)
86+
cx.assign_tys(a_node_id, a, b)
8587
} }.to_ures()
8688
}
8789

@@ -686,7 +688,7 @@ impl resolve_methods for infer_ctxt {
686688
// Type assignment
687689
//
688690
// True if rvalues of type `a` can be assigned to lvalues of type `b`.
689-
// This may cause borrowing to the region scope for `encl_node_id`.
691+
// This may cause borrowing to the region scope enclosing `a_node_id`.
690692
//
691693
// The strategy here is somewhat non-obvious. The problem is
692694
// that the constraint we wish to contend with is not a subtyping
@@ -730,7 +732,7 @@ impl resolve_methods for infer_ctxt {
730732
// this upper-bound might be stricter than what is truly needed.
731733

732734
impl assignment for infer_ctxt {
733-
fn assign_tys(encl_node_id: ast::node_id,
735+
fn assign_tys(a_node_id: ast::node_id,
734736
a: ty::t, b: ty::t) -> ures {
735737

736738
fn select(fst: option<ty::t>, snd: option<ty::t>) -> option<ty::t> {
@@ -745,8 +747,8 @@ impl assignment for infer_ctxt {
745747
}
746748
}
747749

748-
#debug["assign_tys(encl_node_id=%?, %s -> %s)",
749-
encl_node_id, a.to_str(self), b.to_str(self)];
750+
#debug["assign_tys(a_node_id=%?, %s -> %s)",
751+
a_node_id, a.to_str(self), b.to_str(self)];
750752
let _r = indenter();
751753

752754
alt (ty::get(a).struct, ty::get(b).struct) {
@@ -759,34 +761,34 @@ impl assignment for infer_ctxt {
759761
let {root:_, bounds: b_bounds} = self.get(self.vb, b_id);
760762
let a_bnd = select(a_bounds.ub, a_bounds.lb);
761763
let b_bnd = select(b_bounds.lb, b_bounds.ub);
762-
self.assign_tys_or_sub(encl_node_id, a, b, a_bnd, b_bnd)
764+
self.assign_tys_or_sub(a_node_id, a, b, a_bnd, b_bnd)
763765
}
764766

765767
(ty::ty_var(a_id), _) {
766768
let {root:_, bounds:a_bounds} = self.get(self.vb, a_id);
767769
let a_bnd = select(a_bounds.ub, a_bounds.lb);
768-
self.assign_tys_or_sub(encl_node_id, a, b, a_bnd, some(b))
770+
self.assign_tys_or_sub(a_node_id, a, b, a_bnd, some(b))
769771
}
770772

771773
(_, ty::ty_var(b_id)) {
772774
let {root:_, bounds: b_bounds} = self.get(self.vb, b_id);
773775
let b_bnd = select(b_bounds.lb, b_bounds.ub);
774-
self.assign_tys_or_sub(encl_node_id, a, b, some(a), b_bnd)
776+
self.assign_tys_or_sub(a_node_id, a, b, some(a), b_bnd)
775777
}
776778

777779
(_, _) {
778-
self.assign_tys_or_sub(encl_node_id, a, b, some(a), some(b))
780+
self.assign_tys_or_sub(a_node_id, a, b, some(a), some(b))
779781
}
780782
}
781783
}
782784

783785
fn assign_tys_or_sub(
784-
encl_node_id: ast::node_id,
786+
a_node_id: ast::node_id,
785787
a: ty::t, b: ty::t,
786788
a_bnd: option<ty::t>, b_bnd: option<ty::t>) -> ures {
787789

788-
#debug["assign_tys_or_sub(encl_node_id=%?, %s -> %s, %s -> %s)",
789-
encl_node_id, a.to_str(self), b.to_str(self),
790+
#debug["assign_tys_or_sub(a_node_id=%?, %s -> %s, %s -> %s)",
791+
a_node_id, a.to_str(self), b.to_str(self),
790792
a_bnd.to_str(self), b_bnd.to_str(self)];
791793
let _r = indenter();
792794

@@ -802,17 +804,17 @@ impl assignment for infer_ctxt {
802804
alt (ty::get(a_bnd).struct, ty::get(b_bnd).struct) {
803805
(ty::ty_box(mt_a), ty::ty_rptr(r_b, mt_b)) {
804806
let nr_b = ty::mk_box(self.tcx, mt_b);
805-
self.crosspolinate(encl_node_id, a, nr_b, r_b)
807+
self.crosspolinate(a_node_id, a, nr_b, r_b)
806808
}
807809
(ty::ty_uniq(mt_a), ty::ty_rptr(r_b, mt_b)) {
808810
let nr_b = ty::mk_uniq(self.tcx, mt_b);
809-
self.crosspolinate(encl_node_id, a, nr_b, r_b)
811+
self.crosspolinate(a_node_id, a, nr_b, r_b)
810812
}
811813
(ty::ty_evec(mt_a, vs_a),
812814
ty::ty_evec(mt_b, ty::vstore_slice(r_b)))
813815
if is_borrowable(vs_a) {
814816
let nr_b = ty::mk_evec(self.tcx, mt_b, vs_a);
815-
self.crosspolinate(encl_node_id, a, nr_b, r_b)
817+
self.crosspolinate(a_node_id, a, nr_b, r_b)
816818
}
817819
_ {
818820
self.sub_tys(a, b)
@@ -825,17 +827,26 @@ impl assignment for infer_ctxt {
825827
}
826828
}
827829

828-
fn crosspolinate(encl_node_id: ast::node_id,
829-
a: ty::t, nr_b: ty::t, r_b: ty::region) -> ures {
830+
fn crosspolinate(a_node_id: ast::node_id,
831+
a: ty::t,
832+
nr_b: ty::t,
833+
r_b: ty::region) -> ures {
830834

831-
#debug["crosspolinate(encl_node_id=%?, a=%s, nr_b=%s, r_b=%s)",
832-
encl_node_id, a.to_str(self), nr_b.to_str(self),
835+
#debug["crosspolinate(a_node_id=%?, a=%s, nr_b=%s, r_b=%s)",
836+
a_node_id, a.to_str(self), nr_b.to_str(self),
833837
r_b.to_str(self)];
834838

835839
indent {||
836840
self.sub_tys(a, nr_b).then {||
837-
let r_a = ty::re_scope(encl_node_id);
838-
sub(self).contraregions(r_a, r_b).to_ures()
841+
let a_scope_id = self.tcx.region_map.parents.get(a_node_id);
842+
let r_a = ty::re_scope(a_scope_id);
843+
#debug["a_scope_id=%?", a_scope_id];
844+
sub(self).contraregions(r_a, r_b).chain {|_r|
845+
// if successful, add an entry indicating that
846+
// borrowing occurred
847+
self.tcx.borrowings.insert(a_node_id, ());
848+
uok()
849+
}
839850
}
840851
}
841852
}

src/rustc/middle/ty.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,6 @@ enum ast_ty_to_ty_cache_entry {
192192
atttce_resolved(t) /* resolved to a type, irrespective of region */
193193
}
194194

195-
enum borrowing {
196-
bo_none,
197-
bo_box(@borrowing),
198-
bo_uniq(@borrowing)
199-
}
200-
201195
type ctxt =
202196
@{interner: hashmap<intern_key, t_box>,
203197
mut next_id: uint,
@@ -229,7 +223,7 @@ type ctxt =
229223
iface_method_cache: hashmap<def_id, @[method]>,
230224
ty_param_bounds: hashmap<ast::node_id, param_bounds>,
231225
inferred_modes: hashmap<ast::node_id, ast::mode>,
232-
borrowings: hashmap<ast::node_id, borrowing>};
226+
borrowings: hashmap<ast::node_id, ()>};
233227

234228
type t_box = @{struct: sty,
235229
id: uint,

src/rustc/middle/typeck.rs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,15 +1426,13 @@ mod demand {
14261426
}
14271427
}
14281428
1429-
// Checks that the type `actual` can be assigned to `expected`, borrowing
1430-
// to `encl_node_id` if necessary.
1431-
fn assign(fcx: @fn_ctxt, sp: span, encl_node_id: ast::node_id,
1432-
expected: ty::t, actual: ty::t) {
1433-
1434-
alt infer::mk_assignty(fcx.infcx, encl_node_id, actual, expected) {
1429+
// Checks that the type `actual` can be assigned to `expected`.
1430+
fn assign(fcx: @fn_ctxt, sp: span, expected: ty::t, expr: @ast::expr) {
1431+
let expr_ty = fcx.expr_ty(expr);
1432+
alt infer::mk_assignty(fcx.infcx, expr.id, expr_ty, expected) {
14351433
result::ok(()) { /* ok */ }
14361434
result::err(err) {
1437-
fcx.report_mismatched_types(sp, expected, actual, err);
1435+
fcx.report_mismatched_types(sp, expected, expr_ty, err);
14381436
}
14391437
}
14401438
}
@@ -2179,17 +2177,16 @@ fn require_pure_call(ccx: @crate_ctxt, caller_purity: ast::purity,
21792177
}
21802178
}
21812179

2182-
type unifier = fn(@fn_ctxt, span, ty::t, ty::t);
2183-
21842180
fn check_expr(fcx: @fn_ctxt, expr: @ast::expr) -> bool {
2185-
ret check_expr_with_unifier(fcx, expr, ty::mk_nil(fcx.ccx.tcx)) {
2186-
|_fcx, _span, _t1, _t2|
2187-
/* unify is a no-op */
2181+
ret check_expr_with_unifier(fcx, expr, ty::mk_nil(fcx.ccx.tcx)) {||
2182+
/* do not take any action on unify */
21882183
};
21892184
}
21902185

21912186
fn check_expr_with(fcx: @fn_ctxt, expr: @ast::expr, expected: ty::t) -> bool {
2192-
ret check_expr_with_unifier(fcx, expr, expected, demand::suptype);
2187+
ret check_expr_with_unifier(fcx, expr, expected) {||
2188+
demand::suptype(fcx, expr.span, expected, fcx.expr_ty(expr));
2189+
};
21932190
}
21942191

21952192
// determine the `self` type, using fresh variables for all variables declared
@@ -2548,9 +2545,8 @@ fn check_expr_fn_with_unifier(fcx: @fn_ctxt,
25482545
proto: ast::proto,
25492546
decl: ast::fn_decl,
25502547
body: ast::blk,
2551-
unifier: unifier,
2552-
expected: ty::t,
2553-
is_loop_body: bool) {
2548+
is_loop_body: bool,
2549+
unifier: fn()) {
25542550
let tcx = fcx.ccx.tcx;
25552551
let fty = ty::mk_fn(tcx,
25562552
ty_of_fn_decl(tcx, m_check_tyvar(fcx), proto, decl));
@@ -2564,7 +2560,7 @@ fn check_expr_fn_with_unifier(fcx: @fn_ctxt,
25642560
// typecheck the body so that we have more information about the
25652561
// argument types in the body. This is needed to make binops and
25662562
// record projection work on type inferred arguments.
2567-
unifier(fcx, expr.span, expected, fty);
2563+
unifier();
25682564

25692565
let ret_ty = ty::ty_fn_ret(fty);
25702566
let arg_tys = vec::map(ty::ty_fn_args(fty)) {|a| a.ty };
@@ -2574,8 +2570,10 @@ fn check_expr_fn_with_unifier(fcx: @fn_ctxt,
25742570
fcx.self_ty);
25752571
}
25762572

2577-
fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr,
2578-
expected: ty::t, unifier: unifier) -> bool {
2573+
fn check_expr_with_unifier(fcx: @fn_ctxt,
2574+
expr: @ast::expr,
2575+
expected: ty::t,
2576+
unifier: fn()) -> bool {
25792577

25802578
#debug(">> typechecking expr %d (%s)",
25812579
expr.id, syntax::print::pprust::expr_to_str(expr));
@@ -2646,10 +2644,8 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr,
26462644
};
26472645
if is_block == check_blocks {
26482646
let arg_ty = arg_tys[i].ty;
2649-
bot |= check_expr_with_unifier(fcx, a, arg_ty) {
2650-
|fcx, span, expected, actual|
2651-
demand::assign(fcx, span, call_expr_id,
2652-
expected, actual);
2647+
bot |= check_expr_with_unifier(fcx, a, arg_ty) {||
2648+
demand::assign(fcx, a.span, arg_ty, a);
26532649
};
26542650
}
26552651
}
@@ -3071,7 +3067,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr,
30713067
fcx.write_nil(id);
30723068
}
30733069
ast::expr_copy(a) {
3074-
bot = check_expr_with_unifier(fcx, a, expected, unifier);
3070+
bot = check_expr_with(fcx, a, expected);
30753071
fcx.write_ty(id, fcx.expr_ty(a));
30763072
}
30773073
ast::expr_move(lhs, rhs) {
@@ -3142,7 +3138,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr,
31423138
}
31433139
ast::expr_fn(proto, decl, body, captures) {
31443140
check_expr_fn_with_unifier(fcx, expr, proto, decl, body,
3145-
unifier, expected, false);
3141+
false, unifier);
31463142
capture::check_capture_clause(tcx, expr.id, proto, *captures);
31473143
}
31483144
ast::expr_fn_block(decl, body) {
@@ -3155,7 +3151,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr,
31553151
expr_to_str(expr),
31563152
ty_to_str(tcx, expected));
31573153
check_expr_fn_with_unifier(fcx, expr, proto, decl, body,
3158-
unifier, expected, false);
3154+
false, unifier);
31593155
}
31603156
ast::expr_loop_body(b) {
31613157
let rty = structurally_resolved_type(fcx, expr.span, expected);
@@ -3168,8 +3164,9 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr,
31683164
};
31693165
alt check b.node {
31703166
ast::expr_fn_block(decl, body) {
3171-
check_expr_fn_with_unifier(fcx, b, proto, decl, body,
3172-
unifier, inner_ty, true);
3167+
check_expr_fn_with_unifier(fcx, b, proto, decl, body, true) {||
3168+
demand::suptype(fcx, b.span, inner_ty, fcx.expr_ty(b));
3169+
}
31733170
}
31743171
}
31753172
let block_ty = structurally_resolved_type(
@@ -3517,7 +3514,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr,
35173514
ty_to_str(tcx, fcx.expr_ty(expr)),
35183515
ty_to_str(tcx, expected));
35193516

3520-
unifier(fcx, expr.span, expected, fcx.expr_ty(expr));
3517+
unifier();
35213518

35223519
#debug("<< bot=%b", bot);
35233520
ret bot;

0 commit comments

Comments
 (0)