Skip to content

@enumFromInt(@truncate(x)) should work #20375

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

Closed
wooster0 opened this issue Jun 21, 2024 · 3 comments
Closed

@enumFromInt(@truncate(x)) should work #20375

wooster0 opened this issue Jun 21, 2024 · 3 comments
Labels
frontend Tokenization, parsing, AstGen, Sema, and Liveness. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@wooster0
Copy link
Contributor

wooster0 commented Jun 21, 2024

Zig Version

0.13.0-dev.351+64ef45eb0

Steps to Reproduce and Observed Behavior

This could also be seen as a proposal.

const X = enum(u1) {
    a = 0,
    b = 1,
};

test {
    var y: u64 = 239823;
    const x: X = @enumFromInt(@truncate(y));
    _ = x;
}

When I want to convert an integer to an enum and @enumFromInt knows the enum because of the : X type annotation, it can know the result type of the @truncate by looking at the tag type I specified for the X enum.

Currently this happens:

x.zig:8:31: error: @truncate must have a known result type
    const x: X = @enumFromInt(@truncate(y));
                              ^~~~~~~~~~~~
x.zig:8:31: note: use @as to provide explicit result type

Similar to #18218

Expected Behavior

For the code above to compile.

@wooster0 wooster0 added the bug Observed behavior contradicts documented or intended behavior label Jun 21, 2024
@Vexu Vexu added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Jun 21, 2024
@Vexu Vexu added this to the 0.14.0 milestone Jun 21, 2024
@rohlem
Copy link
Contributor

rohlem commented Jun 21, 2024

Currently @enumFromInt is stated (only in the section about non-exhaustive enums) to include a hidden @intCast.
An optional extension proposal could be to remove this hidden assertion,
meaning @enumFromInt(x) would be safer and status-quo would instead be written as @enumFromInt(@intCast(x)).

@mlugg
Copy link
Member

mlugg commented Jun 22, 2024

Right now, this could only work if the change suggested by @rohlem above were implemented. Note that status quo behavior was implemented in (IIRC) 0.9.0; before then, @enumFromInt did not perform an implicit @intCast. I think it's definitely worth revisiting this with the new casting syntax in place.

@mlugg mlugg added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. and removed bug Observed behavior contradicts documented or intended behavior labels Jun 22, 2024
@mlugg
Copy link
Member

mlugg commented Feb 1, 2025

This proposal is now equivalent to #22712; closing as duplicate for that reason.

@mlugg mlugg closed this as completed Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend Tokenization, parsing, AstGen, Sema, and Liveness. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

4 participants