From 2cdb057d208cb0b42fa5f8b9c7461d64843818b8 Mon Sep 17 00:00:00 2001 From: David Mak Date: Mon, 15 Jul 2024 16:56:55 +0800 Subject: [PATCH] artiq/symbol_resolver: Handle type of zero-length lists --- nac3artiq/src/symbol_resolver.rs | 9 ++++++++- nac3core/src/codegen/expr.rs | 6 +++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/nac3artiq/src/symbol_resolver.rs b/nac3artiq/src/symbol_resolver.rs index cc4e92e9..8ed5ea7b 100644 --- a/nac3artiq/src/symbol_resolver.rs +++ b/nac3artiq/src/symbol_resolver.rs @@ -991,8 +991,15 @@ impl InnerResolver { } _ => unreachable!("must be list"), }; - let ty = ctx.get_llvm_type(generator, elem_ty); let size_t = generator.get_size_type(ctx.ctx); + let ty = if len == 0 + && matches!(&*ctx.unifier.get_ty_immutable(elem_ty), TypeEnum::TVar { .. }) + { + // The default type for zero-length lists of unknown element type is size_t + size_t.into() + } else { + ctx.get_llvm_type(generator, elem_ty) + }; let arr_ty = ctx .ctx .struct_type(&[ty.ptr_type(AddressSpace::default()).into(), size_t.into()], false); diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index c42c8444..38ac9a63 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -951,9 +951,9 @@ pub fn destructure_range<'ctx>( /// Allocates a List structure with the given [type][ty] and [length]. The name of the resulting /// LLVM value is `{name}.addr`, or `list.addr` if [name] is not specified. /// -/// Setting `ty` to [`None`] implies that the list does not have a known element type, which is only -/// valid for empty lists. It is undefined behavior to generate a sized list with an unknown element -/// type. +/// Setting `ty` to [`None`] implies that the list is empty **and** does not have a known element +/// type, and will therefore set the `list.data` type as `size_t*`. It is undefined behavior to +/// generate a sized list with an unknown element type. pub fn allocate_list<'ctx, G: CodeGenerator + ?Sized>( generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>,