Skip to content

Invalid cast error when printing extern unions #23414

@nikneym

Description

@nikneym

Zig Version

0.14.0-dev.3357+c44f4501e

Steps to Reproduce and Observed Behavior

It seems printing a union with at least 2 void fields results in unexpected errors. To reproduce:

const std = @import("std");

const Union = extern struct {
    data: extern union {
        first: void,
        second: void,
    },
};

pub fn main() !void {
    const a = Union{
        .data = .{
            .first = {},
        },
    };

    std.debug.print("{}\n", .{a});
}

This creates regular unexpected error output. However, you can get a fancy LLVM one too if you also provide an enum with explicit tag type:

const std = @import("std");

const EnumAndUnion = extern struct {
    enumeration: enum(u8) { first, second },
    data: extern union {
        first: void,
        second: void,
    },
};

pub fn main() !void {
    const a = EnumAndUnion{
        .enumeration = .first,
        .data = .{
            .first = {},
        },
    };

    std.debug.print("{}\n", .{a});
}
run
└─ run repro
   └─ install
      └─ install repro
         └─ zig build-exe repro Debug native failure
error: error: Invalid cast (Producer: 'zig 0.14.0' Reader: 'LLVM 19.1.7')

Expected Behavior

It should print out correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions