Repository navigation
undefined parameter not set to 0xaa bytes in the llvm backend #24100
Description
Activity
- addedbugObserved behavior contradicts documented or intended behaviorObserved behavior contradicts documented or intended behaviorcontributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.This issue is limited in scope and/or knowledge of Zig internals.backend-llvmThe LLVM backend outputs an LLVM IR Module.The LLVM backend outputs an LLVM IR Module.
on Jun 6, 2025 @andrewrk may I please take up this issue? It might take me some time since I am starting out with
zig, but I wish to contribute to the project.@kmr-srbh you can just start working on it, you don't need to ask for permission! :)
Reacted by Saurabh KumarCan confirm this is still an issue as of
0.16.0-dev.1301+cbfa87cbe.Would like to add there's some extra weird behavior when declaring single-byte variables:
- just for
consts (notvar) - call me crazy but the order of statements seems to matter?!
const std = @import("std"); const print = std.debug.print; const expect = std.testing.expect; pub fn main() !void {} test "constant first" { // Only failure. const c: u8 = undefined; try expect(c == 0xAA); var v: u8 = undefined; try expect(v == 0xAA); v = undefined; try expect(v == 0xAA); } test "constant middle" { var v: u8 = undefined; try expect(v == 0xAA); const c: u8 = undefined; try expect(c == 0xAA); v = undefined; try expect(v == 0xAA); } test "constant last" { var v: u8 = undefined; try expect(v == 0xAA); v = undefined; try expect(v == 0xAA); const c: u8 = undefined; try expect(c == 0xAA); }
Output:
$ file -h ../latest ../latest: symbolic link to /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/zig $ ../latest test src/main.zig -fllvm 1/3 main.test.constant first...FAIL (TestUnexpectedResult) /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x1004887a3 in expect (test) if (!ok) return error.TestUnexpectedResult; ^ /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:9:5: 0x100488873 in test.constant first (test) try expect(c == 0xAA); ^ 2 passed; 0 skipped; 1 failed. error: the following test command failed with exit code 1: .zig-cache/o/2f45fb1504487078861b4e1bba8ecc8b/test --seed=0xf588a363
- just for
call me crazy but the order of statements seems to matter?!
After a bit more finagling, it seems (somewhat arbitrarily) dependant on the size of the type of the
consttoo (with 2, 6, 8, and 12 bytes seemingly working!?)Note: this is literally my first piece of
comptimezig so please take it with a grain of salt...const std = @import("std"); const print = std.debug.print; const warn = std.log.warn; const expect = std.testing.expect; pub fn main() !void {} fn default_val_for_type(comptime typ: type) comptime_int { var v = 0; var bits = 1; while (bits <= @bitSizeOf(typ)) : (bits += 1) { v = (v << 1) | (bits % 2); } return v; } comptime { const types = .{ u8, u12, u16, u24, u32, u48, u64, u96, u128, u192, u256, u384, u512 }; // NOTE: putting `u8` to the back somehow makes it work for `u8` too?! // const types = .{ u12, u16, u24, u32, u48, u64, u96, u128, u192, u256, u384, u512, u8 }; for (types) |typ| { _ = struct { test { const expected = default_val_for_type(typ); warn("############################### {s} -- 0x{x:}", .{ @typeName(typ), expected }); const c: typ = undefined; try expect(c == expected); // var v: typ = undefined; // Uncomment => different results?!?! // try expect(v == expected); // v = undefined; // try expect(v == expected); warn("Passed for {s}!\n", .{@typeName(typ)}); } }; } }
Output:
[default] (warn): ############################### u8 -- 0xaa 1/13 main.comptime__struct_149.test_0...FAIL (TestUnexpectedResult) /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test) if (!ok) return error.TestUnexpectedResult; ^ /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a4120b in test_0 (test) try expect(c == expected); ^ [default] (warn): ############################### u12 -- 0xaaa 2/13 main.comptime__struct_150.test_0...FAIL (TestUnexpectedResult) /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test) if (!ok) return error.TestUnexpectedResult; ^ /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a419eb in test_0 (test) try expect(c == expected); ^ [default] (warn): ############################### u16 -- 0xaaaa [default] (warn): Passed for u16! [default] (warn): ############################### u24 -- 0xaaaaaa 4/13 main.comptime__struct_154.test_0...FAIL (TestUnexpectedResult) /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test) if (!ok) return error.TestUnexpectedResult; ^ /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a428bb in test_0 (test) try expect(c == expected); ^ [default] (warn): ############################### u32 -- 0xaaaaaaaa 5/13 main.comptime__struct_156.test_0...FAIL (TestUnexpectedResult) /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test) if (!ok) return error.TestUnexpectedResult; ^ /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a42fa7 in test_0 (test) try expect(c == expected); ^ [default] (warn): ############################### u48 -- 0xaaaaaaaaaaaa [default] (warn): Passed for u48! [default] (warn): ############################### u64 -- 0xaaaaaaaaaaaaaaaa [default] (warn): Passed for u64! [default] (warn): ############################### u96 -- 0xaaaaaaaaaaaaaaaaaaaaaaaa [default] (warn): Passed for u96! [default] (warn): ############################### u128 -- 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 9/13 main.comptime__struct_164.test_0...FAIL (TestUnexpectedResult) /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test) if (!ok) return error.TestUnexpectedResult; ^ /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a4526f in test_0 (test) try expect(c == expected); ^ [default] (warn): ############################### u192 -- 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 10/13 main.comptime__struct_166.test_0...FAIL (TestUnexpectedResult) /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test) if (!ok) return error.TestUnexpectedResult; ^ /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a45a53 in test_0 (test) try expect(c == expected); ^ [default] (warn): ############################### u256 -- 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 11/13 main.comptime__struct_168.test_0...FAIL (TestUnexpectedResult) /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test) if (!ok) return error.TestUnexpectedResult; ^ /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a47c8b in test_0 (test) try expect(c == expected); ^ [default] (warn): ############################### u384 -- 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 12/13 main.comptime__struct_170.test_0...FAIL (TestUnexpectedResult) /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test) if (!ok) return error.TestUnexpectedResult; ^ /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a4a4df in test_0 (test) try expect(c == expected); ^ [default] (warn): ############################### u512 -- 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 13/13 main.comptime__struct_172.test_0...FAIL (TestUnexpectedResult) /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test) if (!ok) return error.TestUnexpectedResult; ^ /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a4dddf in test_0 (test) try expect(c == expected); ^ 4 passed; 0 skipped; 9 failed. error: the following test command failed with exit code 1: .zig-cache/o/cd0e2620c51d4e7991d70f37a5320d2e/test --seed=0xa9e143baPS: being able to dynamically name comptime tests like
test @typeName(typ)would have been pretty slick.
Zig Version
0.15.0-dev.734+adc4418ba
Steps to Reproduce and Observed Behavior
Expected Behavior
Same behavior as the x86 backend: