mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-27 22:09:09 +00:00
Fix unifying optional fields
This commit is contained in:
parent
eb3ee8ffa3
commit
98a8bb8115
2 changed files with 43 additions and 2 deletions
|
@ -2565,4 +2565,34 @@ mod solve_expr {
|
|||
"should fail",
|
||||
);
|
||||
}
|
||||
|
||||
// OPTIONAL RECORD FIELDS
|
||||
|
||||
#[test]
|
||||
fn optional_field_unifies_with_missing() {
|
||||
infer_eq_without_problem(
|
||||
indoc!(
|
||||
r#"
|
||||
negatePoint : { x : Int, y : Int, z ? Num c } -> { x : Int, y : Int, z : Num c }
|
||||
|
||||
negatePoint { x: 1, y: 2 }
|
||||
"#
|
||||
),
|
||||
"{ x : Int, y : Int, z : Num c }",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn optional_field_unifies_with_present() {
|
||||
infer_eq_without_problem(
|
||||
indoc!(
|
||||
r#"
|
||||
negatePoint : { x : Num a, y : Num b, z ? c } -> { x : Num a, y : Num b, z : c }
|
||||
|
||||
negatePoint { x: 1, y: 2.1, z: 0x3 }
|
||||
"#
|
||||
),
|
||||
"{ x : Num a, y : Float, z : Int }",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -614,6 +614,17 @@ fn unify_shared_tags(
|
|||
}
|
||||
}
|
||||
|
||||
fn has_no_required_fields<'a, I, T>(fields: &mut I) -> bool
|
||||
where
|
||||
I: Iterator<Item = &'a RecordField<T>>,
|
||||
T: 'a,
|
||||
{
|
||||
fields.all(|field| match field {
|
||||
RecordField::Required(_) => false,
|
||||
RecordField::Optional(_) => true,
|
||||
})
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn unify_flat_type(
|
||||
subs: &mut Subs,
|
||||
|
@ -627,11 +638,11 @@ fn unify_flat_type(
|
|||
match (left, right) {
|
||||
(EmptyRecord, EmptyRecord) => merge(subs, ctx, Structure(left.clone())),
|
||||
|
||||
(Record(fields, ext), EmptyRecord) if fields.is_empty() => {
|
||||
(Record(fields, ext), EmptyRecord) if has_no_required_fields(&mut fields.values()) => {
|
||||
unify_pool(subs, pool, *ext, ctx.second)
|
||||
}
|
||||
|
||||
(EmptyRecord, Record(fields, ext)) if fields.is_empty() => {
|
||||
(EmptyRecord, Record(fields, ext)) if has_no_required_fields(&mut fields.values()) => {
|
||||
unify_pool(subs, pool, ctx.first, *ext)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue