Skip to content

Commit d68f1a8

Browse files
committedAug 15, 2011
Implement type inference for type-inferred blocks.
1 parent 9b50011 commit d68f1a8

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed
 

Diff for: ‎src/comp/middle/typeck.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -2051,13 +2051,23 @@ fn check_expr_with_unifier(fcx: &@fn_ctxt, expr: &@ast::expr,
20512051
}
20522052
ast::expr_fn(f) {
20532053
let cx = @{tcx: tcx};
2054-
let convert =
2055-
bind ast_ty_to_ty(cx.tcx, bind collect::getter(cx, _), _);
2056-
let ty_of_arg = bind collect::ty_of_arg(cx, _);
2054+
let convert = bind ast_ty_to_ty_crate_tyvar(fcx, _);
2055+
let ty_of_arg = lambda(a: &ast::arg) -> ty::arg {
2056+
let ty_mode = ast_mode_to_mode(a.mode);
2057+
let tt = ast_ty_to_ty_crate_tyvar(fcx, a.ty);
2058+
ret {mode: ty_mode, ty: tt};
2059+
};
20572060
let fty =
20582061
collect::ty_of_fn_decl(cx, convert, ty_of_arg, f.decl, f.proto,
20592062
~[], none).ty;
20602063
write::ty_only_fixup(fcx, id, fty);
2064+
2065+
// Unify the type of the function with the expected type before we
2066+
// typecheck the body so that we have more information about the
2067+
// argument types in the body. This is needed to make binops and
2068+
// record projection work on type inferred arguments.
2069+
unify(fcx, expr.span, expected, fty);
2070+
20612071
check_fn(fcx.ccx, f, id, some(fcx));
20622072
}
20632073
ast::expr_block(b) {
@@ -2611,7 +2621,7 @@ fn check_fn(ccx: &@crate_ctxt, f: &ast::_fn, id: &ast::node_id,
26112621
let gather_result = gather_locals(ccx, f, id, old_fcx);
26122622
let fixups: [ast::node_id] = ~[];
26132623
let fcx: @fn_ctxt =
2614-
@{ret_ty: ast_ty_to_ty_crate(ccx, decl.output),
2624+
@{ret_ty: ty::ty_fn_ret(ccx.tcx, ty::node_id_to_type(ccx.tcx, id)),
26152625
purity: decl.purity,
26162626
proto: f.proto,
26172627
var_bindings: gather_result.var_bindings,

0 commit comments

Comments
 (0)