Skip to content

Commit

Permalink
Update List.replace to no longer use Nat
Browse files Browse the repository at this point in the history
  • Loading branch information
rtfeldman committed Jan 23, 2024
1 parent 2f3d56a commit cbfc92f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions crates/compiler/builtins/bitcode/src/list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ pub fn listConcat(list_a: RocList, list_b: RocList, alignment: u32, element_widt

pub fn listReplaceInPlace(
list: RocList,
index: usize,
index: u64,
element: Opaque,
element_width: usize,
out_element: ?[*]u8,
Expand All @@ -948,14 +948,15 @@ pub fn listReplaceInPlace(
// at the time of writing, the function is implemented roughly as
// `if inBounds then LowLevelListReplace input index item else input`
// so we don't do a bounds check here. Hence, the list is also non-empty,
// because inserting into an empty list is always out of bounds
return listReplaceInPlaceHelp(list, index, element, element_width, out_element);
// because inserting into an empty list is always out of bounds,
// and it's always safe to cast index to usize.
return listReplaceInPlaceHelp(list, @as(usize, @intCast(index)), element, element_width, out_element);
}

pub fn listReplace(
list: RocList,
alignment: u32,
index: usize,
index: u64,
element: Opaque,
element_width: usize,
out_element: ?[*]u8,
Expand All @@ -965,8 +966,9 @@ pub fn listReplace(
// at the time of writing, the function is implemented roughly as
// `if inBounds then LowLevelListReplace input index item else input`
// so we don't do a bounds check here. Hence, the list is also non-empty,
// because inserting into an empty list is always out of bounds
return listReplaceInPlaceHelp(list.makeUnique(alignment, element_width), index, element, element_width, out_element);
// because inserting into an empty list is always out of bounds,
// and it's always safe to cast index to usize.
return listReplaceInPlaceHelp(list.makeUnique(alignment, element_width), @as(usize, @intCast(index)), element, element_width, out_element);
}

inline fn listReplaceInPlaceHelp(
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/gen_llvm/src/llvm/build_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ pub(crate) fn list_drop_at<'a, 'ctx>(
)
}

/// List.replace_unsafe : List elem, Nat, elem -> { list: List elem, value: elem }
/// List.replace_unsafe : List elem, U64, elem -> { list: List elem, value: elem }
pub(crate) fn list_replace_unsafe<'a, 'ctx>(
env: &Env<'a, 'ctx, '_>,
layout_interner: &STLayoutInterner<'a>,
Expand Down

0 comments on commit cbfc92f

Please sign in to comment.