From b4a9616648cf467e65a41ad78fb194d72a378573 Mon Sep 17 00:00:00 2001 From: David Mak Date: Tue, 5 Sep 2023 15:39:11 +0800 Subject: [PATCH] core: Add assertion for when `range` has step of 0 Aligns with the behavior in Python. --- nac3core/src/codegen/stmt.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/nac3core/src/codegen/stmt.rs b/nac3core/src/codegen/stmt.rs index 5b9910de..93bd7eb6 100644 --- a/nac3core/src/codegen/stmt.rs +++ b/nac3core/src/codegen/stmt.rs @@ -280,10 +280,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),