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

Aligns with the behavior in Python.
This commit is contained in:
David Mak 2023-09-05 15:39:11 +08:00
parent e0de82993f
commit b4a9616648
1 changed files with 10 additions and 4 deletions

View File

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