Skip to content

Union with only []type fields causes compile error #25944

@0x6a62

Description

@0x6a62

Zig Version

0.15.2
0.16.0-dev.1316+181b25ce4

Steps to Reproduce and Observed Behavior

Description

When a union has all of it's fields of type []const type, I get the compile error: [@enumFromInt(26829)]: 8 != 1

In case it matters, this is on 64bit Linux.

Examples

Causes compile error:

const std = @import("std");

// compile error
const Foo = union(enum) {
    opt1: []const type,
    opt2: []const type,
};

pub fn main() !void {
    const foo1: Foo = Foo{ .opt1 = &.{} };
    std.debug.print("foo1: {any}\n", .{foo1});

    const foo2: Foo = Foo{ .opt2 = &.{} };
    std.debug.print("foo2: {any}\n", .{foo2});
}

As an investigation, I found the below definitions work, so it appears to be related to multiple fields of only type "[]type". I also tried this with a tagged union, and I get the same results.

// works
const Foo = union(enum) {
    opt1: []const i32,
    opt2: []const type,
};

// works
const Foo = union(enum) {
    opt1: []const type,
};

// works (work-around)
const Foo = union(enum(usize)) {
    opt1: []const type,
    opt2: []const type,
};

Expected Behavior

The union should be able to be compiled and used without error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend-self-hostedbugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions