optional fields improvements

This commit is contained in:
Folkert 2020-07-21 15:25:21 +02:00
parent 48aebc5c59
commit b45b39af5e
14 changed files with 329 additions and 47 deletions

View file

@ -227,24 +227,28 @@ pub fn constrain_pattern(
constrain_pattern(env, &loc_guard.value, loc_guard.region, expected, state);
RecordField::Required(pat_type)
RecordField::Demanded(pat_type)
}
DestructType::Optional(expr_var, loc_expr) => {
state.constraints.push(Constraint::Pattern(
region,
PatternCategory::PatternDefault,
Type::Variable(*expr_var),
PExpected::ForReason(
PReason::OptionalField,
pat_type.clone(),
loc_expr.region,
),
));
state.vars.push(*expr_var);
let expr_expected = Expected::ForReason(
Reason::RecordDefaultField(label.clone()),
pat_type.clone(),
loc_expr.region,
);
state.constraints.push(Constraint::Eq(
Type::Variable(*expr_var),
expr_expected.clone(),
Category::DefaultValue(label.clone()),
region,
));
state.vars.push(*expr_var);
let expr_con =
constrain_expr(env, loc_expr.region, &loc_expr.value, expr_expected);
state.constraints.push(expr_con);
@ -253,7 +257,7 @@ pub fn constrain_pattern(
}
DestructType::Required => {
// No extra constraints necessary.
RecordField::Required(pat_type)
RecordField::Demanded(pat_type)
}
};