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

'comptime_int' error when switching on caught Error #7179

Closed
tau-dev opened this issue Nov 20, 2020 · 4 comments
Closed

'comptime_int' error when switching on caught Error #7179

tau-dev opened this issue Nov 20, 2020 · 4 comments
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@tau-dev
Copy link
Contributor

tau-dev commented Nov 20, 2020

Trying to compile

fn test() error{A}!usize {
    return 1;
}

test "switch" {
    var x = test() catch |err| switch (err) {
        error.A => 2,
        else => 3,
    };
}

results in

error: values of type 'comptime_int' must be comptime known
    var x = test3() catch |err| switch (err) {
                                ^

I'm fairly new to Zig, so maybe I need to solve this differently and the error is intended behavior, but it feels like it shouldn't be.

@LemonBoy
Copy link
Contributor

LemonBoy commented Nov 20, 2020

This section on ZigLearn explains that literal values are of type comptime_int, you need to tell the compiler what type you want by:

  • Using var x: <type> = ...
  • Having at least one branch a cast such as @as(<type>, <number>)

Oh well, I didn't see the test3() has type usize. This looks like a compiler bug, the switch doesn't understand the LHS of the catch already has a well-defined type.

@alexnask alexnask added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Nov 22, 2020
@nektro
Copy link
Contributor

nektro commented Nov 23, 2020

This also seems like a bug because it should be showing the error that the else => is invalid due to the switch already being exhaustive first.

@Vexu Vexu added this to the 0.9.0 milestone Nov 27, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@renatoathaydes
Copy link

renatoathaydes commented Jun 30, 2022

I think I have the same kind of error... I am trying to compile this:

fn center(text: u8, out: *[5]u8) !void {
    const left_pad: usize = if (text < 10) 3 else if (text < 100) 2 else 1;
...

Looks straightforward but doesn't compile because the if expression tries to take the type comptime_int despite the explicit type on the resulting variable.

I also tried this:

fn center(text: u8, out: *[5]u8) !void {
    const left_pad = @as(usize, if (text < 10) 3 else if (text < 100) 2 else 1);
...

Which still fails with the same error.

./color8.zig:8:55: error: values of type 'comptime_int' must be comptime known
    const left_pad = @as(usize, if (text < 10) 3 else if (text < 100) 2 else 1);
                                                      ^
simple...The following command exited with error code 1:
zig-macos-x86_64-0.10.0-dev.2577+5816d3eae/zig build-exe projects/ansi-escapes.zig/examples/simple/color8.zig --cache-dir projects/ansi-escapes.zig/examples/simple/zig-cache --global-cache-dir /Users/renato/.cache/zig --name simple --pkg-begin ansi-escapes projects/ansi-escapes.zig/src/ansi-escapes.zig --pkg-end --enable-cache 
error: the following build command failed with exit code 1:

I have to unfortunately case every single value instead:

const left_pad = if (text < 10) @as(usize, 3) else if (text < 100) @as(usize, 2) else @as(usize, 1);

Which is really awkward.

Hope this kind of issue with comptime can be improved.

@Vexu
Copy link
Member

Vexu commented Jan 16, 2023

Stage2 issue in #11812

@Vexu Vexu closed this as completed Jan 16, 2023
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Jan 16, 2023
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

7 participants