Skip to content

broken packed struct initialization #21235

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

Closed
Traxar opened this issue Aug 28, 2024 · 3 comments
Closed

broken packed struct initialization #21235

Traxar opened this issue Aug 28, 2024 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@Traxar
Copy link

Traxar commented Aug 28, 2024

Zig Version

0.13.0

Steps to Reproduce and Observed Behavior

const std = @import("std");
const expect = std.testing.expect;

const SliceAtHome = packed struct {
    const Slice = @This();
    ptr: [*]bool = undefined,
    len: usize = 0,

    pub inline fn init(n: usize) !Slice {
        var slice = Slice{ .len = n };
        slice = slice;
        return slice;
    }
};

test "MultiSlice" {
    const ms = try SliceAtHome.init(10);
    try expect(ms.len == 0); // passes
    try expect(ms.len == 10); // fails
}

Expected Behavior

test passes

@Traxar Traxar added the bug Observed behavior contradicts documented or intended behavior label Aug 28, 2024
@ianprime0509
Copy link
Contributor

ianprime0509 commented Aug 28, 2024

Here's a related example:

const std = @import("std");

const S = packed struct {
    a: usize = undefined,
    b: usize,
};

pub fn main() void {
    const s_const: S = .{ .b = 123 };
    std.debug.print("{}\n", .{s_const.b});

    var s_var: S = .{ .b = 123 };
    _ = &s_var;
    std.debug.print("{}\n", .{s_var.b});
}

As of 0.14.0-dev.1349+6a21875dd, this prints

123
17007316

(at least for me; the second value will not necessarily be consistent since it appears to be uninitialized memory: https://godbolt.org/z/PM85achM7)

This example works as expected (prints 123 twice) if any of the following changes are made:

  1. Make S a regular (non-packed) struct
  2. Reorder the field b before a
  3. Choose any other default value for a besides undefined

May be related to #20095 and #20938 (both of which involve undefined being handled incorrectly in packed structs, but with different outcomes)

@Traxar
Copy link
Author

Traxar commented Aug 29, 2024

  1. The example also works with the following replacement:
    // var s_var: S = .{ .b = 123 };
    var s_var: S = undefined;
    s_var.b = 123;

This does not change anything about the behaviour elsewhere, compared to the other workarounds

@Vexu
Copy link
Member

Vexu commented Sep 1, 2024

Duplicate of #20938

@Vexu Vexu marked this as a duplicate of #20938 Sep 1, 2024
@Vexu Vexu closed this as not planned Won't fix, can't repro, duplicate, stale Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

3 participants