diff --git a/nac3core/src/codegen/object/list.rs b/nac3core/src/codegen/object/list.rs index 86edcbf8..a236239d 100644 --- a/nac3core/src/codegen/object/list.rs +++ b/nac3core/src/codegen/object/list.rs @@ -1,3 +1,5 @@ +use inkwell::types::BasicType; + use crate::{ codegen::{model::*, CodeGenContext, CodeGenerator}, typecheck::typedef::{iter_type_vars, Type, TypeEnum}, @@ -34,7 +36,8 @@ impl<'ctx, Item: Model<'ctx>> StructKind<'ctx> for List { /// A NAC3 Python List object. #[derive(Debug, Clone, Copy)] pub struct ListObject<'ctx> { - /// Typechecker type of the list items + /// Typechecker type of the list items. Could be [`TypeEnum::TVar`] if unresolved (like + /// in the case of empty lists and the typechecker does not have enough hints). pub item_type: Type, pub instance: Instance<'ctx, Ptr>>>>, } @@ -63,7 +66,15 @@ impl<'ctx> ListObject<'ctx> { } }; - let plist = Ptr(Struct(List { item: Any(ctx.get_llvm_type(generator, item_type)) })); + // If `item_type` is unresolved, the list's ptr will default to `size_t*` + // as a placeholder because there are no opaque pointers in LLVM 14. + let item_type_llvm = if let TypeEnum::TVar { .. } = &*ctx.unifier.get_ty(item_type) { + generator.get_size_type(ctx.ctx).as_basic_type_enum() + } else { + ctx.get_llvm_type(generator, item_type) + }; + + let plist = Ptr(Struct(List { item: Any(item_type_llvm) })); // Create object let value = plist.check_value(generator, ctx.ctx, object.value).unwrap(); @@ -121,7 +132,13 @@ impl<'ctx> ListObject<'ctx> { item_type: Type, len: Instance<'ctx, Int>, ) -> Self { - let item_type_llvm = ctx.get_llvm_type(generator, item_type); + // If `item_type` is unresolved, the list's ptr will default to `size_t*` + // as a placeholder because there are no opaque pointers in LLVM 14. + let item_type_llvm = if let TypeEnum::TVar { .. } = &*ctx.unifier.get_ty(item_type) { + generator.get_size_type(ctx.ctx).as_basic_type_enum() + } else { + ctx.get_llvm_type(generator, item_type) + }; let items = Any(item_type_llvm).array_alloca(generator, ctx, len.value); let instance = Struct(List { item: Any(item_type_llvm) }).alloca(generator, ctx);