Revert "Include annotation type signatures in Expected struct"

This reverts commit 6e4fd5f06a1ae6138659b0073b4e2b375a499588.

This idea didn't work out because cloning the type and storing it on a
variable still resulted in the solver trying to uify the variable with
the type. When there were errors, which there certainly would be if we
tried to unify the variable with a structure that had nested flex/rigid
vars, the nested flex/rigid vars would inherit those errors, and the
program wouldn't typecheck.

Since the motivation here was to expose the signature type to
`reporting` so that we could modify it with suggestions, we should
instead pass that information along in something analogous to the
`Expected` struct.
This commit is contained in:
ayazhafiz 2021-11-25 13:08:18 -05:00
parent a8e38172ac
commit d352d2cdf8
16 changed files with 64 additions and 204 deletions

View file

@ -93,7 +93,7 @@ pub fn constrain_expr(
env: &Env,
region: Region,
expr: &Expr,
expected: Expected<Type, Variable>,
expected: Expected<Type>,
) -> Constraint {
match expr {
Int(var, precision, _, _) => int_literal(*var, *precision, expected, region),
@ -468,7 +468,7 @@ pub fn constrain_expr(
AnnotationSource::TypedIfBranch {
index: Index::zero_based(index),
num_branches,
annotation: *ann_source.annotation(),
region: ann_source.region(),
},
tipe.clone(),
),
@ -488,7 +488,7 @@ pub fn constrain_expr(
AnnotationSource::TypedIfBranch {
index: Index::zero_based(branches.len()),
num_branches,
annotation: *ann_source.annotation(),
region: ann_source.region(),
},
tipe.clone(),
),
@ -606,7 +606,7 @@ pub fn constrain_expr(
*arity,
TypedWhenBranch {
index: Index::zero_based(index),
annotation: *ann_source.annotation(),
region: ann_source.region(),
},
typ.clone(),
),
@ -1021,7 +1021,7 @@ fn constrain_when_branch(
region: Region,
when_branch: &WhenBranch,
pattern_expected: PExpected<Type>,
expr_expected: Expected<Type, Variable>,
expr_expected: Expected<Type>,
) -> Constraint {
let ret_constraint = constrain_expr(env, region, &when_branch.value.value, expr_expected);
@ -1089,7 +1089,7 @@ fn constrain_field(env: &Env, field_var: Variable, loc_expr: &Located<Expr>) ->
}
#[inline(always)]
fn constrain_empty_record(region: Region, expected: Expected<Type, Variable>) -> Constraint {
fn constrain_empty_record(region: Region, expected: Expected<Type>) -> Constraint {
Eq(EmptyRec, expected, Category::Record, region)
}
@ -1182,15 +1182,15 @@ fn constrain_def(env: &Env, def: &Def, body_con: Constraint) -> Constraint {
rigids: ftv,
};
let annotation_ty = Located::at(annotation.region, annotation.annotation_var);
let annotation_expected = FromAnnotation(
def.loc_pattern.clone(),
arity,
AnnotationSource::TypedBody {
annotation: annotation_ty,
region: annotation.region,
},
signature.clone(),
);
def_pattern_state.constraints.push(Eq(
expr_type,
annotation_expected.clone(),
@ -1198,16 +1198,6 @@ fn constrain_def(env: &Env, def: &Def, body_con: Constraint) -> Constraint {
Region::span_across(&annotation.region, &def.loc_expr.region),
));
// Associate the original annotation type itself with an annotation variable,
// so we can get it back out later during error reporting if needed. This avoids
// passing down the bulky annotation type to everyone who cares about it.
def_pattern_state.constraints.push(Store(
annotation.signature.clone(),
annotation.annotation_var,
std::file!(),
std::line!(),
));
// when a def is annotated, and it's body is a closure, treat this
// as a named function (in elm terms) for error messages.
//
@ -1310,7 +1300,7 @@ fn constrain_def(env: &Env, def: &Def, body_con: Constraint) -> Constraint {
def.loc_pattern.clone(),
arguments.len(),
AnnotationSource::TypedBody {
annotation: annotation_ty,
region: annotation.region,
},
ret_type.clone(),
);
@ -1337,7 +1327,7 @@ fn constrain_def(env: &Env, def: &Def, body_con: Constraint) -> Constraint {
def.loc_pattern.clone(),
arity,
AnnotationSource::TypedBody {
annotation: annotation_ty,
region: annotation.region,
},
*signature_closure_type.clone(),
),
@ -1569,19 +1559,10 @@ pub fn rec_defs_help(
def.loc_pattern.clone(),
arity,
AnnotationSource::TypedBody {
annotation: Located::at(annotation.region, annotation.annotation_var),
region: annotation.region,
},
signature.clone(),
);
// Associate the original annotation type itself with an annotation variable,
// so we can get it back out later during error reporting if needed. This avoids
// passing down the bulky annotation type to everyone who cares about it.
let annotation_constr = Store(
annotation.signature.clone(),
annotation.annotation_var,
std::file!(),
std::line!(),
);
// when a def is annotated, and it's body is a closure, treat this
// as a named function (in elm terms) for error messages.
@ -1714,7 +1695,6 @@ pub fn rec_defs_help(
Store(signature, expr_var, std::file!(), std::line!()),
Store(ret_type, ret_var, std::file!(), std::line!()),
closure_constraint,
annotation_constr,
]),
);
@ -1745,7 +1725,6 @@ pub fn rec_defs_help(
})),
// Store type into AST vars. We use Store so errors aren't reported twice
Store(signature, expr_var, std::file!(), std::line!()),
annotation_constr,
]);
rigid_info.vars.extend(&new_rigids);