Skip to content

Commit 585b70d

Browse files
committed
---
yaml --- r: 2983 b: refs/heads/master c: 6feb1df h: refs/heads/master i: 2981: 32282ce 2979: 5b1dc87 2975: b75ff19 v: v3
1 parent 4dd2654 commit 585b70d

File tree

3 files changed

+60
-41
lines changed

3 files changed

+60
-41
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: 537d4ac65a49c4d9932cc8871ffdb9020b5ac05d
2+
refs/heads/master: 6feb1dfd7672838fe3a621250a832f97c39f2260

trunk/src/comp/middle/ty.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,6 +2126,25 @@ mod unify {
21262126
ret fold_ty(tcx, bind folder(tcx, vb, _), typ);
21272127
}
21282128

2129+
// If the given type is a variable, returns the structure of that type.
2130+
fn resolve_type_structure(&ty_ctxt tcx, &@var_bindings vb, t typ)
2131+
-> fixup_result {
2132+
alt (struct(tcx, typ)) {
2133+
case (ty_var(?vid)) {
2134+
if ((vid as uint) >= ufind::set_count(vb.sets)) {
2135+
ret fix_err(vid);
2136+
}
2137+
2138+
auto root_id = ufind::find(vb.sets, vid as uint);
2139+
alt (smallintmap::find[t](vb.types, root_id)) {
2140+
case (none[t]) { ret fix_err(vid); }
2141+
case (some[t](?rt)) { ret fix_ok(rt); }
2142+
}
2143+
}
2144+
case (_) { ret fix_ok(typ); }
2145+
}
2146+
}
2147+
21292148
fn unify_step(&@ctxt cx, &t expected, &t actual) -> result {
21302149
// TODO: rewrite this using tuple pattern matching when available, to
21312150
// avoid all this rightward drift and spikiness.

trunk/src/comp/middle/typeck.rs

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import middle::ty::mo_alias;
2121
import middle::ty::node_type_table;
2222
import middle::ty::pat_ty;
2323
import middle::ty::path_to_str;
24-
import middle::ty::struct;
2524
import middle::ty::ty_param_substs_opt_and_ty;
2625
import middle::ty::ty_to_str;
2726
import middle::ty::type_is_integral;
@@ -195,6 +194,19 @@ fn ast_mode_to_mode(ast::mode mode) -> ty::mode {
195194
ret ty_mode;
196195
}
197196

197+
// Returns the one-level-deep structure of the given type.
198+
fn structure_of(&@fn_ctxt fcx, &span sp, ty::t typ) -> ty::sty {
199+
auto r = ty::unify::resolve_type_structure(fcx.ccx.tcx, fcx.var_bindings,
200+
typ);
201+
alt (r) {
202+
case (fix_ok(?typ_s)) { ret ty::struct(fcx.ccx.tcx, typ_s); }
203+
case (fix_err(_)) {
204+
fcx.ccx.tcx.sess.span_err(sp, "the type of this value must be " +
205+
"known in this context");
206+
}
207+
}
208+
}
209+
198210
// Parses the programmer's textual representation of a type into our internal
199211
// notion of a type. `getter` is a function that returns the type
200212
// corresponding to a definition ID:
@@ -810,20 +822,11 @@ mod collect {
810822

811823
// Type unification
812824

825+
// TODO: rename to just "unify"
813826
mod unify {
814827
fn simple(&@fn_ctxt fcx, &ty::t expected, &ty::t actual)
815828
-> ty::unify::result {
816-
auto result = ty::unify::unify(expected, actual, fcx.var_bindings,
817-
fcx.ccx.tcx);
818-
819-
// FIXME: Shouldn't be necessary, but is until we remove pushdown.
820-
alt (result) {
821-
case (ures_ok(?typ)) {
822-
ret ures_ok(ty::unify::resolve_all_vars(fcx.ccx.tcx,
823-
fcx.var_bindings, typ));
824-
}
825-
case (_) { ret result; }
826-
}
829+
ret ty::unify::unify(expected, actual, fcx.var_bindings, fcx.ccx.tcx);
827830
}
828831
}
829832

@@ -833,10 +836,10 @@ tag autoderef_kind {
833836
NO_AUTODEREF;
834837
}
835838

836-
fn strip_boxes(&ty::ctxt tcx, &ty::t t) -> ty::t {
839+
fn strip_boxes(&@fn_ctxt fcx, &span sp, &ty::t t) -> ty::t {
837840
auto t1 = t;
838841
while (true) {
839-
alt (struct(tcx, t1)) {
842+
alt (structure_of(fcx, sp, t1)) {
840843
case (ty::ty_box(?inner)) { t1 = inner.ty; }
841844
case (_) { ret t1; }
842845
}
@@ -854,11 +857,11 @@ fn add_boxes(&@crate_ctxt ccx, uint n, &ty::t t) -> ty::t {
854857
}
855858

856859

857-
fn count_boxes(&ty::ctxt tcx, &ty::t t) -> uint {
860+
fn count_boxes(&@fn_ctxt fcx, &span sp, &ty::t t) -> uint {
858861
auto n = 0u;
859862
auto t1 = t;
860863
while (true) {
861-
alt (struct(tcx, t1)) {
864+
alt (structure_of(fcx, sp, t1)) {
862865
case (ty::ty_box(?inner)) { n += 1u; t1 = inner.ty; }
863866
case (_) { ret n; }
864867
}
@@ -897,9 +900,9 @@ mod demand {
897900
auto implicit_boxes = 0u;
898901

899902
if (adk == AUTODEREF_OK) {
900-
expected_1 = strip_boxes(fcx.ccx.tcx, expected_1);
901-
actual_1 = strip_boxes(fcx.ccx.tcx, actual_1);
902-
implicit_boxes = count_boxes(fcx.ccx.tcx, actual);
903+
expected_1 = strip_boxes(fcx, sp, expected_1);
904+
actual_1 = strip_boxes(fcx, sp, actual_1);
905+
implicit_boxes = count_boxes(fcx, sp, actual);
903906
}
904907

905908
let vec[mutable ty::t] ty_param_substs = [mutable];
@@ -958,7 +961,7 @@ fn variant_arg_types(&@crate_ctxt ccx, &span sp, &ast::def_id vid,
958961
let vec[ty::t] result = [];
959962

960963
auto tpt = ty::lookup_item_type(ccx.tcx, vid);
961-
alt (struct(ccx.tcx, tpt._1)) {
964+
alt (ty::struct(ccx.tcx, tpt._1)) {
962965
case (ty::ty_fn(_, ?ins, _, _)) {
963966
// N-ary variant.
964967
for (ty::arg arg in ins) {
@@ -1272,7 +1275,7 @@ fn check_pat(&@fn_ctxt fcx, &@ast::pat pat, ty::t expected) {
12721275

12731276
// Take the tag type params out of `expected`.
12741277
auto expected_tps;
1275-
alt (struct(fcx.ccx.tcx, expected)) {
1278+
alt (structure_of(fcx, pat.span, expected)) {
12761279
case (ty::ty_tag(_, ?tps)) { expected_tps = tps; }
12771280
case (_) {
12781281
// FIXME: Switch expected and actual in this message? I
@@ -1405,14 +1408,12 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
14051408
// Check the function.
14061409
check_expr(fcx, f);
14071410

1408-
// Get the function type. We need to have resolved it enough to know
1409-
// it's a ty_fn or ty_native_fn.
1411+
// Get the function type.
14101412
auto fty = expr_ty(fcx.ccx.tcx, f);
1411-
fty = ty::unify::resolve_all_vars(fcx.ccx.tcx, fcx.var_bindings, fty);
14121413

14131414
// Grab the argument types and the return type.
14141415
auto arg_tys;
1415-
alt (ty::struct(fcx.ccx.tcx, fty)) {
1416+
alt (structure_of(fcx, sp, fty)) {
14161417
case (ty::ty_fn(_, ?arg_tys_0, _, _)) {
14171418
arg_tys = arg_tys_0;
14181419
}
@@ -1508,7 +1509,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
15081509
auto lhs_t = expr_ty(fcx.ccx.tcx, lhs);
15091510

15101511
// FIXME: Binops have a bit more subtlety than this.
1511-
auto t = strip_boxes(fcx.ccx.tcx, lhs_t);
1512+
auto t = strip_boxes(fcx, expr.span, lhs_t);
15121513
alt (binop) {
15131514
case (ast::eq) { t = ty::mk_bool(fcx.ccx.tcx); }
15141515
case (ast::lt) { t = ty::mk_bool(fcx.ccx.tcx); }
@@ -1532,7 +1533,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
15321533
rec(ty=oper_t, mut=mut));
15331534
}
15341535
case (ast::deref) {
1535-
alt (struct(fcx.ccx.tcx, oper_t)) {
1536+
alt (structure_of(fcx, expr.span, oper_t)) {
15361537
case (ty::ty_box(?inner)) { oper_t = inner.ty; }
15371538
case (_) {
15381539
fcx.ccx.tcx.sess.span_err
@@ -1542,7 +1543,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
15421543
}
15431544
}
15441545
}
1545-
case (_) { oper_t = strip_boxes(fcx.ccx.tcx, oper_t); }
1546+
case (_) { oper_t = strip_boxes(fcx, expr.span, oper_t); }
15461547
}
15471548

15481549
write::ty_only_fixup(fcx, a.id, oper_t);
@@ -1721,7 +1722,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
17211722

17221723
auto item_t;
17231724
auto lhs_t = expr_ty(fcx.ccx.tcx, lhs);
1724-
alt (struct(fcx.ccx.tcx, lhs_t)) {
1725+
alt (structure_of(fcx, expr.span, lhs_t)) {
17251726
case (ty::ty_chan(?it)) { item_t = it; }
17261727
case (_) {
17271728
fcx.ccx.tcx.sess.span_err(expr.span,
@@ -1775,8 +1776,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
17751776

17761777
case (ast::expr_for(?decl, ?seq, ?body, ?a)) {
17771778
check_expr(fcx, seq);
1778-
alt (struct (fcx.ccx.tcx,
1779-
expr_ty(fcx.ccx.tcx, seq))) {
1779+
alt (structure_of(fcx, expr.span, expr_ty(fcx.ccx.tcx, seq))) {
17801780
// FIXME: I include the check_for_or_each call in
17811781
// each case because of a bug in typestate.
17821782
// The bug is fixed; once there's a new snapshot,
@@ -1877,7 +1877,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
18771877
auto rt_1;
18781878
auto fty = expr_ty(fcx.ccx.tcx, f);
18791879
auto t_1;
1880-
alt (struct(fcx.ccx.tcx, fty)) {
1880+
alt (structure_of(fcx, expr.span, fty)) {
18811881
case (ty::ty_fn(?proto, ?arg_tys, ?rt, ?cf)) {
18821882
proto_1 = proto;
18831883
rt_1 = rt;
@@ -1917,7 +1917,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
19171917
// Pull the return type out of the type of the function.
19181918
auto rt_1 = ty::mk_nil(fcx.ccx.tcx); // FIXME: typestate botch
19191919
auto fty = ty::expr_ty(fcx.ccx.tcx, f);
1920-
alt (struct(fcx.ccx.tcx, fty)) {
1920+
alt (structure_of(fcx, expr.span, fty)) {
19211921
case (ty::ty_fn(_,_,?rt,_)) { rt_1 = rt; }
19221922
case (ty::ty_native_fn(_, _, ?rt)) { rt_1 = rt; }
19231923
case (_) {
@@ -1949,7 +1949,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
19491949
}
19501950

19511951
// Grab this method's type out of the current object type.
1952-
alt (struct(fcx.ccx.tcx, this_obj_ty)) {
1952+
alt (structure_of(fcx, expr.span, this_obj_ty)) {
19531953
case (ty::ty_obj(?methods)) {
19541954
for (ty::method method in methods) {
19551955
if (method.ident == id) {
@@ -2059,7 +2059,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
20592059

20602060
let vec[field] base_fields = [];
20612061

2062-
alt (struct(fcx.ccx.tcx, bexpr_t)) {
2062+
alt (structure_of(fcx, expr.span, bexpr_t)) {
20632063
case (ty::ty_rec(?flds)) { base_fields = flds; }
20642064
case (_) {
20652065
fcx.ccx.tcx.sess.span_err
@@ -2093,10 +2093,10 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
20932093
case (ast::expr_field(?base, ?field, ?a)) {
20942094
check_expr(fcx, base);
20952095
auto base_t = expr_ty(fcx.ccx.tcx, base);
2096-
base_t = strip_boxes(fcx.ccx.tcx, base_t);
2096+
base_t = strip_boxes(fcx, expr.span, base_t);
20972097
base_t = ty::unify::resolve_all_vars(fcx.ccx.tcx,
20982098
fcx.var_bindings, base_t);
2099-
alt (struct(fcx.ccx.tcx, base_t)) {
2099+
alt (structure_of(fcx, expr.span, base_t)) {
21002100
case (ty::ty_tup(?args)) {
21012101
let uint ix = ty::field_num(fcx.ccx.tcx.sess,
21022102
expr.span, field);
@@ -2142,11 +2142,11 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
21422142
case (ast::expr_index(?base, ?idx, ?a)) {
21432143
check_expr(fcx, base);
21442144
auto base_t = expr_ty(fcx.ccx.tcx, base);
2145-
base_t = strip_boxes(fcx.ccx.tcx, base_t);
2145+
base_t = strip_boxes(fcx, expr.span, base_t);
21462146

21472147
check_expr(fcx, idx);
21482148
auto idx_t = expr_ty(fcx.ccx.tcx, idx);
2149-
alt (struct(fcx.ccx.tcx, base_t)) {
2149+
alt (structure_of(fcx, expr.span, base_t)) {
21502150
case (ty::ty_vec(?mt)) {
21512151
if (! type_is_integral(fcx.ccx.tcx, idx_t)) {
21522152
fcx.ccx.tcx.sess.span_err
@@ -2184,7 +2184,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
21842184
case (ast::expr_chan(?x, ?a)) {
21852185
check_expr(fcx, x);
21862186
auto port_t = expr_ty(fcx.ccx.tcx, x);
2187-
alt (struct(fcx.ccx.tcx, port_t)) {
2187+
alt (structure_of(fcx, expr.span, port_t)) {
21882188
case (ty::ty_port(?subtype)) {
21892189
auto ct = ty::mk_chan(fcx.ccx.tcx, subtype);
21902190
write::ty_only_fixup(fcx, a.id, ct);

0 commit comments

Comments
 (0)