From d47abfb3a65b88f9668e7dc907eeebcfa99d3459 Mon Sep 17 00:00:00 2001 From: Folkert Date: Mon, 30 Dec 2019 16:52:57 +0100 Subject: [PATCH] preparations for unifying optional fields --- src/types.rs | 9 +++++++++ src/unify.rs | 1 + tests/test_infer.rs | 35 ++++++++++++++++++----------------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/types.rs b/src/types.rs index a0cb6f48aa..9741010b42 100644 --- a/src/types.rs +++ b/src/types.rs @@ -47,6 +47,15 @@ pub enum RecordFieldLabel { Optional(Lowercase), } +impl Into for RecordFieldLabel { + fn into(self) -> Lowercase { + match self { + RecordFieldLabel::Required(label) => label, + RecordFieldLabel::Optional(label) => label, + } + } +} + impl fmt::Debug for RecordFieldLabel { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { diff --git a/src/unify.rs b/src/unify.rs index 394e8d8372..7f91b6e861 100644 --- a/src/unify.rs +++ b/src/unify.rs @@ -173,6 +173,7 @@ fn unify_record( rec1: RecordStructure, rec2: RecordStructure, ) { + // This is a bit more complicated because of optional fields let fields1 = rec1.fields; let fields2 = rec2.fields; let shared_fields = fields1 diff --git a/tests/test_infer.rs b/tests/test_infer.rs index eb8376f658..e5ad0c45e3 100644 --- a/tests/test_infer.rs +++ b/tests/test_infer.rs @@ -980,6 +980,22 @@ mod test_infer { ); } + #[test] + fn record_type_annotation() { + // check that a closed record remains closed + infer_eq( + indoc!( + r#" + foo : { x : custom } -> custom + foo = \{ x } -> x + + foo + "# + ), + "{ x : custom } -> custom", + ); + } + #[test] fn optional_field() { infer_eq( @@ -988,25 +1004,10 @@ mod test_infer { foo : { x? : Int } -> Int foo = \_ -> 42 - foo {} + foo "# ), - "Int", - ); - } - - #[test] - fn record_type_annotation() { - infer_eq( - indoc!( - r#" - foo : { x : custom } -> custom - foo = \{ x } -> x - - foo - "# - ), - "{ x : custom } -> custom", + "{ x? : Int } -> Int", ); } }