Skip to content

Improve "value with comptime-only type 'comptime_int' depends on runtime control flow" #23401

@uyha

Description

@uyha

Zig Version

0.14.0

Steps to Reproduce and Observed Output

With the following program

pub fn main() !void {
    var engine = std.Random.DefaultPrng.init(0);
    const eh: u8 = switch (engine.random().int(u8)) {
        1 => 2,
        else => 3,
    } + 1;
    _ = eh;
}

const std = @import("std");

The current error message is printed out

src/plus.zig:3:20: error: value with comptime-only type 'comptime_int' depends on runtime control flow
    const eh: u8 = switch (engine.random().int(u8)) {
                   ^~~~~~
src/plus.zig:3:47: note: runtime control flow here
    const eh: u8 = switch (engine.random().int(u8)) {
                           ~~~~~~~~~~~~~~~~~~~^~~~

Expected Output

I think it should suggest a fix to cast the switch expression to a runtime type, something like,

try casting the expression to a runtime int with `@as`

Metadata

Metadata

Assignees

No one assigned

    Labels

    error messageThis issue points out an error message that is unhelpful and should be improved.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions