Skip to content

Commit f4b89f5

Browse files
committed
rustc: Move ty.unify to a separate namespace
1 parent 186717f commit f4b89f5

File tree

2 files changed

+50
-46
lines changed

2 files changed

+50
-46
lines changed

src/comp/middle/ty.rs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ tag sty {
7777

7878
type unify_handler = obj {
7979
fn resolve_local(ast.def_id id) -> option.t[@t];
80-
fn record_local(ast.def_id id, @t ty); // TODO: -> unify_result
81-
fn record_param(uint index, @t binding) -> unify_result;
80+
fn record_local(ast.def_id id, @t ty); // TODO: -> Unify.result
81+
fn record_param(uint index, @t binding) -> Unify.result;
8282
};
8383

8484
tag type_err {
@@ -95,11 +95,6 @@ tag type_err {
9595
terr_arg_count;
9696
}
9797

98-
tag unify_result {
99-
ures_ok(@ty.t);
100-
ures_err(type_err, @ty.t, @ty.t);
101-
}
102-
10398

10499
type ty_param_count_and_ty = tup(uint, @t);
105100
type type_cache = hashmap[ast.def_id,ty_param_count_and_ty];
@@ -1551,12 +1546,16 @@ fn is_lval(@ast.expr expr) -> bool {
15511546
//
15521547
// http://www.cs.man.ac.uk/~hoderk/ubench/unification_full.pdf
15531548

1554-
type var_bindings = rec(UFind.ufind sets,
1555-
hashmap[int,uint] var_ids,
1556-
mutable vec[mutable vec[@t]] types);
1549+
mod Unify {
1550+
tag result {
1551+
ures_ok(@ty.t);
1552+
ures_err(type_err, @ty.t, @ty.t);
1553+
}
1554+
1555+
type var_bindings = rec(UFind.ufind sets,
1556+
hashmap[int,uint] var_ids,
1557+
mutable vec[mutable vec[@t]] types);
15571558

1558-
fn unify(@ty.t expected, @ty.t actual, &unify_handler handler)
1559-
-> unify_result {
15601559
// Wraps the given type in an appropriate cname.
15611560
//
15621561
// TODO: This doesn't do anything yet. We should carry the cname up from
@@ -1565,7 +1564,7 @@ fn unify(@ty.t expected, @ty.t actual, &unify_handler handler)
15651564
// something we'll probably need to develop over time.
15661565

15671566
// Simple structural type comparison.
1568-
fn struct_cmp(@ty.t expected, @ty.t actual) -> unify_result {
1567+
fn struct_cmp(@ty.t expected, @ty.t actual) -> result {
15691568
if (expected.struct == actual.struct) {
15701569
ret ures_ok(expected);
15711570
}
@@ -1589,7 +1588,7 @@ fn unify(@ty.t expected, @ty.t actual, &unify_handler handler)
15891588
}
15901589

15911590
tag fn_common_res {
1592-
fn_common_res_err(unify_result);
1591+
fn_common_res_err(result);
15931592
fn_common_res_ok(vec[arg], @t);
15941593
}
15951594

@@ -1666,7 +1665,7 @@ fn unify(@ty.t expected, @ty.t actual, &unify_handler handler)
16661665
&unify_handler handler,
16671666
vec[arg] expected_inputs, @t expected_output,
16681667
vec[arg] actual_inputs, @t actual_output)
1669-
-> unify_result {
1668+
-> result {
16701669

16711670
if (e_proto != a_proto) {
16721671
ret ures_err(terr_mismatch, expected, actual);
@@ -1693,7 +1692,7 @@ fn unify(@ty.t expected, @ty.t actual, &unify_handler handler)
16931692
&unify_handler handler,
16941693
vec[arg] expected_inputs, @t expected_output,
16951694
vec[arg] actual_inputs, @t actual_output)
1696-
-> unify_result {
1695+
-> result {
16971696
if (e_abi != a_abi) {
16981697
ret ures_err(terr_mismatch, expected, actual);
16991698
}
@@ -1717,7 +1716,7 @@ fn unify(@ty.t expected, @ty.t actual, &unify_handler handler)
17171716
@ty.t actual,
17181717
&unify_handler handler,
17191718
vec[method] expected_meths,
1720-
vec[method] actual_meths) -> unify_result {
1719+
vec[method] actual_meths) -> result {
17211720
let vec[method] result_meths = vec();
17221721
let uint i = 0u;
17231722
let uint expected_len = _vec.len[method](expected_meths);
@@ -1772,7 +1771,7 @@ fn unify(@ty.t expected, @ty.t actual, &unify_handler handler)
17721771
}
17731772

17741773
fn unify_step(&var_bindings bindings, @ty.t expected, @ty.t actual,
1775-
&unify_handler handler) -> unify_result {
1774+
&unify_handler handler) -> result {
17761775
// TODO: rewrite this using tuple pattern matching when available, to
17771776
// avoid all this rightward drift and spikiness.
17781777

@@ -2261,24 +2260,27 @@ fn unify(@ty.t expected, @ty.t actual, &unify_handler handler)
22612260
ret result;
22622261
}
22632262

2264-
let vec[@t] throwaway = vec();
2265-
let vec[mutable vec[@t]] types = vec(mutable throwaway);
2266-
_vec.pop[mutable vec[@t]](types); // FIXME: botch
2263+
fn unify(@ty.t expected, @ty.t actual, &unify_handler handler)
2264+
-> result {
2265+
let vec[@t] throwaway = vec();
2266+
let vec[mutable vec[@t]] types = vec(mutable throwaway);
2267+
_vec.pop[mutable vec[@t]](types); // FIXME: botch
22672268

2268-
auto bindings = rec(sets=UFind.make(),
2269-
var_ids=common.new_int_hash[uint](),
2270-
mutable types=types);
2269+
auto bindings = rec(sets=UFind.make(),
2270+
var_ids=common.new_int_hash[uint](),
2271+
mutable types=types);
22712272

2272-
auto ures = unify_step(bindings, expected, actual, handler);
2273-
alt (ures) {
2274-
case (ures_ok(?t)) {
2275-
auto set_types = unify_sets(bindings);
2276-
auto t2 = substitute(bindings, set_types, t);
2277-
ret ures_ok(t2);
2278-
}
2279-
case (_) { ret ures; }
2273+
auto ures = unify_step(bindings, expected, actual, handler);
2274+
alt (ures) {
2275+
case (ures_ok(?t)) {
2276+
auto set_types = unify_sets(bindings);
2277+
auto t2 = substitute(bindings, set_types, t);
2278+
ret ures_ok(t2);
2279+
}
2280+
case (_) { ret ures; }
2281+
}
2282+
fail; // not reached
22802283
}
2281-
fail; // not reached
22822284
}
22832285

22842286
fn type_err_to_str(&ty.type_err err) -> str {

src/comp/middle/typeck.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import middle.ty.type_is_integral;
2525
import middle.ty.type_is_scalar;
2626
import middle.ty.ty_param_count_and_ty;
2727
import middle.ty.ty_nil;
28+
import middle.ty.Unify.ures_ok;
29+
import middle.ty.Unify.ures_err;
2830

2931
import std._str;
3032
import std._uint;
@@ -49,7 +51,7 @@ tag any_item {
4951
type ty_item_table = hashmap[ast.def_id,any_item];
5052

5153
type unify_cache_entry = tup(@ty.t,@ty.t,vec[mutable @ty.t]);
52-
type unify_cache = hashmap[unify_cache_entry,ty.unify_result];
54+
type unify_cache = hashmap[unify_cache_entry,ty.Unify.result];
5355

5456
type crate_ctxt = rec(session.session sess,
5557
ty.type_cache type_cache,
@@ -805,15 +807,15 @@ mod Collect {
805807
// Type unification
806808

807809
mod Unify {
808-
fn simple(@fn_ctxt fcx, @ty.t expected, @ty.t actual) -> ty.unify_result {
810+
fn simple(@fn_ctxt fcx, @ty.t expected, @ty.t actual) -> ty.Unify.result {
809811
// FIXME: horrid botch
810812
let vec[mutable @ty.t] param_substs = vec(mutable ty.mk_nil());
811813
_vec.pop[mutable @ty.t](param_substs);
812814
ret with_params(fcx, expected, actual, param_substs);
813815
}
814816

815817
fn with_params(@fn_ctxt fcx, @ty.t expected, @ty.t actual,
816-
vec[mutable @ty.t] param_substs) -> ty.unify_result {
818+
vec[mutable @ty.t] param_substs) -> ty.Unify.result {
817819
auto cache_key = tup(expected, actual, param_substs);
818820
if (fcx.ccx.unify_cache.contains_key(cache_key)) {
819821
fcx.ccx.cache_hits += 1u;
@@ -843,7 +845,7 @@ mod Unify {
843845
case (some[@ty.t](?old_type)) {
844846
alt (with_params(fcx, old_type, new_type,
845847
param_substs)) {
846-
case (ty.ures_ok(?ut)) { unified_type = ut; }
848+
case (ures_ok(?ut)) { unified_type = ut; }
847849
case (_) { fail; /* FIXME */ }
848850
}
849851
}
@@ -859,17 +861,17 @@ mod Unify {
859861
ty.substitute_type_params(param_substs_1, unified_type);
860862
fcx.locals.insert(id, unified_type);
861863
}
862-
fn record_param(uint index, @ty.t binding) -> ty.unify_result {
864+
fn record_param(uint index, @ty.t binding) -> ty.Unify.result {
863865
// Unify with the appropriate type in the parameter
864866
// substitution list.
865867
auto old_subst = param_substs.(index);
866868

867869
auto result = with_params(fcx, old_subst, binding,
868870
param_substs);
869871
alt (result) {
870-
case (ty.ures_ok(?new_subst)) {
872+
case (ures_ok(?new_subst)) {
871873
param_substs.(index) = new_subst;
872-
ret ty.ures_ok(ty.mk_bound_param(index));
874+
ret ures_ok(ty.mk_bound_param(index));
873875
}
874876
case (_) { ret result; }
875877
}
@@ -878,7 +880,7 @@ mod Unify {
878880

879881

880882
auto handler = unify_handler(fcx, param_substs);
881-
auto result = ty.unify(expected, actual, handler);
883+
auto result = ty.Unify.unify(expected, actual, handler);
882884
fcx.ccx.unify_cache.insert(cache_key, result);
883885
ret result;
884886
}
@@ -965,7 +967,7 @@ mod Demand {
965967
}
966968

967969
alt (Unify.with_params(fcx, expected_1, actual_1, ty_param_substs)) {
968-
case (ty.ures_ok(?t)) {
970+
case (ures_ok(?t)) {
969971
// TODO: Use "freeze", when we have it.
970972
let vec[@ty.t] result_ty_param_substs = vec();
971973
for (mutable @ty.t ty_param_subst in ty_param_substs) {
@@ -975,7 +977,7 @@ mod Demand {
975977
ret tup(result_ty_param_substs, add_boxes(implicit_boxes, t));
976978
}
977979

978-
case (ty.ures_err(?err, ?expected, ?actual)) {
980+
case (ures_err(?err, ?expected, ?actual)) {
979981
fcx.ccx.sess.span_err(sp, "mismatched types: expected "
980982
+ ty_to_str(expected) + " but found "
981983
+ ty_to_str(actual) + " (" +
@@ -993,8 +995,8 @@ mod Demand {
993995
// Returns true if the two types unify and false if they don't.
994996
fn are_compatible(&@fn_ctxt fcx, @ty.t expected, @ty.t actual) -> bool {
995997
alt (Unify.simple(fcx, expected, actual)) {
996-
case (ty.ures_ok(_)) { ret true; }
997-
case (ty.ures_err(_, _, _)) { ret false; }
998+
case (ures_ok(_)) { ret true; }
999+
case (ures_err(_, _, _)) { ret false; }
9981000
}
9991001
}
10001002

@@ -2723,7 +2725,7 @@ fn check_crate(session.session sess, @ast.crate crate) -> typecheck_result {
27232725
auto hasher = hash_unify_cache_entry;
27242726
auto eqer = eq_unify_cache_entry;
27252727
auto unify_cache =
2726-
map.mk_hashmap[unify_cache_entry,ty.unify_result](hasher, eqer);
2728+
map.mk_hashmap[unify_cache_entry,ty.Unify.result](hasher, eqer);
27272729

27282730
auto ccx = @rec(sess=sess,
27292731
type_cache=result._1,

0 commit comments

Comments
 (0)