From fb5b4697a9f323c610b29c578142880582be60a9 Mon Sep 17 00:00:00 2001 From: ychenfo Date: Mon, 23 Aug 2021 10:34:11 +0800 Subject: [PATCH] fix rebase conflict and some test failure with unifier's error message --- nac3core/src/codegen/expr.rs | 5 +++-- nac3core/src/codegen/mod.rs | 3 ++- nac3core/src/top_level.rs | 15 +++++--------- nac3core/src/typecheck/type_inferencer/mod.rs | 6 ++++-- .../src/typecheck/type_inferencer/test.rs | 2 +- nac3core/src/typecheck/typedef/test.rs | 20 +++++++++---------- nac3standalone/src/main.rs | 4 ++-- 7 files changed, 27 insertions(+), 28 deletions(-) diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index eaf1a881..4c3ffe4a 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -56,7 +56,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { // we cannot have other types, virtual type should be handled by function calls _ => unreachable!(), }; - let def = &self.top_level.definitions[obj_id.0]; + let def = &self.top_level.definitions.read()[obj_id.0]; let index = if let TopLevelDef::Class { fields, .. } = &*def.read() { fields.iter().find_position(|x| x.0 == attr).unwrap().0 } else { @@ -104,7 +104,8 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { ret: Type, ) -> Option> { let key = self.get_subst_key(obj.map(|(a, _)| a), fun.0); - let definition = self.top_level.definitions.get(fun.1 .0).unwrap(); + let top_level_defs = self.top_level.definitions.read(); + let definition = top_level_defs.get(fun.1 .0).unwrap(); let val = if let TopLevelDef::Function { instance_to_symbol, .. } = &*definition.read() { let symbol = instance_to_symbol.get(&key).unwrap_or_else(|| { // TODO: codegen for function that are not yet generated diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index ccd73ec8..b3c59e7f 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -206,7 +206,8 @@ fn get_llvm_type<'ctx>( match &*unifier.get_ty(ty) { TObj { obj_id, fields, .. } => { // a struct with fields in the order of declaration - let definition = top_level.definitions.get(obj_id.0).unwrap(); + let top_level_defs = top_level.definitions.read(); + let definition = top_level_defs.get(obj_id.0).unwrap(); let ty = if let TopLevelDef::Class { fields: fields_list, .. } = &*definition.read() { let fields = fields.borrow(); diff --git a/nac3core/src/top_level.rs b/nac3core/src/top_level.rs index 5e6a6360..052a7f93 100644 --- a/nac3core/src/top_level.rs +++ b/nac3core/src/top_level.rs @@ -339,7 +339,7 @@ pub enum TopLevelDef { } pub struct TopLevelContext { - pub definitions: Arc>>>, + pub definitions: Arc>>>>, pub unifiers: Arc>>, } @@ -357,21 +357,15 @@ pub struct TopLevelComposer { pub keyword_list: Vec, } -impl TopLevelContext { - pub fn read_top_level_def_list(&self) -> &[Arc>] { - self.definitions.as_slice() - } -} - impl TopLevelComposer { pub fn make_top_level_context(self) -> TopLevelContext { TopLevelContext { - definitions: self + definitions: RwLock::new(self .definition_ast_list .into_iter() .map(|(x, ..)| x) .collect::>() - .into(), + ).into(), // FIXME: all the big unifier or? unifiers: Default::default(), } @@ -924,7 +918,8 @@ impl TopLevelComposer { fields, methods, resolver, - type_vars + type_vars, + .. } = class_def.deref_mut() { if let ast::StmtKind::ClassDef { name, bases, body, .. } = &class_ast { (object_id, name.clone(), bases, body, ancestors, fields, methods, type_vars, resolver) diff --git a/nac3core/src/typecheck/type_inferencer/mod.rs b/nac3core/src/typecheck/type_inferencer/mod.rs index a4ccbe17..d6989c5d 100644 --- a/nac3core/src/typecheck/type_inferencer/mod.rs +++ b/nac3core/src/typecheck/type_inferencer/mod.rs @@ -82,8 +82,9 @@ impl<'a> fold::Fold<()> for Inferencer<'a> { } else { None }; + let top_level_defs = self.top_level.definitions.read(); let annotation_type = self.function_data.resolver.parse_type_annotation( - self.top_level.read_top_level_def_list(), + top_level_defs.as_slice(), self.unifier, &self.primitives, annotation.as_ref(), @@ -344,8 +345,9 @@ impl<'a> Inferencer<'a> { } let arg0 = self.fold_expr(args.remove(0))?; let ty = if let Some(arg) = args.pop() { + let top_level_defs = self.top_level.definitions.read(); self.function_data.resolver.parse_type_annotation( - self.top_level.read_top_level_def_list(), + top_level_defs.as_slice(), self.unifier, self.primitives, &arg, diff --git a/nac3core/src/typecheck/type_inferencer/test.rs b/nac3core/src/typecheck/type_inferencer/test.rs index 88dfc218..71c8d715 100644 --- a/nac3core/src/typecheck/type_inferencer/test.rs +++ b/nac3core/src/typecheck/type_inferencer/test.rs @@ -269,7 +269,7 @@ impl TestEnvironment { .collect(); let top_level = TopLevelContext { - definitions: Arc::new(top_level_defs), + definitions: Arc::new(top_level_defs.into()), unifiers: Default::default(), }; diff --git a/nac3core/src/typecheck/typedef/test.rs b/nac3core/src/typecheck/typedef/test.rs index 2972b3f9..ee795056 100644 --- a/nac3core/src/typecheck/typedef/test.rs +++ b/nac3core/src/typecheck/typedef/test.rs @@ -263,7 +263,7 @@ fn test_unify( ("v1", "Tuple[int]"), ("v2", "List[int]"), ], - (("v1", "v2"), "Cannot unify TList with TTuple") + (("v1", "v2"), "Cannot unify list[0] with tuple[0]") ; "type mismatch" )] #[test_case(2, @@ -271,7 +271,7 @@ fn test_unify( ("v1", "Tuple[int]"), ("v2", "Tuple[float]"), ], - (("v1", "v2"), "Cannot unify objects with ID 0 and 1") + (("v1", "v2"), "Cannot unify 0 with 1") ; "tuple parameter mismatch" )] #[test_case(2, @@ -377,7 +377,7 @@ fn test_typevar_range() { let v = env.unifier.get_fresh_var_with_range(&[int, boolean]).0; assert_eq!( env.unifier.unify(int_list, v), - Err("Cannot unify type variable 3 with TList due to incompatible value range".to_string()) + Err("Cannot unify variable 3 with list[0] due to incompatible value range".to_string()) ); // unification between v and float @@ -385,7 +385,7 @@ fn test_typevar_range() { let v = env.unifier.get_fresh_var_with_range(&[int, boolean]).0; assert_eq!( env.unifier.unify(float, v), - Err("Cannot unify type variable 4 with TObj due to incompatible value range".to_string()) + Err("Cannot unify variable 4 with 1 due to incompatible value range".to_string()) ); let v1 = env.unifier.get_fresh_var_with_range(&[int, boolean]).0; @@ -405,7 +405,7 @@ fn test_typevar_range() { // where v in (int, List[v1]), v1 in (int, bool) assert_eq!( env.unifier.unify(float_list, v), - Err("Cannot unify type variable 8 with TList due to incompatible value range".to_string()) + Err("Cannot unify variable 8 with list[1] due to incompatible value range".to_string()) ); let a = env.unifier.get_fresh_var_with_range(&[int, float]).0; @@ -418,7 +418,7 @@ fn test_typevar_range() { env.unifier.unify(a, b).unwrap(); assert_eq!( env.unifier.unify(a, int), - Err("Cannot unify type variable 12 with TObj due to incompatible value range".into()) + Err("Cannot unify variable 12 with 0 due to incompatible value range".into()) ); let a = env.unifier.get_fresh_var_with_range(&[int, float]).0; @@ -441,7 +441,7 @@ fn test_typevar_range() { let int_list = env.unifier.add_ty(TypeEnum::TList { ty: int }); assert_eq!( env.unifier.unify(a_list, int_list), - Err("Cannot unify type variable 19 with TObj due to incompatible value range".into()) + Err("Cannot unify variable 19 with 0 due to incompatible value range".into()) ); let a = env.unifier.get_fresh_var_with_range(&[int, float]).0; @@ -452,7 +452,7 @@ fn test_typevar_range() { env.unifier.unify(a_list, b_list).unwrap(); assert_eq!( env.unifier.unify(b, boolean), - Err("Cannot unify type variable 21 with TObj due to incompatible value range".into()) + Err("Cannot unify variable 21 with 2 due to incompatible value range".into()) ); } @@ -467,11 +467,11 @@ fn test_rigid_var() { let int = env.parse("int", &HashMap::new()); let list_int = env.parse("List[int]", &HashMap::new()); - assert_eq!(env.unifier.unify(a, b), Err("Cannot unify TRigidVar with TRigidVar".to_string())); + assert_eq!(env.unifier.unify(a, b), Err("Cannot unify var3 with var2".to_string())); env.unifier.unify(list_a, list_x).unwrap(); assert_eq!( env.unifier.unify(list_x, list_int), - Err("Cannot unify TObj with TRigidVar".to_string()) + Err("Cannot unify 0 with var2".to_string()) ); env.unifier.replace_rigid_var(a, int); diff --git a/nac3standalone/src/main.rs b/nac3standalone/src/main.rs index 4bbc1eb1..666b9a74 100644 --- a/nac3standalone/src/main.rs +++ b/nac3standalone/src/main.rs @@ -52,8 +52,8 @@ fn main() { let (_, composer) = TopLevelComposer::new(); let mut unifier = composer.unifier.clone(); - let primitives = composer.primitives.clone(); - let top_level = Arc::new(composer.to_top_level_context()); + let primitives = composer.primitives_ty.clone(); + let top_level = Arc::new(composer.make_top_level_context()); unifier.top_level = Some(top_level.clone()); let fun = unifier.add_ty(TypeEnum::TFunc(RefCell::new(FunSignature { args: vec![FuncArg {