From 24c5020e42b40fef7bdb955a5512879e1df5108d Mon Sep 17 00:00:00 2001 From: Tristan Menzel Date: Thu, 25 Jan 2024 12:15:53 -0800 Subject: [PATCH] feat: Optimise constant mod expressions and pre-check for div 0 errors --- src/puya/ir/optimize/arithmetic.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/puya/ir/optimize/arithmetic.py b/src/puya/ir/optimize/arithmetic.py index aa9ff9e453..d1e9793cd3 100644 --- a/src/puya/ir/optimize/arithmetic.py +++ b/src/puya/ir/optimize/arithmetic.py @@ -241,7 +241,13 @@ def try_simplify_arithmetic_ops(value: models.ValueProvider) -> models.ValueProv case AVMOp.mul: c = a_const * b_const case AVMOp.div_floor: + if b_const == 0: + raise CodeError("// would fail at runtime (div 0)", b.source_location) c = a_const // b_const + case AVMOp.mod: + if b_const == 0: + raise CodeError("mod would fail at runtime (div 0)", b.source_location) + c = a_const % b_const case AVMOp.lt: c = 1 if a_const < b_const else 0 case AVMOp.lte: