Skip to content

cannot bitcast from enum to packed struct #18462

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

Open
nektro opened this issue Jan 6, 2024 · 5 comments
Open

cannot bitcast from enum to packed struct #18462

nektro opened this issue Jan 6, 2024 · 5 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@nektro
Copy link
Contributor

nektro commented Jan 6, 2024

Zig Version

0.12.0-dev.1828+225fe6ddb

Steps to Reproduce and Observed Behavior

test {
    const q: E = @enumFromInt(242892);
    const r: P = @bitCast(q);
    _ = r;
}

const E = enum(u32) {
    _,
};

const P = packed struct(u32) {
    a: u16,
    b: u16,
};
❯ zig test test.zig 
test.zig:23:27: error: cannot @bitCast from 'test.E'
    const r: P = @bitCast(q);
                          ^

Expected Behavior

success

@nektro nektro added the bug Observed behavior contradicts documented or intended behavior label Jan 6, 2024
@nektro
Copy link
Contributor Author

nektro commented Jan 6, 2024

errata: the other direction doesn't work either but its far easier to workaround with @bitCast(@intFromEnum(e)) since the intFromEnum call doesn't need a result type

edit: I mention the result type because the workaround for this issue also requires going through an int temporary

@Vexu
Copy link
Member

Vexu commented Jan 6, 2024

I would expect this to be an intentional extension of #3647

@nektro
Copy link
Contributor Author

nektro commented Jan 6, 2024

does that mean @enumFromInt should/does accept packed structs directly? I'll have to try that

: ( alas no, I get error: expected integer type, found 'P'

@Vexu
Copy link
Member

Vexu commented Jan 7, 2024

I don't think so, packed structs are not ints despite being implemented as such.

@nektro
Copy link
Contributor Author

nektro commented Feb 10, 2024

I think if #18882 (comment) is implemented then this starts to make a lot of sense

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
Projects
None yet
Development

No branches or pull requests

2 participants