sam701 / zig-toml

Zig TOML (v1.0.0) parser
MIT License
35 stars 13 forks source link

Fix parsing optionals in ReleaseFast build #10

Closed leki75 closed 3 weeks ago

leki75 commented 4 months ago

Parsing optionals fail at runtime when we use ReleaseFast optimization.

const std = @import("std");
const toml = @import("zig-toml");

const Opts = struct {
    sub: ?Sub,
};

const Sub = struct {
    id: u16,
    name: []const u8,
};

const config_toml =
    \\ [sub]
    \\ id = 1
    \\ name = "planet"
;

test "parse optional" {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    var parser = toml.Parser(Opts).init(allocator);
    defer parser.deinit();

    const parsed = try parser.parseString(config_toml);
    defer parsed.deinit();

    try std.testing.expect(parsed.value.sub != null);
    try std.testing.expectEqual(1, parsed.value.sub.?.id);
    try std.testing.expectEqualStrings("planet", parsed.value.sub.?.name);
}

Running the tests:

❯ zig build test
❯ zig build test --release=fast
test
└─ run test 0/1 passed, 1 failed
error: 'main.test.parse optional' failed
error: while executing test 'main.test.parse optional', the following test command failed:
/Users/leki/temp/zig/.zig-cache/o/d91ddfd592627a7bc8c892de1cb37bab/test --listen=- 
Build Summary: 1/3 steps succeeded; 1 failed; 0/1 tests passed; 1 failed (disable with --summary none)
test transitive failure
└─ run test 0/1 passed, 1 failed
error: the following build command failed with exit code 1:
/Users/leki/temp/zig/.zig-cache/o/c54e788abb8f5b1baf72e7ee2970d86b/build /opt/homebrew/Cellar/zig/0.13.0/bin/zig /Users/leki/temp/zig /Users/leki/temp/zig/.zig-cache /Users/leki/.cache/zig --seed 0xc50410e9 -Zd6d41f6c26d96811 test --release=fast
leki75 commented 3 weeks ago

@sam701 Will you consider merging this PR, please?

sam701 commented 3 weeks ago

@leki75 I'm very sorry, I lost your PR from my radar. Merged. Thanks for your contribution!! 👍