-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Bit-packed struct with undefined default has inconsistent access #19981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
On
On
Note that |
In #19630 it was made so that having So see: const std = @import("std");
pub const Foo = packed struct(u2) {
a: bool,
b: bool,
};
pub fn main() void {
var foo: Foo = undefined;
_ = &foo;
foo.b = true;
std.debug.print("{}\n", .{foo});
std.debug.print("{}\n", .{foo.b});
} which works fine. Might be talking non-sense, but I believe this is what is happening. |
If that's the expected/desired behaviour with: const std = @import("std");
pub const Foo = packed struct(u2) {
a: bool = undefined,
b: bool,
};
pub fn main() void {
var foo = Foo{
.b = true,
};
_ = &foo;
std.debug.print("{}\n", .{foo});
std.debug.print("{}\n", .{foo.b});
} consistent with that expectation (master branch) does mark as undef: store i2 undef, ptr %2, align 1, !dbg !2322 however, simply swapping field order: pub const Foo = packed struct(u2) {
b: bool,
a: bool = undefined,
}; the IR becomes inconsistent with that expectation: store i2 1, ptr %2, align 1, !dbg !2322 fwiw, this specific repro bisects to c231d94 |
@Rexicon226 that idea was put on hold for now, and so has never been in a build of Zig. |
Ah, my bad. No idea then. |
Is there a way to print an undefined value without branching on it and invoking UB? It doesn't surprise me personally that LLVM seems to do weird things after branching on undefined... |
The thing is that only one field is undefined. Also, when casting to an int (where there's no branching) this also happens. |
Caused by #20095, using |
Zig Version
0.13.0-dev.211+6a65561e3
Steps to Reproduce and Observed Behavior
Run the following code:
which prints
Notice how when we print
foo
, we get.b = false
, but when we printfoo.b
we gettrue
.Expected Behavior
Both of them should print
b
as beingtrue
, but the first one doesn't.A few things I've noticed:
undefined
default value withfalse
, then everything works as expectedThe text was updated successfully, but these errors were encountered: