diagnostic codes / Traps
E9003 division-by-zero-trap
A division or remainder had a zero divisor.
E9003division-by-zero-trapdiv and mod by zero are defined traps; guard the divisor
| severity | trap |
|---|---|
| band | Traps |
| in the v0 bootstrap | emitted |
| explain | ralt explain E9003 |
Both `/` and `%` trap on a zero divisor. Integer division truncates toward zero, and the remainder takes the sign of the dividend; a zero divisor has no defined result in either operation, so it is a trap rather than a guess.
What it looks like
e9003.tp
use tier core; fn main() -> i32 { let z = 0; print_i32(7 / z); return 0; }
ralt run e9003.tp tripon trap E9003 division-by-zero-trap at e9003.tp:5 echo $? 101
What to do
- Check the divisor before the operation.
- Return an `opt` or a `result` from the function when zero is a valid input and not a bug.
- `%` with a zero divisor is the same trap, not a separate code.
Why the code exists
In C, division by zero is undefined for integers. A defined trap means a fuzzer can trust the exit code and a reader can trust the report.
What ralt explain says
ralt explain E9003 E9003 severity: error name: division-by-zero-trap hint: div and mod by zero are defined traps; guard the divisor
Related
- Traps and no-UB /docs/language/traps