Skip to content

Commit e6292be

Browse files
committed
---
yaml --- r: 2629 b: refs/heads/master c: 6417610 h: refs/heads/master i: 2627: 7ccf6ea v: v3
1 parent f89f33c commit e6292be

File tree

4 files changed

+47
-40
lines changed

4 files changed

+47
-40
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: c750c520e3d1034274b40d8139a41a10976f2a5d
2+
refs/heads/master: 64176107886f75d92725f9607ffc0dd5e488afff

trunk/src/comp/middle/ty.rs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,18 +1865,19 @@ mod unify {
18651865
ures_err(type_err, t, t);
18661866
}
18671867

1868-
type var_bindings = rec(ufind::ufind sets,
1869-
hashmap[int,uint] var_ids,
1870-
mutable vec[mutable vec[t]] types);
1868+
type bindings[T] = rec(ufind::ufind sets,
1869+
hashmap[T,uint] ids,
1870+
mutable vec[mutable vec[t]] types);
18711871

1872-
fn mk_var_bindings() -> @var_bindings {
1872+
fn mk_bindings[T](map::hashfn[T] hasher, map::eqfn[T] eqer)
1873+
-> @bindings[T] {
18731874
let vec[mutable vec[t]] types = [mutable];
18741875
ret @rec(sets=ufind::make(),
1875-
var_ids=common::new_int_hash[uint](),
1876+
ids=map::mk_hashmap[T,uint](hasher, eqer),
18761877
mutable types=types);
18771878
}
18781879

1879-
type ctxt = rec(@var_bindings var_bindings,
1880+
type ctxt = rec(@bindings[int] bindings,
18801881
unify_handler handler,
18811882
ty_ctxt tcx);
18821883

@@ -2079,10 +2080,10 @@ mod unify {
20792080

20802081
fn get_or_create_set(&@ctxt cx, int id) -> uint {
20812082
auto set_num;
2082-
alt (cx.var_bindings.var_ids.find(id)) {
2083+
alt (cx.bindings.ids.find(id)) {
20832084
case (none[uint]) {
2084-
set_num = ufind::make_set(cx.var_bindings.sets);
2085-
cx.var_bindings.var_ids.insert(id, set_num);
2085+
set_num = ufind::make_set(cx.bindings.sets);
2086+
cx.bindings.ids.insert(id, set_num);
20862087
}
20872088
case (some[uint](?n)) { set_num = n; }
20882089
}
@@ -2107,18 +2108,17 @@ mod unify {
21072108
alt (struct(cx.tcx, expected)) {
21082109
case (ty::ty_var(?expected_id)) {
21092110
auto expected_n = get_or_create_set(cx, expected_id);
2110-
ufind::union(cx.var_bindings.sets, expected_n,
2111-
actual_n);
2111+
ufind::union(cx.bindings.sets, expected_n, actual_n);
21122112
}
21132113

21142114
case (_) {
21152115
// Just bind the type variable to the expected type.
2116-
auto vlen = vec::len[vec[t]](cx.var_bindings.types);
2116+
auto vlen = vec::len[vec[t]](cx.bindings.types);
21172117
if (actual_n < vlen) {
2118-
cx.var_bindings.types.(actual_n) += [expected];
2118+
cx.bindings.types.(actual_n) += [expected];
21192119
} else {
21202120
assert (actual_n == vlen);
2121-
cx.var_bindings.types += [mutable [expected]];
2121+
cx.bindings.types += [mutable [expected]];
21222122
}
21232123
}
21242124
}
@@ -2482,12 +2482,12 @@ mod unify {
24822482
case (ty::ty_var(?expected_id)) {
24832483
// Add a binding.
24842484
auto expected_n = get_or_create_set(cx, expected_id);
2485-
auto vlen = vec::len[vec[t]](cx.var_bindings.types);
2485+
auto vlen = vec::len[vec[t]](cx.bindings.types);
24862486
if (expected_n < vlen) {
2487-
cx.var_bindings.types.(expected_n) += [actual];
2487+
cx.bindings.types.(expected_n) += [actual];
24882488
} else {
24892489
assert (expected_n == vlen);
2490-
cx.var_bindings.types += [mutable [actual]];
2490+
cx.bindings.types += [mutable [actual]];
24912491
}
24922492
ret ures_ok(expected);
24932493
}
@@ -2519,19 +2519,23 @@ mod unify {
25192519
}
25202520

25212521
// Performs type binding substitution.
2522-
fn substitute(&ty_ctxt tcx, &@var_bindings var_bindings,
2523-
&vec[t] set_types, &t typ) -> t {
2522+
fn substitute(&ty_ctxt tcx,
2523+
&@bindings[int] bindings,
2524+
&vec[t] set_types,
2525+
&t typ) -> t {
25242526
if (!type_contains_vars(tcx, typ)) {
25252527
ret typ;
25262528
}
25272529

2528-
fn substituter(ty_ctxt tcx, @var_bindings var_bindings, vec[t] types,
2530+
fn substituter(ty_ctxt tcx,
2531+
@bindings[int] bindings,
2532+
vec[t] types,
25292533
t typ) -> t {
25302534
alt (struct(tcx, typ)) {
25312535
case (ty_var(?id)) {
2532-
alt (var_bindings.var_ids.find(id)) {
2536+
alt (bindings.ids.find(id)) {
25332537
case (some[uint](?n)) {
2534-
auto root = ufind::find(var_bindings.sets, n);
2538+
auto root = ufind::find(bindings.sets, n);
25352539
ret types.(root);
25362540
}
25372541
case (none[uint]) { ret typ; }
@@ -2541,24 +2545,22 @@ mod unify {
25412545
}
25422546
}
25432547

2544-
auto f = bind substituter(tcx, var_bindings, set_types, _);
2548+
auto f = bind substituter(tcx, bindings, set_types, _);
25452549
ret fold_ty(tcx, f, typ);
25462550
}
25472551

2548-
fn unify_sets(&@var_bindings var_bindings) -> vec[t] {
2549-
let vec[t] throwaway = [];
2550-
let vec[mutable vec[t]] set_types = [mutable throwaway];
2551-
vec::pop[vec[t]](set_types); // FIXME: botch
2552+
fn unify_sets[T](&@bindings[T] bindings) -> vec[t] {
2553+
let vec[mutable vec[t]] set_types = [mutable];
25522554

2553-
for (ufind::node node in var_bindings.sets.nodes) {
2555+
for (ufind::node node in bindings.sets.nodes) {
25542556
let vec[t] v = [];
25552557
set_types += [mutable v];
25562558
}
25572559

25582560
auto i = 0u;
25592561
while (i < vec::len[vec[t]](set_types)) {
2560-
auto root = ufind::find(var_bindings.sets, i);
2561-
set_types.(root) += var_bindings.types.(i);
2562+
auto root = ufind::find(bindings.sets, i);
2563+
set_types.(root) += bindings.types.(i);
25622564
i += 1u;
25632565
}
25642566

@@ -2578,15 +2580,15 @@ mod unify {
25782580
fn unify(&t expected,
25792581
&t actual,
25802582
&unify_handler handler,
2581-
&@var_bindings var_bindings,
2583+
&@bindings[int] bindings,
25822584
&ty_ctxt tcx) -> result {
2583-
auto cx = @rec(var_bindings=var_bindings, handler=handler, tcx=tcx);
2585+
auto cx = @rec(bindings=bindings, handler=handler, tcx=tcx);
25842586
ret unify_step(cx, expected, actual);
25852587
}
25862588

2587-
fn fixup(&ty_ctxt tcx, &@var_bindings var_bindings, t typ) -> t {
2588-
auto set_types = unify_sets(var_bindings);
2589-
ret substitute(tcx, var_bindings, set_types, typ);
2589+
fn fixup(&ty_ctxt tcx, &@bindings[int] bindings, t typ) -> t {
2590+
auto set_types = unify_sets[int](bindings);
2591+
ret substitute(tcx, bindings, set_types, typ);
25902592
}
25912593
}
25922594

trunk/src/comp/middle/typeck.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import middle::ty::ty_nil;
3737
import middle::ty::unify::ures_ok;
3838
import middle::ty::unify::ures_err;
3939

40+
import std::int;
4041
import std::str;
4142
import std::uint;
4243
import std::vec;
@@ -969,15 +970,15 @@ mod unify {
969970

970971
auto handler = unify_handler(scx, param_substs);
971972

972-
auto var_bindings = ty::unify::mk_var_bindings();
973-
auto result = ty::unify::unify(expected, actual, handler,
974-
var_bindings, scx.fcx.ccx.tcx);
973+
auto bindings = ty::unify::mk_bindings[int](int::hash, int::eq_alias);
974+
auto result = ty::unify::unify(expected, actual, handler, bindings,
975+
scx.fcx.ccx.tcx);
975976

976977
alt (result) {
977978
case (ures_ok(?rty)) {
978979
if (ty::type_contains_vars(scx.fcx.ccx.tcx, rty)) {
979980
result = ures_ok(ty::unify::fixup(scx.fcx.ccx.tcx,
980-
var_bindings, rty));
981+
bindings, rty));
981982
}
982983
}
983984
case (_) { /* nothing */ }

trunk/src/lib/int.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ fn negative(int x) -> bool { ret x < 0; }
1717
fn nonpositive(int x) -> bool { ret x <= 0; }
1818
fn nonnegative(int x) -> bool { ret x >= 0; }
1919

20+
// FIXME: Make sure this works with negative integers.
21+
fn hash(&int x) -> uint { ret x as uint; }
22+
fn eq_alias(&int x, &int y) -> bool { ret x == y; }
23+
2024
iter range(int lo, int hi) -> int {
2125
let int lo_ = lo;
2226
while (lo_ < hi) {

0 commit comments

Comments
 (0)