Fix List.append off-by-one bug

This commit is contained in:
Richard Feldman 2020-07-22 19:38:33 -04:00
parent 2ef37adc74
commit 2af9854b24

View file

@ -2232,28 +2232,21 @@ fn list_append<'a, 'ctx, 'env>(
let index_name = "#index";
let index_alloca = builder.build_alloca(ctx.i64_type(), index_name);
// FIRST LOOP
// The index variable begins at 0 and increments
// on each iteration of the loop
builder.build_store(index_alloca, ctx.i64_type().const_int(0, false));
// FIRST LOOP
{
let first_loop_bb =
ctx.append_basic_block(parent, "first_list_append_loop");
builder.build_unconditional_branch(first_loop_bb);
builder.position_at_end(first_loop_bb);
// #index = #index + 1
let curr_first_loop_index = builder
.build_load(index_alloca, index_name)
.into_int_value();
let next_first_loop_index = builder.build_int_add(
curr_first_loop_index,
ctx.i64_type().const_int(1, false),
"nextindex",
);
builder.build_store(index_alloca, next_first_loop_index);
let first_list_ptr =
load_list_ptr(builder, first_list_wrapper, ptr_type);
@ -2282,10 +2275,19 @@ fn list_append<'a, 'ctx, 'env>(
// Mutate the new array in-place to change the element.
builder.build_store(combined_list_elem_ptr, first_list_elem);
// #index = #index + 1
let next_first_loop_index = builder.build_int_add(
curr_first_loop_index,
ctx.i64_type().const_int(1, false),
"nextindex",
);
builder.build_store(index_alloca, next_first_loop_index);
// #index < first_list_len
let first_loop_end_cond = builder.build_int_compare(
IntPredicate::ULT,
curr_first_loop_index,
next_first_loop_index,
first_list_len,
"loopcond",
);
@ -2298,28 +2300,24 @@ fn list_append<'a, 'ctx, 'env>(
first_loop_bb,
after_first_loop_bb,
);
builder.position_at_end(after_first_loop_bb);
// SECOND LOOP
builder.position_at_end(after_first_loop_bb);
}
// Reset the index variable to 0
builder.build_store(index_alloca, ctx.i64_type().const_int(0, false));
// SECOND LOOP
{
let second_loop_bb =
ctx.append_basic_block(parent, "second_list_append_loop");
builder.build_unconditional_branch(second_loop_bb);
builder.position_at_end(second_loop_bb);
// #index = #index + 1
let curr_second_index = builder
.build_load(index_alloca, index_name)
.into_int_value();
let next_second_index = builder.build_int_add(
curr_second_index,
ctx.i64_type().const_int(1, false),
"nextindex",
);
builder.build_store(index_alloca, next_second_index);
let second_list_ptr =
load_list_ptr(builder, second_list_wrapper, ptr_type);
@ -2361,10 +2359,19 @@ fn list_append<'a, 'ctx, 'env>(
// Mutate the new array in-place to change the element.
builder.build_store(combined_list_elem_ptr, second_list_elem);
// #index = #index + 1
let next_second_index = builder.build_int_add(
curr_second_index,
ctx.i64_type().const_int(1, false),
"increment_index",
);
builder.build_store(index_alloca, next_second_index);
// #index < second_list_len
let second_loop_end_cond = builder.build_int_compare(
IntPredicate::ULT,
curr_second_index,
next_second_index,
second_list_len,
"loopcond",
);
@ -2415,6 +2422,7 @@ fn list_append<'a, 'ctx, 'env>(
collection(ctx, ptr_bytes),
"cast_collection",
)
}
};
build_basic_phi2(