From 917d447605142e9333ccc4589952b1038e661383 Mon Sep 17 00:00:00 2001 From: ychenfo Date: Thu, 9 Sep 2021 02:08:07 +0800 Subject: [PATCH] nac3core: clean up, fix broken test --- nac3core/src/toplevel/mod.rs | 22 ++++++------ nac3core/src/toplevel/type_annotation.rs | 35 +++++++++++-------- .../src/typecheck/type_inferencer/test.rs | 2 +- nac3core/src/typecheck/typedef/mod.rs | 7 ++-- 4 files changed, 35 insertions(+), 31 deletions(-) diff --git a/nac3core/src/toplevel/mod.rs b/nac3core/src/toplevel/mod.rs index ce395982..13f9b5e0 100644 --- a/nac3core/src/toplevel/mod.rs +++ b/nac3core/src/toplevel/mod.rs @@ -551,7 +551,7 @@ impl TopLevelComposer { } } - println!("type_var_to_concrete_def1: {:?}", type_var_to_concrete_def); + // println!("type_var_to_concrete_def1: {:?}", type_var_to_concrete_def); // handle the inheritanced methods and fields let mut current_ancestor_depth: usize = 2; @@ -587,19 +587,19 @@ impl TopLevelComposer { } } - println!("type_var_to_concrete_def3: {:?}\n", type_var_to_concrete_def); + // println!("type_var_to_concrete_def3: {:?}\n", type_var_to_concrete_def); // unification of previously assigned typevar for (ty, def) in type_var_to_concrete_def { - println!( - "{:?}_{} -> {:?}\n", - ty, - unifier.stringify(ty, - &mut |id| format!("class{}", id), - &mut |id| format!("tvar{}", id) - ), - def - ); + // println!( + // "{:?}_{} -> {:?}\n", + // ty, + // unifier.stringify(ty, + // &mut |id| format!("class{}", id), + // &mut |id| format!("tvar{}", id) + // ), + // def + // ); let target_ty = get_type_from_type_annotation_kinds(&temp_def_list, unifier, primitives, &def)?; unifier.unify(ty, target_ty)?; diff --git a/nac3core/src/toplevel/type_annotation.rs b/nac3core/src/toplevel/type_annotation.rs index 3f8ddb59..69abf29f 100644 --- a/nac3core/src/toplevel/type_annotation.rs +++ b/nac3core/src/toplevel/type_annotation.rs @@ -213,10 +213,13 @@ pub fn get_type_from_type_annotation_kinds( // NOTE: check for compatible range here let mut result: HashMap = HashMap::new(); for (tvar, p) in type_vars.iter().zip(param_ty) { - if let TypeEnum::TVar { id, range, meta: TypeVarMeta::Generic } = unifier.get_ty(*tvar).as_ref() { + if let TypeEnum::TVar { id, range, meta: TypeVarMeta::Generic } = + unifier.get_ty(*tvar).as_ref() + { let ok: bool = { // NOTE: create a temp type var and unify to check compatibility - let temp = unifier.get_fresh_var_with_range(range.borrow().as_slice()); + let temp = + unifier.get_fresh_var_with_range(range.borrow().as_slice()); unifier.unify(temp.0, p).is_ok() }; if ok { @@ -230,7 +233,7 @@ pub fn get_type_from_type_annotation_kinds( &mut |id| format!("tvar{}", id) ), *id - )) + )); } } else { unreachable!("must be generic type var") @@ -250,16 +253,16 @@ pub fn get_type_from_type_annotation_kinds( (name.clone(), subst_ty) })); - println!("tobj_fields: {:?}", tobj_fields); - println!( - "{:?}: {}\n", - tobj_fields.get("__init__").unwrap(), - unifier.stringify( - *tobj_fields.get("__init__").unwrap(), - &mut |id| format!("class{}", id), - &mut |id| format!("tvar{}", id) - ) - ); + // println!("tobj_fields: {:?}", tobj_fields); + // println!( + // "{:?}: {}\n", + // tobj_fields.get("__init__").unwrap(), + // unifier.stringify( + // *tobj_fields.get("__init__").unwrap(), + // &mut |id| format!("class{}", id), + // &mut |id| format!("tvar{}", id) + // ) + // ); Ok(unifier.add_ty(TypeEnum::TObj { obj_id: *id, @@ -342,13 +345,15 @@ pub fn get_type_var_contained_in_type_annotation(ann: &TypeAnnotation) -> Vec result.extend(get_type_var_contained_in_type_annotation(ann.as_ref())), + TypeAnnotation::ListKind(ann) => { + result.extend(get_type_var_contained_in_type_annotation(ann.as_ref())) + } TypeAnnotation::TupleKind(anns) => { for a in anns { result.extend(get_type_var_contained_in_type_annotation(a)); } } - TypeAnnotation::PrimitiveKind( .. ) => {} + TypeAnnotation::PrimitiveKind(..) => {} } result } diff --git a/nac3core/src/typecheck/type_inferencer/test.rs b/nac3core/src/typecheck/type_inferencer/test.rs index bf1c3727..b000f2d0 100644 --- a/nac3core/src/typecheck/type_inferencer/test.rs +++ b/nac3core/src/typecheck/type_inferencer/test.rs @@ -350,7 +350,7 @@ impl TestEnvironment { "}, [("a", "fn[[x=bool], bool]"), ("b", "fn[[x=int32], int32]"), ("c", "bool"), - ("d", "int32"), ("foo1", "Foo[bool]"), ("foo2", "Foo[int32]")].iter().cloned().collect(), + ("d", "int32"), ("foo1", "Foo[1->bool]"), ("foo2", "Foo[1->int32]")].iter().cloned().collect(), &[] ; "obj test")] #[test_case(indoc! {" diff --git a/nac3core/src/typecheck/typedef/mod.rs b/nac3core/src/typecheck/typedef/mod.rs index 1017d28f..56b7dbe9 100644 --- a/nac3core/src/typecheck/typedef/mod.rs +++ b/nac3core/src/typecheck/typedef/mod.rs @@ -714,10 +714,9 @@ impl Unifier { let name = obj_to_name(obj_id.0); let params = params.borrow(); if !params.is_empty() { - let params = - params.iter().map(|(id, v)| - format!("{}->{}", *id, self.stringify(*v, obj_to_name, var_to_name)) - ); + let params = params.iter().map(|(id, v)| { + format!("{}->{}", *id, self.stringify(*v, obj_to_name, var_to_name)) + }); // NOTE: sort to preserve order let mut params = params.sorted(); format!("{}[{}]", name, params.join(", "))