Skip to content
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

Seemingly random values from @bitCast(u64, packed_struct) #3674

Closed
SamTebbs33 opened this issue Nov 12, 2019 · 2 comments
Closed

Seemingly random values from @bitCast(u64, packed_struct) #3674

SamTebbs33 opened this issue Nov 12, 2019 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code. stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@SamTebbs33
Copy link
Contributor

SamTebbs33 commented Nov 12, 2019

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

const S1 = packed struct {
    a: u8,
};

const S2 = packed struct {
    a: u4,
};

const S = packed struct {
    int: u52,
    s1: S1,
    s2: S2,
};

fn makeStruct(s1: S1, s2: S2) S {
    return S{
        .int = 0,
        .s1 = S1{
            .a = s1.a,
        },
        .s2 = S2{ .a = s2.a },
    };
}

test "reproduce" {
    const actual = makeStruct(S1{ .a = 0 }, S2{ .a = 0 });
    // Arbitrary value, not important for this test case
    const expected: u64 = 0;
    expectEqual(expected, @bitCast(u64, actual));
}

Each time this is run with zig test reproduce.zig it has a different value for actual. It doesn't happen if actual is created in-place rather than with a function, so could that mean it has something to do with result location semantics? It doesn't happen if S1.a and S2.a are assigned to 0 directly rather than from the members of the structs given as arguments. I couldn't reproduce this before 5b27943

@andrewrk andrewrk added this to the 0.6.0 milestone Nov 14, 2019
@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code. stage1 The process of building from source via WebAssembly and the C backend. labels Nov 14, 2019
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Mar 12, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 17, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@nektro
Copy link
Contributor

nektro commented Oct 12, 2021

this works now

@Vexu
Copy link
Member

Vexu commented Dec 28, 2022

test/behavior/packed-struct.zig contains tests similar enough to this.

@Vexu Vexu closed this as completed Dec 28, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Dec 28, 2022
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 miscompilation The compiler reports success but produces semantically incorrect code. stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

4 participants