Typecheck numeric suffixes in patterns

This commit is contained in:
ayazhafiz 2022-02-01 23:35:14 -05:00
parent a6f7579c07
commit df8113ce32
8 changed files with 270 additions and 41 deletions

View file

@ -178,34 +178,83 @@ pub fn constrain_pattern(
);
}
NumLiteral(var, _, _, _bound) => {
// TODO: constrain bound here
state.vars.push(*var);
&NumLiteral(var, _, _, bound) => {
state.vars.push(var);
let num_type = builtins::num_num(Type::Variable(var));
builtins::add_numeric_bound_constr(
&mut state.constraints,
num_type.clone(),
bound,
region,
Category::Num,
);
state.constraints.push(Constraint::Pattern(
region,
PatternCategory::Num,
builtins::num_num(Type::Variable(*var)),
num_type,
expected,
));
}
IntLiteral(precision_var, _, _, _bound) => {
// TODO: constrain bound here
&IntLiteral(num_var, precision_var, _, _, bound) => {
// First constraint on the free num var; this improves the resolved type quality in
// case the bound is an alias.
builtins::add_numeric_bound_constr(
&mut state.constraints,
Type::Variable(num_var),
bound,
region,
Category::Int,
);
// Link the free num var with the int var and our expectation.
let int_type = builtins::num_int(Type::Variable(precision_var));
state.constraints.push(Constraint::Eq(
Type::Variable(num_var),
Expected::NoExpectation(int_type),
Category::Int,
region,
));
// Also constrain the pattern against the num var, again to reuse aliases if they're present.
state.constraints.push(Constraint::Pattern(
region,
PatternCategory::Int,
builtins::num_int(Type::Variable(*precision_var)),
Type::Variable(num_var),
expected,
));
}
FloatLiteral(precision_var, _, _, _bound) => {
// TODO: constrain bound here
&FloatLiteral(num_var, precision_var, _, _, bound) => {
// First constraint on the free num var; this improves the resolved type quality in
// case the bound is an alias.
builtins::add_numeric_bound_constr(
&mut state.constraints,
Type::Variable(num_var),
bound,
region,
Category::Float,
);
// Link the free num var with the float var and our expectation.
let float_type = builtins::num_float(Type::Variable(precision_var));
state.constraints.push(Constraint::Eq(
Type::Variable(num_var),
Expected::NoExpectation(float_type),
Category::Float,
region,
));
// Also constrain the pattern against the num var, again to reuse aliases if they're present.
state.constraints.push(Constraint::Pattern(
region,
PatternCategory::Float,
builtins::num_float(Type::Variable(*precision_var)),
Type::Variable(num_var),
expected,
));
}