Skip to content

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

Description

@vadim-za

Zig Version

zig-0.12.0-dev.3489+c808e546a

Steps to Reproduce and Observed Behavior

Compile and run the following code

conststd=@import("std");
constA=struct{
fnu(self: *@This()) *U {
return@fieldParentPtr(U,"a",self);
}
};
constB=struct{
fnu(self: *@This()) *U {
return@fieldParentPtr(U,"b",self);
}
};
constU=union(enum) { a: A, b: B };
pubfnmain() !void {
varu: U= .{ .a= .{} };
constpa=&u.a;
constpu=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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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