core: Add assertion for when `range` has step of 0

David Mak 2023-09-05 15:39:11 +08:00
parent 7b9f8e8aaa
commit 74034fe473
1 changed files with 10 additions and 4 deletions

View File

@ -279,10 +279,16 @@ pub fn gen_for<'ctx, 'a, G: CodeGenerator>(
ctx.builder.build_store(i, start);
// Pre-Loop Checks:
// - step == 0 -> ValueError
// - start < stop for step > 0 || start > stop for step < 0
// TODO: Generate step == 0 -> raise ValueError
// Check "If step is zero, ValueError is raised."
let rangenez = ctx.builder.build_int_compare(IntPredicate::NE, step, int32.const_zero(), "");
ctx.make_assert(
generator,
rangenez,
"ValueError",
"range() arg 3 must not be zero",
[None, None, None],
ctx.current_loc
);
ctx.builder.build_conditional_branch(
gen_in_range_check(ctx, start, stop, step),