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
This commit is contained in:
Kasey White 2023-03-02 21:35:17 -05:00 committed by Lucas
parent 26b4156d5a
commit 5779b77ccc
4 changed files with 18 additions and 40 deletions

View File

@ -1186,19 +1186,15 @@ pub fn find_and_replace_generics(tipo: &mut Arc<Type>, mono_types: &IndexMap<u64
Type::Var { tipo: var_tipo } => { Type::Var { tipo: var_tipo } => {
let var_type = var_tipo.as_ref().borrow().clone(); let var_type = var_tipo.as_ref().borrow().clone();
let var_tipo = match var_type { let var_tipo = match var_type {
TypeVar::Unbound { .. } => todo!(),
TypeVar::Link { tipo } => { TypeVar::Link { tipo } => {
let mut tipo = tipo; let mut tipo = tipo;
find_and_replace_generics(&mut tipo, mono_types); find_and_replace_generics(&mut tipo, mono_types);
tipo tipo
} }
TypeVar::Generic { .. } => unreachable!(), TypeVar::Generic { .. } | TypeVar::Unbound { .. } => unreachable!(),
}; };
let t = Type::Var { *tipo = var_tipo;
tipo: RefCell::from(TypeVar::Link { tipo: var_tipo }).into(),
};
*tipo = t.into()
} }
}; };
} }
@ -1259,6 +1255,10 @@ pub fn get_variant_name(new_name: &mut String, t: &Arc<Type>) {
} else { } else {
let mut full_type = "_data".to_string(); 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(); let inner_types = t.get_inner_types();
for arg_type in inner_types { for arg_type in inner_types {
@ -1371,6 +1371,7 @@ pub fn monomorphize(
let mut new_air = ir.clone(); let mut new_air = ir.clone();
let mut new_name = String::new(); let mut new_name = String::new();
let mut needs_variant = false; let mut needs_variant = false;
for (index, ir) in ir.into_iter().enumerate() { for (index, ir) in ir.into_iter().enumerate() {
match ir { match ir {
Air::Var { Air::Var {

View File

@ -199,12 +199,12 @@ impl Type {
} }
is_a_generic is_a_generic
} }
Type::Fn { args, .. } => { Type::Fn { args, ret } => {
let mut is_a_generic = false; let mut is_a_generic = false;
for arg in args { for arg in args {
is_a_generic = is_a_generic || arg.is_generic(); 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() { if self.is_list() {
match self { match self {
Self::App { args, .. } => args.clone(), Self::App { args, .. } => args.clone(),
Self::Var { tipo } => tipo.borrow().get_inner_type(), Self::Var { tipo } => tipo.borrow().get_inner_types(),
_ => vec![], _ => vec![],
} }
} else if self.is_tuple() { } else if self.is_tuple() {
match self { match self {
Self::Tuple { elems } => elems.to_vec(), Self::Tuple { elems } => elems.to_vec(),
Self::Var { tipo } => tipo.borrow().get_inner_type(), Self::Var { tipo } => tipo.borrow().get_inner_types(),
_ => vec![], _ => vec![],
} }
} else if matches!(self.get_uplc_type(), UplcType::Data) { } else if matches!(self.get_uplc_type(), UplcType::Data) {
@ -246,7 +246,7 @@ impl Type {
args.push(ret.clone()); args.push(ret.clone());
args args
} }
Type::Var { tipo } => tipo.borrow().get_inner_type(), Type::Var { tipo } => tipo.borrow().get_inner_types(),
_ => unreachable!(), _ => unreachable!(),
} }
} else { } else {
@ -506,7 +506,7 @@ impl TypeVar {
} }
} }
pub fn get_inner_type(&self) -> Vec<Arc<Type>> { pub fn get_inner_types(&self) -> Vec<Arc<Type>> {
match self { match self {
Self::Link { tipo } => tipo.get_inner_types(), Self::Link { tipo } => tipo.get_inner_types(),
var => { var => {

View File

@ -89,16 +89,10 @@ impl<'a> CodeGenerator<'a> {
self.build_ir(body, &mut ir_stack, scope); self.build_ir(body, &mut ir_stack, scope);
println!("{:#?}", ir_stack);
self.define_ir(&mut ir_stack); self.define_ir(&mut ir_stack);
println!("{:#?}", ir_stack);
self.convert_opaque_type_to_inner_ir(&mut 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); let mut term = self.uplc_code_gen(&mut ir_stack);
if self.needs_field_access { if self.needs_field_access {
@ -3150,8 +3144,6 @@ impl<'a> CodeGenerator<'a> {
let mut recursion_func_map_to_add = recursion_func_map.clone(); 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() { for func_index in func_index_map.clone().iter() {
let func = func_index.0; let func = func_index.0;
@ -3276,11 +3268,6 @@ impl<'a> CodeGenerator<'a> {
self.build_ir(&function.body, &mut func_ir, scope.to_vec()); 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 param_types = constructor.tipo.arg_types().unwrap();
let mut mono_types: IndexMap<u64, Arc<Type>> = IndexMap::new(); let mut mono_types: IndexMap<u64, Arc<Type>> = IndexMap::new();
@ -3288,22 +3275,17 @@ impl<'a> CodeGenerator<'a> {
for (index, arg) in function.arguments.iter().enumerate() { for (index, arg) in function.arguments.iter().enumerate() {
if arg.tipo.is_generic() { if arg.tipo.is_generic() {
let mut map = mono_types.into_iter().collect_vec(); let mut map = mono_types.into_iter().collect_vec();
map.append(&mut get_generics_and_type( let param_type = &param_types[index];
&arg.tipo,
&param_types[index], map.append(&mut get_generics_and_type(&arg.tipo, &param_type));
));
mono_types = map.into_iter().collect(); mono_types = map.into_iter().collect();
} }
} }
println!("DID WE GET HERE");
let (variant_name, func_ir) = let (variant_name, func_ir) =
monomorphize(func_ir, mono_types, &constructor.tipo); monomorphize(func_ir, mono_types, &constructor.tipo);
println!("AND HERE?");
let function_key = FunctionAccessKey { let function_key = FunctionAccessKey {
module_name: module.clone(), module_name: module.clone(),
function_name: non_variant_function_key.function_name, 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() { let recursive = if func_calls.get(&function_key).is_some() {
func_calls.remove(&function_key); func_calls.remove(&function_key);
true true
@ -3433,7 +3413,6 @@ impl<'a> CodeGenerator<'a> {
); );
} }
} else { } else {
println!("ARE WE GETTING HERE323323");
for func in to_be_defined_map.clone().iter() { for func in to_be_defined_map.clone().iter() {
if get_common_ancestor(scope, func.1) == scope.to_vec() { if get_common_ancestor(scope, func.1) == scope.to_vec() {
if let Some(index_scope) = func_index_map.get(func.0) { if let Some(index_scope) = func_index_map.get(func.0) {
@ -3456,7 +3435,7 @@ impl<'a> CodeGenerator<'a> {
} }
a => { a => {
let scope = a.scope(); let scope = a.scope();
println!("ARE WE GETTING HERE2222");
for func in to_be_defined_map.clone().iter() { for func in to_be_defined_map.clone().iter() {
if get_common_ancestor(&scope, func.1) == scope.to_vec() { if get_common_ancestor(&scope, func.1) == scope.to_vec() {
if let Some(index_scope) = func_index_map.get(func.0) { 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 // Still to be defined
for func in to_be_defined_map.clone().iter() { for func in to_be_defined_map.clone().iter() {
let index_scope = func_index_map.get(func.0).unwrap(); let index_scope = func_index_map.get(func.0).unwrap();

View File

@ -106,5 +106,5 @@ test flatten_with_2() {
None None
} }
}, },
) == [("b", ""), ("a", "2")] ) == [("a", "2"), ("b", "")]
} }