Integer Overflow
integer_overflow.nr20 lines
integer_overflow.nr· neuro
// Neuro Example: Integer overflow semantics
//
// Debug builds (compiled with -O0) trap at runtime when a `+`, `-`, or `*`
// on an integer type overflows, turning a silent miscalculation into an
// immediate abort. Release builds (-O1..-O3) wrap using two's complement,
// matching hardware behavior with zero overhead.
//
// This program stays within range, so it returns the same value (55) in both
// debug and release builds. Push any of these sums past the type's maximum to
// observe the debug-build trap.
func main() -> i32 {
mut acc: i32 = 0
for i in 1..=10 {
acc = acc + i // 1+2+...+10 = 55, well within i32 range
println("+ {i} -> {acc}")
}
println("total (same in debug and release) = {acc}")
return acc
}
neurc run integer_overflow.nr
+ 1 -> 1
+ 2 -> 3
+ 3 -> 6
+ 4 -> 10
+ 5 -> 15
+ 6 -> 21
+ 7 -> 28
+ 8 -> 36
+ 9 -> 45
+ 10 -> 55
total (same in debug and release) = 55