From 5779b77cccf40cdf239132d281463ab536c44735 Mon Sep 17 00:00:00 2001 From: Kasey White Date: Thu, 2 Mar 2023 21:35:17 -0500 Subject: [PATCH] fix: Had to also check for generic type in return of a function. add check to prevent stack overflow error Add panic to prevent any other occurrences of stack overflow --- crates/aiken-lang/src/builder.rs | 13 ++++----- crates/aiken-lang/src/tipo.rs | 12 ++++----- crates/aiken-lang/src/uplc.rs | 31 +++------------------- examples/acceptance_tests/068/lib/tests.ak | 2 +- 4 files changed, 18 insertions(+), 40 deletions(-) diff --git a/crates/aiken-lang/src/builder.rs b/crates/aiken-lang/src/builder.rs index 561835c4..1703c016 100644 --- a/crates/aiken-lang/src/builder.rs +++ b/crates/aiken-lang/src/builder.rs @@ -1186,19 +1186,15 @@ pub fn find_and_replace_generics(tipo: &mut Arc, mono_types: &IndexMap { let var_type = var_tipo.as_ref().borrow().clone(); let var_tipo = match var_type { - TypeVar::Unbound { .. } => todo!(), TypeVar::Link { tipo } => { let mut tipo = tipo; find_and_replace_generics(&mut tipo, mono_types); tipo } - TypeVar::Generic { .. } => unreachable!(), + TypeVar::Generic { .. } | TypeVar::Unbound { .. } => unreachable!(), }; - let t = Type::Var { - tipo: RefCell::from(TypeVar::Link { tipo: var_tipo }).into(), - }; - *tipo = t.into() + *tipo = var_tipo; } }; } @@ -1259,6 +1255,10 @@ pub fn get_variant_name(new_name: &mut String, t: &Arc) { } else { let mut full_type = "_data".to_string(); + if t.is_generic() { + panic!("FOUND A POLYMORPHIC TYPE. EXPECTED MONOMORPHIC TYPE"); + } + let inner_types = t.get_inner_types(); for arg_type in inner_types { @@ -1371,6 +1371,7 @@ pub fn monomorphize( let mut new_air = ir.clone(); let mut new_name = String::new(); let mut needs_variant = false; + for (index, ir) in ir.into_iter().enumerate() { match ir { Air::Var { diff --git a/crates/aiken-lang/src/tipo.rs b/crates/aiken-lang/src/tipo.rs index c99b4f63..fabb31c5 100644 --- a/crates/aiken-lang/src/tipo.rs +++ b/crates/aiken-lang/src/tipo.rs @@ -199,12 +199,12 @@ impl Type { } is_a_generic } - Type::Fn { args, .. } => { + Type::Fn { args, ret } => { let mut is_a_generic = false; for arg in args { is_a_generic = is_a_generic || arg.is_generic(); } - is_a_generic + is_a_generic || ret.is_generic() } } } @@ -229,13 +229,13 @@ impl Type { if self.is_list() { match self { Self::App { args, .. } => args.clone(), - Self::Var { tipo } => tipo.borrow().get_inner_type(), + Self::Var { tipo } => tipo.borrow().get_inner_types(), _ => vec![], } } else if self.is_tuple() { match self { Self::Tuple { elems } => elems.to_vec(), - Self::Var { tipo } => tipo.borrow().get_inner_type(), + Self::Var { tipo } => tipo.borrow().get_inner_types(), _ => vec![], } } else if matches!(self.get_uplc_type(), UplcType::Data) { @@ -246,7 +246,7 @@ impl Type { args.push(ret.clone()); args } - Type::Var { tipo } => tipo.borrow().get_inner_type(), + Type::Var { tipo } => tipo.borrow().get_inner_types(), _ => unreachable!(), } } else { @@ -506,7 +506,7 @@ impl TypeVar { } } - pub fn get_inner_type(&self) -> Vec> { + pub fn get_inner_types(&self) -> Vec> { match self { Self::Link { tipo } => tipo.get_inner_types(), var => { diff --git a/crates/aiken-lang/src/uplc.rs b/crates/aiken-lang/src/uplc.rs index dd00365d..f334f9be 100644 --- a/crates/aiken-lang/src/uplc.rs +++ b/crates/aiken-lang/src/uplc.rs @@ -89,16 +89,10 @@ impl<'a> CodeGenerator<'a> { self.build_ir(body, &mut ir_stack, scope); - println!("{:#?}", ir_stack); - self.define_ir(&mut ir_stack); - println!("{:#?}", ir_stack); - self.convert_opaque_type_to_inner_ir(&mut ir_stack); - println!("{:#?}", ir_stack); - let mut term = self.uplc_code_gen(&mut ir_stack); if self.needs_field_access { @@ -3150,8 +3144,6 @@ impl<'a> CodeGenerator<'a> { let mut recursion_func_map_to_add = recursion_func_map.clone(); - println!("ARE WE GETTING HERE"); - for func_index in func_index_map.clone().iter() { let func = func_index.0; @@ -3276,11 +3268,6 @@ impl<'a> CodeGenerator<'a> { self.build_ir(&function.body, &mut func_ir, scope.to_vec()); - println!( - "OUR FUNC IR FOR {:#?} IS: {:#?}", - non_variant_function_key, func_ir - ); - let param_types = constructor.tipo.arg_types().unwrap(); let mut mono_types: IndexMap> = IndexMap::new(); @@ -3288,22 +3275,17 @@ impl<'a> CodeGenerator<'a> { for (index, arg) in function.arguments.iter().enumerate() { if arg.tipo.is_generic() { let mut map = mono_types.into_iter().collect_vec(); - map.append(&mut get_generics_and_type( - &arg.tipo, - ¶m_types[index], - )); + let param_type = ¶m_types[index]; + + map.append(&mut get_generics_and_type(&arg.tipo, ¶m_type)); mono_types = map.into_iter().collect(); } } - println!("DID WE GET HERE"); - let (variant_name, func_ir) = monomorphize(func_ir, mono_types, &constructor.tipo); - println!("AND HERE?"); - let function_key = FunctionAccessKey { module_name: module.clone(), function_name: non_variant_function_key.function_name, @@ -3412,8 +3394,6 @@ impl<'a> CodeGenerator<'a> { } } - println!("FUNC CALLS IS {:#?}", func_calls); - let recursive = if func_calls.get(&function_key).is_some() { func_calls.remove(&function_key); true @@ -3433,7 +3413,6 @@ impl<'a> CodeGenerator<'a> { ); } } else { - println!("ARE WE GETTING HERE323323"); for func in to_be_defined_map.clone().iter() { if get_common_ancestor(scope, func.1) == scope.to_vec() { if let Some(index_scope) = func_index_map.get(func.0) { @@ -3456,7 +3435,7 @@ impl<'a> CodeGenerator<'a> { } a => { let scope = a.scope(); - println!("ARE WE GETTING HERE2222"); + for func in to_be_defined_map.clone().iter() { if get_common_ancestor(&scope, func.1) == scope.to_vec() { if let Some(index_scope) = func_index_map.get(func.0) { @@ -3479,8 +3458,6 @@ impl<'a> CodeGenerator<'a> { } } - println!("REACHED HERE"); - // Still to be defined for func in to_be_defined_map.clone().iter() { let index_scope = func_index_map.get(func.0).unwrap(); diff --git a/examples/acceptance_tests/068/lib/tests.ak b/examples/acceptance_tests/068/lib/tests.ak index 9a6548e7..8586a2e1 100644 --- a/examples/acceptance_tests/068/lib/tests.ak +++ b/examples/acceptance_tests/068/lib/tests.ak @@ -106,5 +106,5 @@ test flatten_with_2() { None } }, - ) == [("b", ""), ("a", "2")] + ) == [("a", "2"), ("b", "")] }