Open fubark opened 1 month ago
I am able to reproduce this on windows without the foo
function:
const std = @import("std");
pub const Foo = packed struct {
a: u31,
b: bool,
pub fn init() Foo {
return .{ .a = undefined, .b = true };
}
};
pub fn main() !void {
const a = Foo.init();
std.debug.print("{} {b}\n", .{a.b, @as(u32, @bitCast(a))});
// Expected "true", and a non zero value
// Found "false", 0
}
$ zig run foo.zig
true 10111000111110011110110010010000
$ zig run foo.zig
false 10111010011011010010010110000
Duplicate of #20095
@Vexu I don't believe this is a duplicate. #20095 is about reading undefined
fields, but we are reading a field that has been set to true
and seeing undefined behavior.
I believe this issue is only in the llvm backend:
const exe = b.addExecutable(.{
.name = "foo2",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.use_llvm = false,
});
true 10000000000000000000000000000000
true 10000000000000000000000000000000
true 10000000000000000000000000000000
true 10000000000000000000000000000000
I'd expect it to be caused by the same issue but we can keep this open too.
Zig Version
0.14.0-dev.839+a931bfada
Steps to Reproduce and Observed Behavior
Expected Behavior
There seems to be an issue with passing a packed struct into a function when the first field is initialized as undefined.