Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Type() cannot build tagged unions #8114

Open
fengb opened this issue Mar 1, 2021 · 1 comment
Open

@Type() cannot build tagged unions #8114

fengb opened this issue Mar 1, 2021 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@fengb
Copy link
Contributor

fengb commented Mar 1, 2021

My understanding is that tagged union are created via .layout = .Auto, .tag_type = EnumDefinition, but this currently generates compiler errors indicating this union isn't tagged:

const std = @import("std");

const Broken = @Type(.{
    .Union = .{
        .layout = .Auto,
        .tag_type = enum { temp },
        .fields = &[_]std.builtin.TypeInfo.UnionField{
            .{ .name = "foo", .field_type = void, .alignment = 0 },
        },
        .decls = &.{},
    },
});

comptime {
    // error: switch on union which has no attached enum
    switch (Broken{ .foo = {} }) {
        .foo => {},
    }
}

@g-w1 mentioned that the stage1 compiler is crossreferencing against the wrong condition: #8069

Interestingly enough, this seems to work if I hijack an existing tagged union:

const Ref = union(enum) {temp};
const Working = blk: {
    var info = @typeInfo(Ref);
    info.Union.tag_type = enum { foo };
    info.Union.fields = &[_]std.builtin.TypeInfo.UnionField{
        .{ .name = "foo", .field_type = void, .alignment = 0 },
    };
    break :blk @Type(info);
};
@Vexu Vexu added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Mar 1, 2021
@Vexu Vexu added this to the 0.9.0 milestone Mar 1, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@sh4r1k7
Copy link

sh4r1k7 commented Mar 9, 2022

Just ran into this myself and I found that you don't need to hijack an existing type, merely store the type info into a variable then pass it to @Type(t).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

4 participants