From f7a825c6148ab634d2804508118dec939c41136d Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 17 Oct 2011 15:06:19 -0700 Subject: [PATCH] Split record_var_binding into methods for expected and actual This allows unify to maintain the same subtype relationship between expected and actual throughout unify, which we are going to need for mutable? and for function types. --- src/comp/middle/ty.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/comp/middle/ty.rs b/src/comp/middle/ty.rs index 3a4d852f2ade..fec7f4de06ad 100644 --- a/src/comp/middle/ty.rs +++ b/src/comp/middle/ty.rs @@ -1840,13 +1840,35 @@ mod unify { } } } - fn record_var_binding(cx: @ctxt, key: int, typ: t) -> result { + + fn record_var_binding_for_expected( + cx: @ctxt, key: int, typ: t) -> result { + record_var_binding( + cx, key, typ, + fn (cx: @ctxt, old_type: t, new_type: t) -> result { + unify_step(cx, old_type, new_type) + }) + } + + fn record_var_binding_for_actual( + cx: @ctxt, key: int, typ: t) -> result { + record_var_binding( + cx, key, typ, + fn (cx: @ctxt, old_type: t, new_type: t) -> result { + unify_step(cx, new_type, old_type) + }) + } + + fn record_var_binding( + cx: @ctxt, key: int, typ: t, + unify_types: fn(@ctxt, t, t) -> result) -> result { + ufind::grow(cx.vb.sets, (key as uint) + 1u); let root = ufind::find(cx.vb.sets, key as uint); let result_type = typ; alt smallintmap::find::(cx.vb.types, root) { some(old_type) { - alt unify_step(cx, old_type, typ) { + alt unify_types(cx, old_type, typ) { ures_ok(unified_type) { result_type = unified_type; } rs { ret rs; } } @@ -2099,7 +2121,7 @@ mod unify { } _ { // Just bind the type variable to the expected type. - alt record_var_binding(cx, actual_id, expected) { + alt record_var_binding_for_actual(cx, actual_id, expected) { ures_ok(_) {/* fall through */ } rs { ret rs; } } @@ -2113,7 +2135,7 @@ mod unify { ty::ty_var(expected_id) { // Add a binding. (`actual` can't actually be a var here.) - alt record_var_binding(cx, expected_id, actual) { + alt record_var_binding_for_expected(cx, expected_id, actual) { ures_ok(_) {/* fall through */ } rs { ret rs; } }