get rid of nested tuple in type annotation helper function

This commit is contained in:
ychenfo 2021-08-25 14:59:31 +08:00
parent 862d205f67
commit 0bab477ab0
2 changed files with 19 additions and 16 deletions

View File

@ -420,15 +420,17 @@ impl TopLevelComposer {
.collect::<Result<Vec<_>, _>>()?; .collect::<Result<Vec<_>, _>>()?;
// check if all are unique type vars // check if all are unique type vars
let mut occured_type_var_id: HashSet<u32> = HashSet::new(); let all_unique_type_var = {
let all_unique_type_var = type_vars.iter().all(|x| { let mut occured_type_var_id: HashSet<u32> = HashSet::new();
let ty = unifier.get_ty(*x); type_vars.iter().all(|x| {
if let TypeEnum::TVar { id, .. } = ty.as_ref() { let ty = unifier.get_ty(*x);
occured_type_var_id.insert(*id) if let TypeEnum::TVar { id, .. } = ty.as_ref() {
} else { occured_type_var_id.insert(*id)
false } else {
} false
}); }
})
};
if !all_unique_type_var { if !all_unique_type_var {
return Err("expect unique type variables".into()); return Err("expect unique type variables".into());
} }
@ -439,7 +441,7 @@ impl TopLevelComposer {
.map(|x| { .map(|x| {
// must be type var here after previous check // must be type var here after previous check
let dup = duplicate_type_var(unifier, x); let dup = duplicate_type_var(unifier, x);
(dup.1, (dup.0).0) (dup.1, dup.0)
}) })
.collect_vec(); .collect_vec();
@ -620,9 +622,9 @@ impl TopLevelComposer {
} else { } else {
// if not, create a duplicate // if not, create a duplicate
let ty_copy = duplicate_type_var(unifier, ty); let ty_copy = duplicate_type_var(unifier, ty);
ty = ty_copy.0.0; ty = ty_copy.0;
occured_type_var.insert(*id, ty); occured_type_var.insert(*id, ty);
function_var_map.insert(ty_copy.1, ty_copy.0.0); function_var_map.insert(ty_copy.1, ty_copy.0);
} }
} }

View File

@ -56,7 +56,7 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
Ok(TypeAnnotation::TypeVarKind( Ok(TypeAnnotation::TypeVarKind(
*id, *id,
// TODO: maybe not duplicate will also be fine here? // TODO: maybe not duplicate will also be fine here?
duplicate_type_var(unifier, ty).0.0 duplicate_type_var(unifier, ty).0
)) ))
} else { } else {
Err("not a type variable identifier".into()) Err("not a type variable identifier".into())
@ -217,12 +217,13 @@ pub fn get_type_from_type_annotation_kinds(
pub fn duplicate_type_var( pub fn duplicate_type_var(
unifier: &mut Unifier, unifier: &mut Unifier,
type_var: Type type_var: Type
) -> ((Type, u32), u32) { ) -> (Type, u32, u32) {
let ty = unifier.get_ty(type_var); let ty = unifier.get_ty(type_var);
if let TypeEnum::TVar { id, range, .. } = ty.as_ref() { if let TypeEnum::TVar { id, range, .. } = ty.as_ref() {
let range = range.borrow(); let range = range.borrow();
let range = range.as_slice(); let range = range.as_slice();
(unifier.get_fresh_var_with_range(range), *id) let dup = unifier.get_fresh_var_with_range(range);
(dup.0, dup.1, *id)
} else { } else {
unreachable!("must be type var here to be duplicated"); unreachable!("must be type var here to be duplicated");
} }
@ -253,7 +254,7 @@ pub fn make_self_type_annotation(
.iter() .iter()
.map(|(var_id, ty)| TypeAnnotation::TypeVarKind( .map(|(var_id, ty)| TypeAnnotation::TypeVarKind(
*var_id, *var_id,
duplicate_type_var(unifier, *ty).0.0 duplicate_type_var(unifier, *ty).0
)) ))
.collect_vec() .collect_vec()
}) })