@@ -21,7 +21,6 @@ import middle::ty::mo_alias;
21
21
import middle:: ty:: node_type_table;
22
22
import middle:: ty:: pat_ty;
23
23
import middle:: ty:: path_to_str;
24
- import middle:: ty:: struct;
25
24
import middle:: ty:: ty_param_substs_opt_and_ty;
26
25
import middle:: ty:: ty_to_str;
27
26
import middle:: ty:: type_is_integral;
@@ -195,6 +194,19 @@ fn ast_mode_to_mode(ast::mode mode) -> ty::mode {
195
194
ret ty_mode;
196
195
}
197
196
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
+
198
210
// Parses the programmer's textual representation of a type into our internal
199
211
// notion of a type. `getter` is a function that returns the type
200
212
// corresponding to a definition ID:
@@ -810,20 +822,11 @@ mod collect {
810
822
811
823
// Type unification
812
824
825
+ // TODO: rename to just "unify"
813
826
mod unify {
814
827
fn simple( & @fn_ctxt fcx , & ty:: t expected , & ty:: t actual )
815
828
-> 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) ;
827
830
}
828
831
}
829
832
@@ -833,10 +836,10 @@ tag autoderef_kind {
833
836
NO_AUTODEREF;
834
837
}
835
838
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 {
837
840
auto t1 = t;
838
841
while ( true) {
839
- alt ( struct ( tcx , t1) ) {
842
+ alt ( structure_of ( fcx , sp , t1 ) ) {
840
843
case ( ty:: ty_box ( ?inner ) ) { t1 = inner. ty; }
841
844
case ( _) { ret t1; }
842
845
}
@@ -854,11 +857,11 @@ fn add_boxes(&@crate_ctxt ccx, uint n, &ty::t t) -> ty::t {
854
857
}
855
858
856
859
857
- fn count_boxes( & ty :: ctxt tcx , & ty:: t t) -> uint {
860
+ fn count_boxes( & @fn_ctxt fcx , & span sp , & ty:: t t) -> uint {
858
861
auto n = 0 u;
859
862
auto t1 = t;
860
863
while ( true ) {
861
- alt ( struct ( tcx , t1) ) {
864
+ alt ( structure_of ( fcx , sp , t1) ) {
862
865
case ( ty:: ty_box( ?inner) ) { n += 1 u; t1 = inner. ty; }
863
866
case ( _) { ret n; }
864
867
}
@@ -897,9 +900,9 @@ mod demand {
897
900
auto implicit_boxes = 0 u;
898
901
899
902
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) ;
903
906
}
904
907
905
908
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,
958
961
let vec[ ty:: t] result = [ ] ;
959
962
960
963
auto tpt = ty:: lookup_item_type( ccx. tcx, vid) ;
961
- alt ( struct ( ccx. tcx, tpt. _1) ) {
964
+ alt ( ty :: struct ( ccx. tcx, tpt. _1) ) {
962
965
case ( ty:: ty_fn( _, ?ins, _, _) ) {
963
966
// N-ary variant.
964
967
for ( ty:: arg arg in ins) {
@@ -1272,7 +1275,7 @@ fn check_pat(&@fn_ctxt fcx, &@ast::pat pat, ty::t expected) {
1272
1275
1273
1276
// Take the tag type params out of `expected`.
1274
1277
auto expected_tps;
1275
- alt ( struct ( fcx. ccx . tcx , expected) ) {
1278
+ alt ( structure_of ( fcx, pat . span , expected) ) {
1276
1279
case ( ty:: ty_tag( _, ?tps) ) { expected_tps = tps; }
1277
1280
case ( _) {
1278
1281
// FIXME: Switch expected and actual in this message? I
@@ -1405,14 +1408,12 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
1405
1408
// Check the function.
1406
1409
check_expr( fcx, f) ;
1407
1410
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.
1410
1412
auto fty = expr_ty( fcx. ccx. tcx, f) ;
1411
- fty = ty:: unify:: resolve_all_vars( fcx. ccx. tcx, fcx. var_bindings, fty) ;
1412
1413
1413
1414
// Grab the argument types and the return type.
1414
1415
auto arg_tys;
1415
- alt ( ty :: struct ( fcx. ccx . tcx , fty) ) {
1416
+ alt ( structure_of ( fcx, sp , fty) ) {
1416
1417
case ( ty:: ty_fn( _, ?arg_tys_0, _, _) ) {
1417
1418
arg_tys = arg_tys_0;
1418
1419
}
@@ -1508,7 +1509,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
1508
1509
auto lhs_t = expr_ty( fcx. ccx. tcx, lhs) ;
1509
1510
1510
1511
// 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) ;
1512
1513
alt ( binop) {
1513
1514
case ( ast:: eq) { t = ty:: mk_bool( fcx. ccx. tcx) ; }
1514
1515
case ( ast:: lt) { t = ty:: mk_bool( fcx. ccx. tcx) ; }
@@ -1532,7 +1533,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
1532
1533
rec( ty=oper_t, mut =mut ) ) ;
1533
1534
}
1534
1535
case ( ast:: deref) {
1535
- alt ( struct ( fcx. ccx . tcx , oper_t) ) {
1536
+ alt ( structure_of ( fcx, expr . span , oper_t) ) {
1536
1537
case ( ty:: ty_box( ?inner) ) { oper_t = inner. ty; }
1537
1538
case ( _) {
1538
1539
fcx. ccx. tcx. sess. span_err
@@ -1542,7 +1543,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
1542
1543
}
1543
1544
}
1544
1545
}
1545
- case ( _) { oper_t = strip_boxes( fcx. ccx . tcx , oper_t) ; }
1546
+ case ( _) { oper_t = strip_boxes( fcx, expr . span , oper_t) ; }
1546
1547
}
1547
1548
1548
1549
write:: ty_only_fixup( fcx, a. id, oper_t) ;
@@ -1721,7 +1722,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
1721
1722
1722
1723
auto item_t;
1723
1724
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) ) {
1725
1726
case ( ty:: ty_chan( ?it) ) { item_t = it; }
1726
1727
case ( _) {
1727
1728
fcx. ccx. tcx. sess. span_err( expr. span,
@@ -1775,8 +1776,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
1775
1776
1776
1777
case ( ast:: expr_for( ?decl, ?seq, ?body, ?a) ) {
1777
1778
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) ) ) {
1780
1780
// FIXME: I include the check_for_or_each call in
1781
1781
// each case because of a bug in typestate.
1782
1782
// The bug is fixed; once there's a new snapshot,
@@ -1877,7 +1877,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
1877
1877
auto rt_1;
1878
1878
auto fty = expr_ty( fcx. ccx. tcx, f) ;
1879
1879
auto t_1;
1880
- alt ( struct ( fcx. ccx . tcx , fty) ) {
1880
+ alt ( structure_of ( fcx, expr . span , fty) ) {
1881
1881
case ( ty:: ty_fn( ?proto, ?arg_tys, ?rt, ?cf) ) {
1882
1882
proto_1 = proto;
1883
1883
rt_1 = rt;
@@ -1917,7 +1917,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
1917
1917
// Pull the return type out of the type of the function.
1918
1918
auto rt_1 = ty:: mk_nil( fcx. ccx. tcx) ; // FIXME: typestate botch
1919
1919
auto fty = ty:: expr_ty( fcx. ccx. tcx, f) ;
1920
- alt ( struct ( fcx. ccx . tcx , fty) ) {
1920
+ alt ( structure_of ( fcx, expr . span , fty) ) {
1921
1921
case ( ty:: ty_fn( _, _, ?rt, _) ) { rt_1 = rt; }
1922
1922
case ( ty:: ty_native_fn( _, _, ?rt) ) { rt_1 = rt; }
1923
1923
case ( _) {
@@ -1949,7 +1949,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
1949
1949
}
1950
1950
1951
1951
// 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) ) {
1953
1953
case ( ty:: ty_obj( ?methods) ) {
1954
1954
for ( ty:: method method in methods) {
1955
1955
if ( method. ident == id) {
@@ -2059,7 +2059,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
2059
2059
2060
2060
let vec[ field] base_fields = [ ] ;
2061
2061
2062
- alt ( struct ( fcx. ccx . tcx , bexpr_t) ) {
2062
+ alt ( structure_of ( fcx, expr . span , bexpr_t) ) {
2063
2063
case ( ty:: ty_rec( ?flds) ) { base_fields = flds; }
2064
2064
case ( _) {
2065
2065
fcx. ccx. tcx. sess. span_err
@@ -2093,10 +2093,10 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
2093
2093
case ( ast:: expr_field( ?base, ?field, ?a) ) {
2094
2094
check_expr( fcx, base) ;
2095
2095
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) ;
2097
2097
base_t = ty:: unify:: resolve_all_vars( fcx. ccx. tcx,
2098
2098
fcx. var_bindings, base_t) ;
2099
- alt ( struct ( fcx. ccx . tcx , base_t) ) {
2099
+ alt ( structure_of ( fcx, expr . span , base_t) ) {
2100
2100
case ( ty:: ty_tup( ?args) ) {
2101
2101
let uint ix = ty:: field_num( fcx. ccx. tcx. sess,
2102
2102
expr. span, field) ;
@@ -2142,11 +2142,11 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
2142
2142
case ( ast:: expr_index( ?base, ?idx, ?a) ) {
2143
2143
check_expr( fcx, base) ;
2144
2144
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) ;
2146
2146
2147
2147
check_expr( fcx, idx) ;
2148
2148
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) ) {
2150
2150
case ( ty:: ty_vec( ?mt) ) {
2151
2151
if ( ! type_is_integral( fcx. ccx. tcx, idx_t) ) {
2152
2152
fcx. ccx. tcx. sess. span_err
@@ -2184,7 +2184,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
2184
2184
case ( ast:: expr_chan( ?x, ?a) ) {
2185
2185
check_expr( fcx, x) ;
2186
2186
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) ) {
2188
2188
case ( ty:: ty_port( ?subtype) ) {
2189
2189
auto ct = ty:: mk_chan( fcx. ccx. tcx, subtype) ;
2190
2190
write:: ty_only_fixup( fcx, a. id, ct) ;
0 commit comments