Stream: compiler development

Topic: Upgrade zig std.math.absInt


view this post on Zulip Luke Boswell (Jul 24 2024 at 08:52):

Here is an example where I'm not 100% on how we upgrade from zig 0.11.0 to 0.13.0.

https://github.com/roc-lang/roc/blob/d5db3137a3d8da46f92c31b6bf088bc495f759c2/crates/compiler/builtins/bitcode/src/dec.zig#L467

Docs for the old version
https://ziglang.org/documentation/0.11.0/std/#A;std:math.absInt

Docs for the new builtin I think may be what we want
https://ziglang.org/documentation/0.13.0/#abs

Or maybe one of these
https://ziglang.org/documentation/0.13.0/std/#std.math

current

const numerator_abs_i128 = math.absInt(numerator_i128) catch {
    // Currently, if you try to do multiplication on i64::MIN, panic
    // unless you're specifically multiplying by 0 or 1.
    //
    // Maybe we could support more cases in the future
    if (denominator_i128 == one_point_zero_i128) {
        return self;
    } else {
        roc_panic("Decimal division overflow in numerator!", 0);
    }
};

zig 13 error

/nix/store/xgga5anfp46iafhlrisqqz2b66jw2alb-zig-0.13.0/lib/std/math.zig:1:1: note: struct declared here
const builtin = @import("builtin");
^~~~~
src/dec.zig:1501:20: note: called from here
    const result = @call(.always_inline, RocDec.abs, .{arg}) catch {
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    comptime_1: src/main.zig:24:20
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
src/dec.zig:467:40: error: root struct of file 'math' has no member named 'absInt'
        const numerator_abs_i128 = math.absInt(numerator_i128) catch {
                                   ~~~~^~~~~~~

view this post on Zulip Brendan Hansknecht (Jul 24 2024 at 15:29):

Yeah, we want @abs here.

view this post on Zulip Brendan Hansknecht (Jul 24 2024 at 15:29):

Instead of erroring on failure, it returns an unsigned type

view this post on Zulip Brendan Hansknecht (Jul 24 2024 at 15:30):

So i128 becomes a u128

view this post on Zulip Brendan Hansknecht (Jul 24 2024 at 15:30):

Catching the error will instead happen when we ensure the u128 fits back into an i128

view this post on Zulip Brendan Hansknecht (Jul 24 2024 at 15:32):

so @abs follow by if (value < std.math.maxInt(i128)) ...


Last updated: Jul 06 2025 at 12:14 UTC