Integer Overflow
Integer overflow during an arithmetic operation MUST cause a runtime panic: when the mathematical result of the operation falls outside the range [min, max] of its integer type, the operation traps rather than wrapping or truncating (core calculus docs/formal/01-core-calculus.md §6.4, rule (D-Arith-Trap), and the neg (min_T)_T → ↯overflow case for unary negation).
On an overflow trap, the program MUST terminate with exit code 101 — the panic exit code of Appendix B — after printing an error message identifying the overflow (core calculus docs/formal/01-core-calculus.md §6.12, rule (Result-Panic)).
The following operations MAY overflow (core calculus docs/formal/01-core-calculus.md §6.4: +, -, *, and neg trap by rule (D-Arith-Trap); the min / -1 and min % -1 cases trap by rule (D-Div-Overflow)):
- Addition (
+) - Subtraction (
-) - Multiplication (
*) - Negation (
-unary) - Division (
/) and remainder (%), exactly when the dividend is the signed type's minimum value and the divisor is-1(the quotient-MINis not representable; the remainder operation overflows in the same case even though its mathematical result would be0)
fn main() -> i32 {
2147483647 + 1 // Runtime error: integer overflow
}
fn main() -> i32 {
-2147483648 - 1 // Runtime error: integer overflow
}
Wrapping arithmetic that does not panic on overflow is available through the @wrapping_add, @wrapping_sub, and @wrapping_mul intrinsics (§4.13, rules 4.13:97–4.13:102), which reduce their result modulo 2^N instead of trapping. Wrapping forms of the arithmetic operators remain future work.