From 74034fe4737f10ebc3ea17a318bdfb6a60511d3c 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 --- 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 77ed897..2ee2aed 100644 --- a/nac3core/src/codegen/stmt.rs +++ b/nac3core/src/codegen/stmt.rs @@ -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),