Skip to content

@fieldParentPtr gives wrong result for zero-size unions #19474

@vadim-za

Description

@vadim-za

Zig Version

zig-0.12.0-dev.3489+c808e546a

Steps to Reproduce and Observed Behavior

Compile and run the following code

const std = @import("std");

const A = struct{
    fn u(self: *@This()) *U {
        return @fieldParentPtr(U,"a",self);
    }
};
const B = struct{
    fn u(self: *@This()) *U {
        return @fieldParentPtr(U,"b",self);
    }
};
const U = union(enum) { a: A, b: B };

pub fn main() !void {
    var u: U = .{ .a = .{} };
    const pa = &u.a;
    const pu = pa.u();
    std.debug.print("{X} {X}\n",.{@intFromPtr(pu),@intFromPtr(&u)});
}

The code prints two address values, the first being 1 smaller than the second one. E.g.

3B477FFB76 3B477FFB77

Tested on x86_64 and Aarch64.

Expected Behavior

Expected both values to be equal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend-llvmThe LLVM backend outputs an LLVM IR Module.bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions