mirror of
https://github.com/roc-lang/roc.git
synced 2024-09-21 07:49:17 +03:00
fix bug with incrementing elem loop on empty range
This commit is contained in:
parent
d212dffa1a
commit
d3cb0e03d4
@ -634,33 +634,40 @@ where
|
||||
|
||||
let entry = env.builder.get_insert_block().unwrap();
|
||||
|
||||
// constant 1i64
|
||||
// constant 1usize
|
||||
let one = env.ptr_int().const_int(1, false);
|
||||
let zero = env.ptr_int().const_zero();
|
||||
|
||||
// allocate a stack slot for the current index
|
||||
let index_alloca = builder.build_alloca(env.ptr_int(), index_name);
|
||||
builder.build_store(index_alloca, env.ptr_int().const_zero());
|
||||
builder.build_store(index_alloca, zero);
|
||||
|
||||
let loop_bb = ctx.append_basic_block(parent, "loop");
|
||||
builder.build_unconditional_branch(loop_bb);
|
||||
builder.position_at_end(loop_bb);
|
||||
|
||||
let current_index_phi = env.builder.build_phi(env.ptr_int(), "current_index");
|
||||
let current_index = current_index_phi.as_basic_value().into_int_value();
|
||||
|
||||
let next_index = builder.build_int_add(current_index, one, "next_index");
|
||||
|
||||
current_index_phi.add_incoming(&[(&next_index, loop_bb), (&env.ptr_int().const_zero(), entry)]);
|
||||
|
||||
// The body of the loop
|
||||
loop_fn(current_index);
|
||||
|
||||
// #index < end
|
||||
let loop_end_cond = bounds_check_comparison(builder, next_index, end);
|
||||
|
||||
let after_loop_bb = ctx.append_basic_block(parent, "after_outer_loop_2");
|
||||
let after_loop_bb = ctx.append_basic_block(parent, "after_loop");
|
||||
|
||||
let loop_end_cond = bounds_check_comparison(builder, zero, end);
|
||||
builder.build_conditional_branch(loop_end_cond, loop_bb, after_loop_bb);
|
||||
|
||||
{
|
||||
builder.position_at_end(loop_bb);
|
||||
|
||||
let current_index_phi = env.builder.build_phi(env.ptr_int(), "current_index");
|
||||
let current_index = current_index_phi.as_basic_value().into_int_value();
|
||||
|
||||
let next_index = builder.build_int_add(current_index, one, "next_index");
|
||||
|
||||
current_index_phi
|
||||
.add_incoming(&[(&next_index, loop_bb), (&env.ptr_int().const_zero(), entry)]);
|
||||
|
||||
// The body of the loop
|
||||
loop_fn(current_index);
|
||||
|
||||
// #index < end
|
||||
let loop_end_cond = bounds_check_comparison(builder, next_index, end);
|
||||
|
||||
builder.build_conditional_branch(loop_end_cond, loop_bb, after_loop_bb);
|
||||
}
|
||||
|
||||
builder.position_at_end(after_loop_bb);
|
||||
|
||||
index_alloca
|
||||
|
Loading…
Reference in New Issue
Block a user