From 0bab477ab04baf7ba3740c419ec44bc3090fef14 Mon Sep 17 00:00:00 2001 From: ychenfo Date: Wed, 25 Aug 2021 14:59:31 +0800 Subject: [PATCH] get rid of nested tuple in type annotation helper function --- nac3core/src/toplevel/mod.rs | 26 +++++++++++++----------- nac3core/src/toplevel/type_annotation.rs | 9 ++++---- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/nac3core/src/toplevel/mod.rs b/nac3core/src/toplevel/mod.rs index ed69fb6f..72776b4d 100644 --- a/nac3core/src/toplevel/mod.rs +++ b/nac3core/src/toplevel/mod.rs @@ -420,15 +420,17 @@ impl TopLevelComposer { .collect::, _>>()?; // check if all are unique type vars - let mut occured_type_var_id: HashSet = HashSet::new(); - let all_unique_type_var = type_vars.iter().all(|x| { - let ty = unifier.get_ty(*x); - if let TypeEnum::TVar { id, .. } = ty.as_ref() { - occured_type_var_id.insert(*id) - } else { - false - } - }); + let all_unique_type_var = { + let mut occured_type_var_id: HashSet = HashSet::new(); + type_vars.iter().all(|x| { + let ty = unifier.get_ty(*x); + if let TypeEnum::TVar { id, .. } = ty.as_ref() { + occured_type_var_id.insert(*id) + } else { + false + } + }) + }; if !all_unique_type_var { return Err("expect unique type variables".into()); } @@ -439,7 +441,7 @@ impl TopLevelComposer { .map(|x| { // must be type var here after previous check let dup = duplicate_type_var(unifier, x); - (dup.1, (dup.0).0) + (dup.1, dup.0) }) .collect_vec(); @@ -620,9 +622,9 @@ impl TopLevelComposer { } else { // if not, create a duplicate let ty_copy = duplicate_type_var(unifier, ty); - ty = ty_copy.0.0; + ty = ty_copy.0; 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); } } diff --git a/nac3core/src/toplevel/type_annotation.rs b/nac3core/src/toplevel/type_annotation.rs index d5bad4e3..7839437f 100644 --- a/nac3core/src/toplevel/type_annotation.rs +++ b/nac3core/src/toplevel/type_annotation.rs @@ -56,7 +56,7 @@ pub fn parse_ast_to_type_annotation_kinds( Ok(TypeAnnotation::TypeVarKind( *id, // TODO: maybe not duplicate will also be fine here? - duplicate_type_var(unifier, ty).0.0 + duplicate_type_var(unifier, ty).0 )) } else { Err("not a type variable identifier".into()) @@ -217,12 +217,13 @@ pub fn get_type_from_type_annotation_kinds( pub fn duplicate_type_var( unifier: &mut Unifier, type_var: Type -) -> ((Type, u32), u32) { +) -> (Type, u32, u32) { let ty = unifier.get_ty(type_var); if let TypeEnum::TVar { id, range, .. } = ty.as_ref() { let range = range.borrow(); 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 { unreachable!("must be type var here to be duplicated"); } @@ -253,7 +254,7 @@ pub fn make_self_type_annotation( .iter() .map(|(var_id, ty)| TypeAnnotation::TypeVarKind( *var_id, - duplicate_type_var(unifier, *ty).0.0 + duplicate_type_var(unifier, *ty).0 )) .collect_vec() })