Appendix B: Undefined Behavior and Runtime Panics

Gruel currently has no undefined behavior. All operations in Gruel have defined semantics: they either complete successfully, fail to compile, or cause a runtime panic.

This is a deliberate design choice. Where other systems languages define certain conditions as undefined behavior (allowing implementations to assume they never occur), Gruel instead detects these conditions and responds with a defined runtime panic.

Future versions of Gruel may introduce undefined behavior for specific low-level operations (such as unchecked arithmetic or raw pointer manipulation), but these will be explicitly marked as such and will require opt-in syntax.

Runtime Panics

Gruel detects certain error conditions at runtime and responds with a panic, terminating the program with a specific exit code.

Integer Overflow

Signed or unsigned integer arithmetic that overflows the representable range MUST wrap around modulo 2^N (where N is the bit width of the type). Integer overflow is not a runtime panic. See chapter 8.1 for details.

Division by Zero

Division or remainder with a divisor of zero MUST cause a runtime panic.

Operations affected:

  • Division (/)
  • Remainder (%)

Runtime behavior: Panic with exit code 101.

Array Bounds Violation

Accessing an array element with an index outside the valid range [0, length) MUST cause a runtime panic.

Operations affected:

  • Array indexing (arr[i])
  • Array element assignment (arr[i] = v)

Runtime behavior: Panic with exit code 101.

Exit Codes

ConditionExit Code
Division by zero101
Array out of bounds101

All runtime panics produce exit code 101, matching Rust's convention for unwinding panics.