34 lines
832 B
LLVM
34 lines
832 B
LLVM
; expect: run
|
|
; result: {"variant":"overflow","payload":[]}
|
|
; phase: 0
|
|
; spec: docs/spec/16-standard-library.md §2.1.1; docs/spec/07a-air-instructions.md
|
|
module test.bounded_arithmetic
|
|
air 0
|
|
lang 0.1
|
|
features [effects]
|
|
|
|
type @U64Result = enum { ok(u64), overflow }
|
|
|
|
fn @main() -> @U64Result
|
|
effects []
|
|
entry:
|
|
%a = const.u64 18446744073709551615
|
|
%b = const.u64 1
|
|
%result = call @checked_add(%a, %b)
|
|
ret %result
|
|
|
|
fn @checked_add(%a: u64, %b: u64) -> @U64Result
|
|
effects []
|
|
entry:
|
|
%maximum = const.u64 18446744073709551615
|
|
%remaining = sub.checked.u64 %maximum, %a
|
|
%overflows = gt.u64 %b, %remaining
|
|
cbr %overflows, @failure(), @success()
|
|
failure:
|
|
%error = enum.make @U64Result.overflow()
|
|
ret %error
|
|
success:
|
|
%sum = add.checked.u64 %a, %b
|
|
%ok = enum.make @U64Result.ok(%sum)
|
|
ret %ok
|