Skip to content

Commit 7f45ba4

Browse files
committed
Move inference-related fields/methods from fn_ctxt to infer_ctxt.
1 parent 251a0a3 commit 7f45ba4

File tree

6 files changed

+68
-60
lines changed

6 files changed

+68
-60
lines changed

src/rustc/middle/typeck.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import middle::ty;
5353
import middle::ty::{arg, field, node_type_table, mk_nil,
5454
ty_param_bounds_and_ty, lookup_public_fields};
5555
import middle::ty::{ty_vid, region_vid, vid};
56+
import middle::typeck::infer::{ty_and_region_var_methods};
5657
import util::ppaux::{ty_to_str, tys_to_str, region_to_str,
5758
bound_region_to_str, vstore_to_str};
5859
import std::smallintmap;
@@ -68,6 +69,7 @@ import std::list;
6869
import list::{list, nil, cons};
6970

7071
export check_crate;
72+
export infer;
7173
export method_map;
7274
export method_origin, serialize_method_origin, deserialize_method_origin;
7375
export vtable_map;

src/rustc/middle/typeck/check.rs

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ type fn_ctxt =
8686
proto: ast::proto,
8787
infcx: infer::infer_ctxt,
8888
locals: hashmap<ast::node_id, ty_vid>,
89-
ty_var_counter: @mut uint,
90-
region_var_counter: @mut uint,
9189

9290
mut blocks: [ast::node_id], // stack of blocks in scope, may be empty
9391
in_scope_regions: isr_alist,
@@ -181,13 +179,11 @@ fn check_fn(ccx: @crate_ctxt,
181179
// Create the function context. This is either derived from scratch or,
182180
// in the case of function expressions, based on the outer context.
183181
let fcx: @fn_ctxt = {
184-
let {infcx, locals, tvc, rvc, purity,
185-
node_types, node_type_substs} = alt old_fcx {
182+
let {infcx, locals, purity, node_types, node_type_substs} =
183+
alt old_fcx {
186184
none {
187185
{infcx: infer::new_infer_ctxt(tcx),
188186
locals: int_hash(),
189-
tvc: @mut 0u,
190-
rvc: @mut 0u,
191187
purity: decl.purity,
192188
node_types: smallintmap::mk(),
193189
node_type_substs: map::int_hash()}
@@ -196,8 +192,6 @@ fn check_fn(ccx: @crate_ctxt,
196192
assert decl.purity == ast::impure_fn;
197193
{infcx: fcx.infcx,
198194
locals: fcx.locals,
199-
tvc: fcx.ty_var_counter,
200-
rvc: fcx.region_var_counter,
201195
purity: fcx.purity,
202196
node_types: fcx.node_types,
203197
node_type_substs: fcx.node_type_substs}
@@ -219,8 +213,6 @@ fn check_fn(ccx: @crate_ctxt,
219213
proto: proto,
220214
infcx: infcx,
221215
locals: locals,
222-
ty_var_counter: tvc,
223-
region_var_counter: rvc,
224216
mut blocks: [],
225217
in_scope_regions: isr,
226218
node_types: node_types,
@@ -265,7 +257,7 @@ fn check_fn(ccx: @crate_ctxt,
265257
let tcx = fcx.ccx.tcx;
266258

267259
let assign = fn@(nid: ast::node_id, ty_opt: option<ty::t>) {
268-
let var_id = fcx.next_ty_var_id();
260+
let var_id = fcx.infcx.next_ty_var_id();
269261
fcx.locals.insert(nid, var_id);
270262
alt ty_opt {
271263
none {/* nothing to do */ }
@@ -429,13 +421,13 @@ impl of ast_conv for @fn_ctxt {
429421
}
430422

431423
fn ty_infer(_span: span) -> ty::t {
432-
self.next_ty_var()
424+
self.infcx.next_ty_var()
433425
}
434426
}
435427

436428
impl of region_scope for @fn_ctxt {
437429
fn anon_region() -> result<ty::region, str> {
438-
result::ok(self.next_region_var())
430+
result::ok(self.infcx.next_region_var())
439431
}
440432
fn named_region(id: str) -> result<ty::region, str> {
441433
empty_rscope.named_region(id).chain_err { |_e|
@@ -522,25 +514,6 @@ impl methods for @fn_ctxt {
522514
fn opt_node_ty_substs(id: ast::node_id) -> option<ty::substs> {
523515
self.node_type_substs.find(id)
524516
}
525-
fn next_ty_var_id() -> ty_vid {
526-
let id = *self.ty_var_counter;
527-
*self.ty_var_counter += 1u;
528-
ret ty_vid(id);
529-
}
530-
fn next_ty_var() -> ty::t {
531-
ty::mk_var(self.ccx.tcx, self.next_ty_var_id())
532-
}
533-
fn next_ty_vars(n: uint) -> [ty::t] {
534-
vec::from_fn(n) {|_i| self.next_ty_var() }
535-
}
536-
fn next_region_var_id() -> region_vid {
537-
let id = *self.region_var_counter;
538-
*self.region_var_counter += 1u;
539-
ret region_vid(id);
540-
}
541-
fn next_region_var() -> ty::region {
542-
ret ty::re_var(self.next_region_var_id());
543-
}
544517

545518
fn report_mismatched_types(sp: span, e: ty::t, a: ty::t,
546519
err: ty::type_err) {
@@ -691,7 +664,7 @@ fn impl_self_ty(fcx: @fn_ctxt, did: ast::def_id) -> ty_param_substs_and_ty {
691664
rp: rp,
692665
raw_ty: ty::mk_class(tcx, local_def(class_id),
693666
{self_r: alt rp {
694-
ast::rp_self { some(fcx.next_region_var()) }
667+
ast::rp_self { some(fcx.infcx.next_region_var()) }
695668
ast::rp_none { none }},
696669
self_ty: none,
697670
tps: ty::ty_params_to_tys(tcx, ts)})}
@@ -708,9 +681,9 @@ fn impl_self_ty(fcx: @fn_ctxt, did: ast::def_id) -> ty_param_substs_and_ty {
708681

709682
let self_r = alt rp {
710683
ast::rp_none { none }
711-
ast::rp_self { some(fcx.next_region_var()) }
684+
ast::rp_self { some(fcx.infcx.next_region_var()) }
712685
};
713-
let tps = fcx.next_ty_vars(n_tps);
686+
let tps = fcx.infcx.next_ty_vars(n_tps);
714687

715688
let substs = {self_r: self_r, self_ty: none, tps: tps};
716689
let substd_ty = ty::subst(tcx, substs, raw_ty);
@@ -772,7 +745,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
772745
} else {
773746
"s were"
774747
}]);
775-
fcx.next_ty_vars(supplied_arg_count)
748+
fcx.infcx.next_ty_vars(supplied_arg_count)
776749
}
777750
}
778751

@@ -875,7 +848,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
875848
let (if_t, if_bot) =
876849
alt elsopt {
877850
some(els) {
878-
let if_t = fcx.next_ty_var();
851+
let if_t = fcx.infcx.next_ty_var();
879852
let thn_bot = check_block(fcx, thn);
880853
let thn_t = fcx.node_ty(thn.node.id);
881854
demand::suptype(fcx, thn.span, if_t, thn_t);
@@ -938,7 +911,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
938911
// result [ML T] where TL <: T and TR <: T. In other words, the
939912
// result type is (generally) the LUB of (TL, TR) and takes the
940913
// mutability from the LHS.
941-
let t_var = fcx.next_ty_var();
914+
let t_var = fcx.infcx.next_ty_var();
942915
let const_vec_t = ty::mk_vec(tcx, {ty: t_var,
943916
mutbl: ast::m_const});
944917
demand::suptype(fcx, lhs.span, const_vec_t, lhs_t);
@@ -960,7 +933,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
960933
}
961934

962935
(_, _) if ty::is_binopable(tcx, lhs_t, op) {
963-
let tvar = fcx.next_ty_var();
936+
let tvar = fcx.infcx.next_ty_var();
964937
demand::suptype(fcx, expr.span, tvar, lhs_t);
965938
let rhs_bot = check_expr_with(fcx, rhs, tvar);
966939
let rhs_t = alt op {
@@ -1087,7 +1060,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
10871060
}
10881061
ast::expr_vec(args, mutbl) {
10891062
let tt = ast_expr_vstore_to_vstore(fcx, ev, vec::len(args), vst);
1090-
let t: ty::t = fcx.next_ty_var();
1063+
let t: ty::t = fcx.infcx.next_ty_var();
10911064
for args.each {|e| bot |= check_expr_with(fcx, e, t); }
10921065
ty::mk_evec(tcx, {ty: t, mutbl: mutbl}, tt)
10931066
}
@@ -1117,7 +1090,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
11171090
ast::expr_binary(ast::gt, lhs, rhs) |
11181091
ast::expr_binary(ast::ge, lhs, rhs) {
11191092
let tcx = fcx.ccx.tcx;
1120-
let tvar = fcx.next_ty_var();
1093+
let tvar = fcx.infcx.next_ty_var();
11211094
bot |= check_expr_with(fcx, lhs, tvar);
11221095
bot |= check_expr_with(fcx, rhs, tvar);
11231096
fcx.write_ty(id, ty::mk_bool(tcx));
@@ -1463,7 +1436,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
14631436
fcx.write_ty(id, t_1);
14641437
}
14651438
ast::expr_vec(args, mutbl) {
1466-
let t: ty::t = fcx.next_ty_var();
1439+
let t: ty::t = fcx.infcx.next_ty_var();
14671440
for args.each {|e| bot |= check_expr_with(fcx, e, t); }
14681441
let typ = ty::mk_vec(tcx, {ty: t, mutbl: mutbl});
14691442
fcx.write_ty(id, typ);
@@ -1608,7 +1581,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
16081581
field, ty_to_str(tcx, t_err)];
16091582
tcx.sess.span_err(expr.span, msg);
16101583
// NB: Adding a bogus type to allow typechecking to continue
1611-
fcx.write_ty(id, fcx.next_ty_var());
1584+
fcx.write_ty(id, fcx.infcx.next_ty_var());
16121585
}
16131586
}
16141587
}
@@ -1836,8 +1809,6 @@ fn check_const(ccx: @crate_ctxt, _sp: span, e: @ast::expr, id: ast::node_id) {
18361809
proto: ast::proto_box,
18371810
infcx: infer::new_infer_ctxt(ccx.tcx),
18381811
locals: int_hash(),
1839-
ty_var_counter: @mut 0u,
1840-
region_var_counter: @mut 0u,
18411812
mut blocks: [],
18421813
in_scope_regions: @nil,
18431814
node_types: smallintmap::mk(),
@@ -1878,8 +1849,6 @@ fn check_enum_variants(ccx: @crate_ctxt,
18781849
proto: ast::proto_box,
18791850
infcx: infer::new_infer_ctxt(ccx.tcx),
18801851
locals: int_hash(),
1881-
ty_var_counter: @mut 0u,
1882-
region_var_counter: @mut 0u,
18831852
mut blocks: [],
18841853
in_scope_regions: @nil,
18851854
node_types: smallintmap::mk(),
@@ -2148,24 +2117,24 @@ fn instantiate_path(fcx: @fn_ctxt,
21482117
// For now, there is no way to explicitly specify the region bound.
21492118
// This will have to change eventually.
21502119
let self_r = alt tpt.rp {
2151-
ast::rp_self { some(fcx.next_region_var()) }
2120+
ast::rp_self { some(fcx.infcx.next_region_var()) }
21522121
ast::rp_none { none }
21532122
};
21542123

21552124
let tps = if ty_substs_len == 0u {
2156-
fcx.next_ty_vars(ty_param_count)
2125+
fcx.infcx.next_ty_vars(ty_param_count)
21572126
} else if ty_param_count == 0u {
21582127
fcx.ccx.tcx.sess.span_err
21592128
(sp, "this item does not take type parameters");
2160-
fcx.next_ty_vars(ty_param_count)
2129+
fcx.infcx.next_ty_vars(ty_param_count)
21612130
} else if ty_substs_len > ty_param_count {
21622131
fcx.ccx.tcx.sess.span_err
21632132
(sp, "too many type parameters provided for this item");
2164-
fcx.next_ty_vars(ty_param_count)
2133+
fcx.infcx.next_ty_vars(ty_param_count)
21652134
} else if ty_substs_len < ty_param_count {
21662135
fcx.ccx.tcx.sess.span_err
21672136
(sp, "not enough type parameters provided for this item");
2168-
fcx.next_ty_vars(ty_param_count)
2137+
fcx.infcx.next_ty_vars(ty_param_count)
21692138
} else {
21702139
pth.types.map { |aty| fcx.to_ty(aty) }
21712140
};

src/rustc/middle/typeck/check/alt.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import middle::typeck::infer::{ty_and_region_var_methods};
2+
13
fn check_alt(fcx: @fn_ctxt,
24
expr: @ast::expr,
35
discrim: @ast::expr,
46
arms: [ast::arm]) -> bool {
57
let tcx = fcx.ccx.tcx;
68
let mut bot = false;
79

8-
let pattern_ty = fcx.next_ty_var();
10+
let pattern_ty = fcx.infcx.next_ty_var();
911
bot = check_expr_with(fcx, discrim, pattern_ty);
1012

1113
// Typecheck the patterns first, so that we get types for all the
@@ -22,7 +24,7 @@ fn check_alt(fcx: @fn_ctxt,
2224
for arm.pats.each {|p| check_pat(pcx, p, pattern_ty);}
2325
}
2426
// Now typecheck the blocks.
25-
let mut result_ty = fcx.next_ty_var();
27+
let mut result_ty = fcx.infcx.next_ty_var();
2628
let mut arm_non_bot = false;
2729
for arms.each {|arm|
2830
alt arm.guard {

src/rustc/middle/typeck/check/method.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Code to handle method lookups (which can be quite complex) */
22

33
import regionmanip::universally_quantify_regions;
4+
import middle::typeck::infer::{ty_and_region_var_methods};
45

56
enum lookup = {
67
fcx: @fn_ctxt,
@@ -266,18 +267,18 @@ impl methods for lookup {
266267
let n_tps_supplied = self.supplied_tps.len();
267268
let m_substs = {
268269
if n_tps_supplied == 0u {
269-
self.fcx.next_ty_vars(n_tps_m)
270+
self.fcx.infcx.next_ty_vars(n_tps_m)
270271
} else if n_tps_m == 0u {
271272
tcx.sess.span_err(
272273
self.expr.span,
273274
"this method does not take type parameters");
274-
self.fcx.next_ty_vars(n_tps_m)
275+
self.fcx.infcx.next_ty_vars(n_tps_m)
275276
} else if n_tps_supplied != n_tps_m {
276277
tcx.sess.span_err(
277278
self.expr.span,
278279
"incorrect number of type \
279280
parameters given for this method");
280-
self.fcx.next_ty_vars(n_tps_m)
281+
self.fcx.infcx.next_ty_vars(n_tps_m)
281282
} else {
282283
self.supplied_tps
283284
}

src/rustc/middle/typeck/check/regionmanip.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import middle::typeck::infer::{ty_and_region_var_methods};
2+
13
// Helper functions related to manipulating region types.
24

35
// Helper for the other universally_quantify_*() routines. Extracts the bound
@@ -13,7 +15,7 @@ fn universally_quantify_from_sty(fcx: @fn_ctxt,
1315
indent {||
1416
let tcx = fcx.tcx();
1517
let isr = collect_bound_regions_in_tys(tcx, @nil, bound_tys) { |br|
16-
let rvar = fcx.next_region_var();
18+
let rvar = fcx.infcx.next_region_var();
1719
#debug["Bound region %s maps to %s",
1820
bound_region_to_str(fcx.ccx.tcx, br),
1921
region_to_str(fcx.ccx.tcx, rvar)];

src/rustc/middle/typeck/infer.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ export mk_assignty;
165165
export resolve_shallow;
166166
export resolve_deep;
167167
export resolve_deep_var;
168+
export ty_and_region_var_methods;
168169
export compare_tys;
169170
export fixup_err, fixup_err_to_str;
170171

@@ -195,6 +196,10 @@ enum infer_ctxt = @{
195196
tcx: ty::ctxt,
196197
vb: vals_and_bindings<ty::ty_vid, ty::t>,
197198
rb: vals_and_bindings<ty::region_vid, ty::region>,
199+
200+
// For keeping track of existing type/region variables.
201+
ty_var_counter: @mut uint,
202+
region_var_counter: @mut uint,
198203
};
199204

200205
enum fixup_err {
@@ -219,8 +224,9 @@ type fres<T> = result::result<T, fixup_err>;
219224
fn new_infer_ctxt(tcx: ty::ctxt) -> infer_ctxt {
220225
infer_ctxt(@{tcx: tcx,
221226
vb: {vals: smallintmap::mk(), mut bindings: []},
222-
rb: {vals: smallintmap::mk(), mut bindings: []}})
223-
}
227+
rb: {vals: smallintmap::mk(), mut bindings: []},
228+
ty_var_counter: @mut 0u,
229+
region_var_counter: @mut 0u})}
224230

225231
fn mk_subty(cx: infer_ctxt, a: ty::t, b: ty::t) -> ures {
226232
#debug["mk_subty(%s <: %s)", a.to_str(cx), b.to_str(cx)];
@@ -423,6 +429,32 @@ impl methods for infer_ctxt {
423429
}
424430
}
425431

432+
impl ty_and_region_var_methods for infer_ctxt {
433+
fn next_ty_var_id() -> ty_vid {
434+
let id = *self.ty_var_counter;
435+
*self.ty_var_counter += 1u;
436+
ret ty_vid(id);
437+
}
438+
439+
fn next_ty_var() -> ty::t {
440+
ty::mk_var(self.tcx, self.next_ty_var_id())
441+
}
442+
443+
fn next_ty_vars(n: uint) -> [ty::t] {
444+
vec::from_fn(n) {|_i| self.next_ty_var() }
445+
}
446+
447+
fn next_region_var_id() -> region_vid {
448+
let id = *self.region_var_counter;
449+
*self.region_var_counter += 1u;
450+
ret region_vid(id);
451+
}
452+
453+
fn next_region_var() -> ty::region {
454+
ret ty::re_var(self.next_region_var_id());
455+
}
456+
}
457+
426458
impl unify_methods for infer_ctxt {
427459

428460
fn set<V:copy vid, T:copy to_str>(

0 commit comments

Comments
 (0)