From f806ad6bf0fad5a720c9b87310d86838612f3b7a Mon Sep 17 00:00:00 2001 From: FintanH Date: Wed, 31 Jul 2019 12:15:12 +0100 Subject: Reuse the merge_maps function to implement the right-biased union --- dhall/src/phase/normalize.rs | 2 +- dhall/src/phase/typecheck.rs | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'dhall/src') diff --git a/dhall/src/phase/normalize.rs b/dhall/src/phase/normalize.rs index be2ba51..da19dc1 100644 --- a/dhall/src/phase/normalize.rs +++ b/dhall/src/phase/normalize.rs @@ -372,7 +372,7 @@ enum Ret<'a> { Expr(ExprF), } -fn merge_maps( +pub(crate) fn merge_maps( map1: &HashMap, map2: &HashMap, mut f: impl FnMut(&V, &V) -> V, diff --git a/dhall/src/phase/typecheck.rs b/dhall/src/phase/typecheck.rs index 2315edb..9107e36 100644 --- a/dhall/src/phase/typecheck.rs +++ b/dhall/src/phase/typecheck.rs @@ -599,6 +599,8 @@ fn type_last_layer( Ok(RetTypeOnly(text_type)) } BinOp(RightBiasedRecordMerge, l, r) => { + use crate::phase::normalize::merge_maps; + let l_type = l.get_type()?; let l_kind = l_type.get_type()?; let r_type = r.get_type()?; @@ -614,7 +616,7 @@ fn type_last_layer( ); // Extract the LHS record type - let mut kts_x = match l_type.to_value() { + let kts_x = match l_type.to_value() { Value::RecordType(kts) => kts, _ => return Err(mkerr(MustCombineRecord(l.clone()))), }; @@ -627,15 +629,13 @@ fn type_last_layer( // Union the two records, prefering // the values found in the RHS. - for (label, value) in kts_y { - kts_x.insert(label, value); - } + let kts = merge_maps(&kts_x, &kts_y, |_, r_t| r_t.clone()); // Construct the final record type from the union Ok(RetTypeOnly(tck_record_type( ctx, - kts_x.iter() - .map(|(x, v)| Ok((x.clone(), v.to_type()))), + kts.iter() + .map(|(x, v)| Ok((x.clone(), v.to_type()))), )? .into_type())) } -- cgit v1.2.3