Fix types merging in Metavalue::flatten function
@@ -265,13 +265,15 @@ impl MetaValue {
value: _,
} = outer;
+ if types.is_some() {
// If both have type annotations, the result will have the outer one as a type annotation.
// However we still need to enforce the corresponding contract to preserve the operational
// semantics. Thus, the inner type annotation is derelicted to a contract.
match inner.types.take() {
- Some(ctr) if types.is_some() => contracts.push(ctr),
+ Some(ctr) => contracts.push(ctr),
_ => (),
};
+ }
contracts.extend(inner.contracts.into_iter());
@@ -1325,3 +1327,21 @@ pub mod make {
Term::Import(path.into()).into()
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ /// Regression test for issue [#548](https://github.com/tweag/nickel/issues/548)
+ #[test]
+ fn metavalue_flatten() {
+ let mut inner = MetaValue::new();
+ inner.types = Some(Contract {
+ types: Types(AbsType::Num()),
+ label: Label::dummy(),
+ });
+ let outer = MetaValue::new();
+ let res = MetaValue::flatten(outer, inner);
+ assert_ne!(res.types, None);
+ }
+}
