Skip to content

Commit 1956d11

Browse files
committed
refactor to condense common usage pattern
1 parent ab4105d commit 1956d11

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed

src/rustc/middle/typeck.rs

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,9 +1790,9 @@ fn region_env() -> @region_env {
17901790
// Replaces all region parameters in the given type with region variables.
17911791
// This is used when typechecking function calls, bind expressions, and method
17921792
// calls.
1793-
fn universally_quantify_regions(fcx: @fn_ctxt, renv: @region_env, ty: ty::t)
1794-
-> ty::t {
1793+
fn universally_quantify_regions(fcx: @fn_ctxt, ty: ty::t) -> ty::t {
17951794
if ty::type_has_rptrs(ty) {
1795+
let renv = region_env();
17961796
ty::fold_ty(fcx.ccx.tcx, ty::fm_rptr({|r, _under_rptr|
17971797
alt r {
17981798
ty::re_param(param_id) {
@@ -2301,12 +2301,10 @@ fn lookup_method_inner(fcx: @fn_ctxt, expr: @ast::expr,
23012301
self_ty = instantiate_self_regions(fcx.ccx.tcx,
23022302
ty::re_param(next_rid),
23032303
self_ty);
2304-
self_ty = universally_quantify_regions(fcx, region_env(),
2305-
self_ty);
2304+
self_ty = universally_quantify_regions(fcx, self_ty);
23062305

23072306
// ... and "ty" refers to the caller side.
2308-
let ty = universally_quantify_regions(fcx, region_env(),
2309-
ty);
2307+
let ty = universally_quantify_regions(fcx, ty);
23102308

23112309
alt unify::unify(fcx, self_ty, ty) {
23122310
result::ok(_) {
@@ -2440,9 +2438,11 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
24402438

24412439
// A generic function to factor out common logic from call and bind
24422440
// expressions.
2443-
fn check_call_or_bind(fcx: @fn_ctxt, sp: span, fty: ty::t,
2444-
args: [option<@ast::expr>]) -> bool {
2441+
fn check_call_or_bind(
2442+
fcx: @fn_ctxt, sp: span, fty: ty::t,
2443+
args: [option<@ast::expr>]) -> {fty: ty::t, bot: bool} {
24452444

2445+
let fty = universally_quantify_regions(fcx, fty);
24462446
let sty = structure_of(fcx, sp, fty);
24472447
// Grab the argument types
24482448
let mut arg_tys = alt sty {
@@ -2510,7 +2510,10 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
25102510
}
25112511
ret bot;
25122512
};
2513-
ret check_args(false) | check_args(true);
2513+
2514+
let bot = check_args(false) | check_args(true);
2515+
2516+
{fty: fty, bot: bot}
25142517
}
25152518

25162519
// A generic function for checking assignment expressions
@@ -2532,11 +2535,11 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
25322535

25332536
let bot = check_expr(fcx, f);
25342537

2535-
let mut fn_ty = fcx.expr_ty(f);
2536-
fn_ty = universally_quantify_regions(fcx, region_env(), fn_ty);
2537-
25382538
// Call the generic checker.
2539-
ret check_call_or_bind(fcx, sp, fn_ty, args_opt_0) | bot;
2539+
ret {
2540+
let fn_ty = fcx.expr_ty(f);
2541+
check_call_or_bind(fcx, sp, fn_ty, args_opt_0).bot | bot
2542+
};
25402543
}
25412544

25422545
// A generic function for doing all of the checking for call expressions
@@ -2612,10 +2615,10 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
26122615
let callee_id = ast_util::op_expr_callee_id(op_ex);
26132616
alt lookup_method(fcx, op_ex, callee_id, opname, self_t, [], false) {
26142617
some(origin) {
2615-
let method_ty = fcx.node_ty(callee_id);
2616-
let method_ty = universally_quantify_regions(fcx, region_env(),
2617-
method_ty);
2618-
let bot = check_call_or_bind(fcx, op_ex.span, method_ty, args);
2618+
let {fty: method_ty, bot: bot} = {
2619+
let method_ty = fcx.node_ty(callee_id);
2620+
check_call_or_bind(fcx, op_ex.span, method_ty, args)
2621+
};
26192622
fcx.ccx.method_map.insert(op_ex.id, origin);
26202623
some((ty::ty_fn_ret(method_ty), bot))
26212624
}
@@ -2834,18 +2837,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
28342837
let defn = lookup_def(fcx, pth.span, id);
28352838

28362839
let tpt = ty_param_bounds_and_ty_for_def(fcx, expr.span, defn);
2837-
if ty::def_has_ty_params(defn) {
2838-
instantiate_path(fcx, pth, tpt, expr.span, expr.id);
2839-
} else {
2840-
// The definition doesn't take type parameters. If the programmer
2841-
// supplied some, that's an error
2842-
if vec::len::<@ast::ty>(pth.node.types) > 0u {
2843-
tcx.sess.span_fatal(expr.span,
2844-
"this kind of value does not \
2845-
take type parameters");
2846-
}
2847-
fcx.write_ty(id, tpt.ty);
2848-
}
2840+
instantiate_path(fcx, pth, tpt, expr.span, expr.id);
28492841
}
28502842
ast::expr_mac(_) { tcx.sess.bug("unexpanded macro"); }
28512843
ast::expr_fail(expr_opt) {
@@ -3045,10 +3037,10 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
30453037
// Call the generic checker.
30463038
bot = check_expr(fcx, f);
30473039

3048-
let mut fn_ty = fcx.expr_ty(f);
3049-
fn_ty = universally_quantify_regions(fcx, region_env(), fn_ty);
3050-
3051-
let ccob_bot = check_call_or_bind(fcx, expr.span, fn_ty, args);
3040+
let ccob_bot = {
3041+
let fn_ty = fcx.expr_ty(f);
3042+
check_call_or_bind(fcx, expr.span, fn_ty, args).bot
3043+
};
30523044
bot |= ccob_bot;
30533045

30543046
// TODO: Perform substitutions on the return type.

0 commit comments

Comments
 (0)