Update ListLen and ListGetUnsafe to not use Nat

This commit is contained in:
Richard Feldman 2024-01-21 13:53:41 -05:00
parent 2e72021a74
commit 97f21e65fe
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
4 changed files with 60 additions and 49 deletions

View file

@ -143,6 +143,8 @@ pub(crate) fn list_get_unsafe<'a, 'ctx>(
layout_interner,
layout_interner.get_repr(element_layout),
);
// listGetUnsafe takes a U64, but we need to convert that to usize for index calculation.
let elem_index = builder.new_build_int_cast(elem_index, env.ptr_int(), "u64_to_usize");
let ptr_type = elem_type.ptr_type(AddressSpace::default());
// Load the pointer to the array data
let array_data_ptr = load_list_ptr(builder, wrapper_struct, ptr_type);
@ -423,7 +425,7 @@ fn bounds_check_comparison<'ctx>(
builder.new_build_int_compare(IntPredicate::ULT, elem_index, len, "bounds_check")
}
/// List.len : List * -> Nat
/// List.len : List * -> usize (return value will be cast to usize in user-facing API)
pub(crate) fn list_len<'ctx>(
builder: &Builder<'ctx>,
wrapper_struct: StructValue<'ctx>,

View file

@ -618,10 +618,15 @@ pub(crate) fn run_low_level<'a, 'ctx>(
)
}
ListLen => {
// List.len : List * -> Nat
// List.len : List * -> U64
arguments!(list);
list_len(env.builder, list.into_struct_value()).into()
let len_usize = list_len(env.builder, list.into_struct_value());
// List.len returns U64, although length is stored as usize
env.builder
.new_build_int_cast(len_usize, env.context.i64_type(), "usize_to_u64")
.into()
}
ListGetCapacity => {
// List.capacity: List a -> Nat