Add a datatype

This commit is contained in:
Ayaz Hafiz 2022-07-22 13:02:22 -04:00
parent e06cb6e1a7
commit a03cf85529
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58

View file

@ -736,7 +736,7 @@ pub fn constrain_expr(
let mut pattern_headers = SendMap::default(); let mut pattern_headers = SendMap::default();
let mut pattern_cons = Vec::with_capacity(branches.len() + 2); let mut pattern_cons = Vec::with_capacity(branches.len() + 2);
let mut delayed_is_open_constraints = Vec::with_capacity(2); let mut delayed_is_open_constraints = Vec::with_capacity(2);
let mut branch_cons = Vec::with_capacity(branches.len()); let mut body_cons = Vec::with_capacity(branches.len());
for (index, when_branch) in branches.iter().enumerate() { for (index, when_branch) in branches.iter().enumerate() {
let expected_pattern = |sub_pattern, sub_region| { let expected_pattern = |sub_pattern, sub_region| {
@ -750,13 +750,13 @@ pub fn constrain_expr(
) )
}; };
let ( let ConstrainedBranch {
new_pattern_vars, vars: new_pattern_vars,
new_pattern_headers, headers: new_pattern_headers,
pattern_con, pattern_constraints,
partial_delayed_is_open_constraints, is_open_constrains,
branch_con, body_constraints,
) = constrain_when_branch_help( } = constrain_when_branch_help(
constraints, constraints,
env, env,
region, region,
@ -785,10 +785,10 @@ pub fn constrain_expr(
} }
pattern_headers.extend(new_pattern_headers); pattern_headers.extend(new_pattern_headers);
pattern_cons.push(pattern_con); pattern_cons.push(pattern_constraints);
delayed_is_open_constraints.extend(partial_delayed_is_open_constraints); delayed_is_open_constraints.extend(is_open_constrains);
branch_cons.push(branch_con); body_cons.push(body_constraints);
} }
// Deviation: elm adds another layer of And nesting // Deviation: elm adds another layer of And nesting
@ -838,7 +838,7 @@ pub fn constrain_expr(
// Solve all the pattern constraints together, introducing variables in the pattern as // Solve all the pattern constraints together, introducing variables in the pattern as
// need be before solving the bodies. // need be before solving the bodies.
let pattern_constraints = constraints.and_constraint(pattern_cons); let pattern_constraints = constraints.and_constraint(pattern_cons);
let body_constraints = constraints.and_constraint(branch_cons); let body_constraints = constraints.and_constraint(body_cons);
let when_body_con = constraints.let_constraint( let when_body_con = constraints.let_constraint(
[], [],
pattern_vars, pattern_vars,
@ -1802,6 +1802,14 @@ fn constrain_value_def(
} }
} }
struct ConstrainedBranch {
vars: Vec<Variable>,
headers: VecMap<Symbol, Loc<Type>>,
pattern_constraints: Constraint,
is_open_constrains: Vec<Constraint>,
body_constraints: Constraint,
}
/// Constrain a when branch, returning (variables in pattern, symbols introduced in pattern, pattern constraint, body constraint). /// Constrain a when branch, returning (variables in pattern, symbols introduced in pattern, pattern constraint, body constraint).
/// We want to constraint all pattern constraints in a "when" before body constraints. /// We want to constraint all pattern constraints in a "when" before body constraints.
#[inline(always)] #[inline(always)]
@ -1812,13 +1820,7 @@ fn constrain_when_branch_help(
when_branch: &WhenBranch, when_branch: &WhenBranch,
pattern_expected: impl Fn(HumanIndex, Region) -> PExpected<Type>, pattern_expected: impl Fn(HumanIndex, Region) -> PExpected<Type>,
expr_expected: Expected<Type>, expr_expected: Expected<Type>,
) -> ( ) -> ConstrainedBranch {
Vec<Variable>,
VecMap<Symbol, Loc<Type>>,
Constraint,
Vec<Constraint>,
Constraint,
) {
let ret_constraint = constrain_expr( let ret_constraint = constrain_expr(
constraints, constraints,
env, env,
@ -1912,13 +1914,13 @@ fn constrain_when_branch_help(
) )
}; };
( ConstrainedBranch {
state.vars, vars: state.vars,
state.headers, headers: state.headers,
pattern_constraints, pattern_constraints,
delayed_is_open_constraints, is_open_constrains: delayed_is_open_constraints,
body_constraints, body_constraints,
) }
} }
fn constrain_field( fn constrain_field(