Skip to content

Commit 8967c4b

Browse files
committed
simplify demand module
1 parent 5c12cd7 commit 8967c4b

File tree

1 file changed

+23
-64
lines changed

1 file changed

+23
-64
lines changed

src/rustc/middle/typeck.rs

Lines changed: 23 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,59 +1402,13 @@ fn require_same_types(
14021402
}
14031403
14041404
mod demand {
1405-
fn simple(fcx: @fn_ctxt, sp: span, expected: ty::t, actual: ty::t) ->
1406-
ty::t {
1407-
full(fcx, sp, unify::unify, expected, actual, []).ty
1408-
}
1409-
1410-
// n.b.: order of arguments is reversed.
1411-
fn subty(fcx: @fn_ctxt, sp: span, actual: ty::t, expected: ty::t) {
1412-
full(fcx, sp, unify::unify, expected, actual, []);
1413-
}
1414-
1415-
fn with_substs(fcx: @fn_ctxt, sp: span, expected: ty::t, actual: ty::t,
1416-
ty_param_substs_0: [ty::t]) -> ty_param_substs_and_ty {
1417-
full(fcx, sp, unify::unify, expected, actual, ty_param_substs_0)
1418-
}
1419-
14201405
// Requires that the two types unify, and prints an error message if they
1421-
// don't. Returns the unified type and the type parameter substitutions.
1422-
fn full(fcx: @fn_ctxt,
1423-
sp: span,
1424-
unifier: fn@(@fn_ctxt, ty::t, ty::t)
1425-
-> result<(), ty::type_err>,
1426-
expected: ty::t,
1427-
actual: ty::t,
1428-
ty_param_substs_0: [ty::t]) ->
1429-
ty_param_substs_and_ty {
1430-
1431-
let mut ty_param_substs: [mut ty::t] = [mut];
1432-
let mut ty_param_subst_var_ids: [ty_vid] = [];
1433-
for ty_param_substs_0.each {|ty_param_subst|
1434-
// Generate a type variable and unify it with the type parameter
1435-
// substitution. We will then pull out these type variables.
1436-
let t_0 = next_ty_var(fcx);
1437-
ty_param_substs += [mut t_0];
1438-
ty_param_subst_var_ids += [ty::ty_var_id(t_0)];
1439-
simple(fcx, sp, ty_param_subst, t_0);
1440-
}
1441-
1442-
fn mk_result(fcx: @fn_ctxt, result_ty: ty::t,
1443-
ty_param_subst_var_ids: [ty_vid]) ->
1444-
ty_param_substs_and_ty {
1445-
let mut result_ty_param_substs: [ty::t] = [];
1446-
for ty_param_subst_var_ids.each {|var_id|
1447-
let tp_subst = ty::mk_var(fcx.ccx.tcx, var_id);
1448-
result_ty_param_substs += [tp_subst];
1449-
}
1450-
ret {substs: result_ty_param_substs, ty: result_ty};
1451-
}
1406+
// don't.
1407+
fn simple(fcx: @fn_ctxt, sp: span,
1408+
expected: ty::t, actual: ty::t) {
14521409
1453-
1454-
alt unifier(fcx, expected, actual) {
1455-
result::ok(()) {
1456-
ret mk_result(fcx, expected, ty_param_subst_var_ids);
1457-
}
1410+
alt infer::mk_subty(fcx.infcx, actual, expected) {
1411+
result::ok(()) { /* ok */ }
14581412
result::err(err) {
14591413
fcx.ccx.tcx.sess.span_err(sp,
14601414
"mismatched types: expected `" +
@@ -1465,7 +1419,6 @@ mod demand {
14651419
ty::type_err_to_str(
14661420
fcx.ccx.tcx, err) +
14671421
")");
1468-
ret mk_result(fcx, expected, ty_param_subst_var_ids);
14691422
}
14701423
}
14711424
}
@@ -1938,20 +1891,27 @@ fn universally_quantify_before_call(
19381891

19391892
fn check_pat_variant(pcx: pat_ctxt, pat: @ast::pat, path: @ast::path,
19401893
subpats: [@ast::pat], expected: ty::t) {
1894+
19411895
// Typecheck the path.
19421896
let fcx = pcx.fcx;
19431897
let tcx = pcx.fcx.ccx.tcx;
1898+
1899+
// Lookup the enum and variant def ids:
19441900
let v_def = lookup_def(pcx.fcx, path.span, pat.id);
19451901
let v_def_ids = ast_util::variant_def_ids(v_def);
1946-
let ctor_tpt = ty::lookup_item_type(tcx, v_def_ids.enm);
1947-
instantiate_path(pcx.fcx, path, ctor_tpt, pat.span, pat.id);
1902+
1903+
// Assign the pattern the type of the *enum*, not the variant.
1904+
let enum_tpt = ty::lookup_item_type(tcx, v_def_ids.enm);
1905+
instantiate_path(pcx.fcx, path, enum_tpt, pat.span, pat.id);
19481906

19491907
// Take the enum type params out of `expected`.
19501908
alt structure_of(pcx.fcx, pat.span, expected) {
19511909
ty::ty_enum(_, expected_tps) {
1952-
let ctor_ty = fcx.node_ty(pat.id);
1953-
demand::with_substs(pcx.fcx, pat.span, expected, ctor_ty,
1954-
expected_tps);
1910+
// check that the type of the value being matched is a subtype
1911+
// of the type of the pattern:
1912+
let pat_ty = fcx.node_ty(pat.id);
1913+
demand::simple(fcx, pat.span, pat_ty, expected);
1914+
19551915
// Get the number of arguments in this enum variant.
19561916
let arg_types = variant_arg_types(pcx.fcx.ccx, pat.span,
19571917
v_def_ids.var, expected_tps);
@@ -2029,12 +1989,12 @@ fn check_pat(pcx: pat_ctxt, pat: @ast::pat, expected: ty::t) {
20291989
if !pat_util::pat_is_variant(tcx.def_map, pat) {
20301990
let vid = lookup_local(pcx.fcx, pat.span, pat.id);
20311991
let mut typ = ty::mk_var(tcx, vid);
2032-
typ = demand::simple(pcx.fcx, pat.span, expected, typ);
1992+
demand::simple(pcx.fcx, pat.span, expected, typ);
20331993
let canon_id = pcx.map.get(path_to_ident(name));
20341994
if canon_id != pat.id {
20351995
let tv_id = lookup_local(pcx.fcx, pat.span, canon_id);
20361996
let ct = ty::mk_var(tcx, tv_id);
2037-
typ = demand::simple(pcx.fcx, pat.span, ct, typ);
1997+
demand::simple(pcx.fcx, pat.span, ct, typ);
20381998
}
20391999
fcx.write_ty(pat.id, typ);
20402000
alt sub {
@@ -2203,12 +2163,11 @@ fn require_pure_call(ccx: @crate_ctxt, caller_purity: ast::purity,
22032163
}
22042164
}
22052165

2206-
type unifier = fn@(@fn_ctxt, span, ty::t, ty::t) -> ty::t;
2166+
type unifier = fn@(@fn_ctxt, span, ty::t, ty::t);
22072167

22082168
fn check_expr(fcx: @fn_ctxt, expr: @ast::expr) -> bool {
2209-
fn dummy_unify(_fcx: @fn_ctxt, _sp: span, _expected: ty::t, actual: ty::t)
2210-
-> ty::t {
2211-
actual
2169+
fn dummy_unify(_fcx: @fn_ctxt, _sp: span,
2170+
_expected: ty::t, _actual: ty::t) {
22122171
}
22132172
ret check_expr_with_unifier(fcx, expr, dummy_unify,
22142173
ty::mk_nil(fcx.ccx.tcx));
@@ -3139,7 +3098,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
31393098
}
31403099
if !check_block(fcx, arm.body) { arm_non_bot = true; }
31413100
let bty = fcx.node_ty(arm.body.node.id);
3142-
result_ty = demand::simple(fcx, arm.body.span, result_ty, bty);
3101+
demand::simple(fcx, arm.body.span, result_ty, bty);
31433102
}
31443103
bot |= !arm_non_bot;
31453104
if !arm_non_bot { result_ty = ty::mk_bot(tcx); }

0 commit comments

Comments
 (0)