preparations for unifying optional fields

This commit is contained in:
Folkert 2019-12-30 16:52:57 +01:00
parent 13b3748996
commit d47abfb3a6
3 changed files with 28 additions and 17 deletions

View file

@ -47,6 +47,15 @@ pub enum RecordFieldLabel {
Optional(Lowercase),
}
impl Into<Lowercase> 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 {

View file

@ -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

View file

@ -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",
);
}
}