Understanding error: value with comptime-only type 'comptime_int' depends on runtime control flow

I mistakenly wrote this code with error

fn point(letter: u8) u32 {
   switch(letter) {
      'A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T' => 1,
      'D', 'G' => 2,
      'B', 'C', 'M', 'P' => 3,
      'F', 'H', 'V', 'W', 'Y' => 4,
      'K' => 5,
      'J', 'X' => 8,
      'Q', 'Z' => 10,
      else => unreachable
    }
}

The mistake was I did not have a return statement. But the compilation error I got was

scrabble_score.zig:11:4: error: value with comptime-only type 'comptime_int' depends on runtime control flow
scrabble_score.zig:11:11: note: runtime control flow here

My question is what does this error message mean? and how could I have understood it to be able to easily tell what the issue is because I ended up founding my error by staring long at the code, because the compile error message was not helpful…

The fixed code with the return statement.

fn point(letter: u8) u32 {
   return switch(letter) {
      'A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T' => 1,
      'D', 'G' => 2,
      'B', 'C', 'M', 'P' => 3,
      'F', 'H', 'V', 'W', 'Y' => 4,
      'K' => 5,
      'J', 'X' => 8,
      'Q', 'Z' => 10,
      else => unreachable
    };
}

When the return is in place, the return type u32 is derived for the integer constants 1,2,…,8,10.
When the return statement is missing, the type of the constants remains comptime_int.

error: value with comptime-only type 'comptime_int' depends on runtime control flow

In the error message, “value with comptime-only type ‘comptime_int’” means the integer constants, “depends on runtime control flow” means the switch statement cannot run in compile-time in order to derive the type of integer constants.

6 Likes