{"record":{"id":"2c50f2d35a6b81d3","repo":"BoundaryML/baml","slug":"modulo-by-zero-in-operator","errorCode":null,"errorMessage":"modulo by zero in %= operator","messagePattern":"modulo by zero in %= operator","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":960,"sourceCode":"                                        bail!(\"division by zero in /= operator\");\n                                    }\n                                    BamlValueWithMeta::Float((a as f64) / b, meta)\n                                }\n                                (\n                                    BamlValueWithMeta::Float(a, meta),\n                                    BamlValueWithMeta::Int(b, _),\n                                ) => {\n                                    if b == 0 {\n                                        bail!(\"division by zero in /= operator\");\n                                    }\n                                    BamlValueWithMeta::Float(a / (b as f64), meta)\n                                }\n                                _ => bail!(\"unsupported types for /= operator\"),\n                            },\n                            AssignOp::ModAssign => match (current_val.clone(), rhs_val.clone()) {\n                                (BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {\n                                    if b == 0 {\n                                        bail!(\"modulo by zero in %= operator\");\n                                    }\n                                    BamlValueWithMeta::Int(a % b, meta)\n                                }\n                                _ => bail!(\"unsupported types for %= operator\"),\n                            },\n                            AssignOp::BitXorAssign => {\n                                match (current_val.clone(), rhs_val.clone()) {\n                                    (\n                                        BamlValueWithMeta::Int(a, meta),\n                                        BamlValueWithMeta::Int(b, _),\n                                    ) => BamlValueWithMeta::Int(a ^ b, meta),\n                                    _ => bail!(\"bitwise ^= requires integer operands\"),\n                                }\n                            }\n                            AssignOp::BitAndAssign => {\n                                match (current_val.clone(), rhs_val.clone()) {\n                                    (\n                                        BamlValueWithMeta::Int(a, meta),","sourceCodeStart":942,"sourceCodeEnd":978,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L942-L978","documentation":"THIR interpreter runtime error: the `%=` (modulo) operator was evaluated with a zero right-hand side. Like division, modulo by zero has no defined result, so evaluation of the compound assignment aborts with this bail at runtime.","triggerScenarios":"Executing `x %= y` where x and y are Int and y equals 0.","commonSituations":"Modulo by a computed value that turned out to be 0 (e.g. bucket size, chunk length from empty config).","solutions":["Check the modulus is nonzero before applying %=.","Return a default value when the modulus is 0.","Fix the upstream computation producing the 0 modulus."],"exampleFix":"// before\nidx %= bucketSize\n// after\nif bucketSize != 0 { idx %= bucketSize } else { idx = 0 }","handlingStrategy":"validation","validationCode":"if (rhs === 0) throw new Error('modulus is zero');","typeGuard":"function isNonZeroInt(v) { return typeof v === 'number' && Number.isInteger(v) && v !== 0; }","tryCatchPattern":"try { applyAssignOp('ModAssign', lhs, rhs); } catch (e) { if (e.message.includes('modulo by zero in %=')) { /* use fallback index */ } else { throw e; } }","preventionTips":["Check modulus != 0 before %=.","Validate bucket/chunk sizes at configuration time.","Provide a fallback value when the modulus is 0."],"tags":["runtime","arithmetic","modulo-by-zero","interpreter"],"backgroundTag":"invalid-argument-value","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}