Skip to content

A packed struct member of a struct isn't equal to itself. #14200

@DiskPoppy

Description

@DiskPoppy

Zig Version

0.11.0-dev.900+0fb53bd24

Steps to Reproduce and Observed Behavior

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

const DirectionalPad = packed struct {
    x: i2 = 0,
    y: i2 = 0,
};

const Input = struct {
    dir: DirectionalPad = DirectionalPad{},
};

fn testPositive(input: Input) bool {
    return input.dir.y == 1;
}

var t: bool = true;

pub fn createInput() Input {
    return Input{
        .dir = DirectionalPad{ .x = 0, .y = if (t) 1 else 0 },
    };
}

test "packed struct member equality" {
    const is_positive = testPositive(createInput());
    try testing.expectEqual(is_positive, true);
}

It is the most minimal reproduction of this bug I could make. The test fails, however when "printf debugging" input.dir.y is 1, but comparing it with 1 fails.

Expected Behavior

That input.dir.y is 1 and testing equal to 1 results in 'true'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend-llvmThe LLVM backend outputs an LLVM IR Module.bugObserved behavior contradicts documented or intended behaviormiscompilationThe compiler reports success but produces semantically incorrect code.upstreamAn issue with a third party project that Zig uses.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions