Error: unable to resolve comptime value when passing slice to a function

Hi, can someone please help me to deal with this very strange error. I am trying to read data from the serial port and do something with it.

Here is the code snippet:

pub fn hasHello(buffer: []u8) bool {
    return buffer.len > 0; // Stub
}

fn conn_waiting(conn: *BoardConn) void {
    var buf: [64]u8 = std.mem.zeroes([64]u8);
    // or
    // var buf: [64]u8 = [1]u8{0} ** 64;
    conn.lock.lock();
    defer conn.lock.unlock();

    while (conn.state == ConnState.Waiting and conn.running) {
        conn.wait.timedWait(&conn.lock, 100 * std.time.ns_per_ms) catch {};

        if (conn.port == null) {
            std.log.warn("Port is null, closing...", .{});
            conn.state = ConnState.Disconnected;
            break;
        }

        const len = conn.port.?.read(&buf) catch |err| {
            std.log.err("Failed to read from port: {s}", .{@errorName(err)});
            conn.err = "Failed to read from port";
            conn.state = ConnState.Disconnected;
            break;
        };

        if (len > 0 || hasHello(&buf)) {
//board.zig:122:33: error: unable to resolve comptime value
//        if (len > 0 || hasHello(&buf)) {
//                                ^~~~
//board.zig:122:32: note: types must be comptime-known
//        if (len > 0 || hasHello(&buf)) {
            continue; // Do something useful
        }
    }
}

I am completely lost on why Zig is trying to do anything comptime here. Also I checked the signature of the File.read() function and it looks very similar:

pub fn read(self: File, buffer: []u8) ReadError!usize {

But call to it compiles fine without any comptime errors.

I’ve tried using [*]u8, [*]const u8, []u8, [] const u8 but I get the same error with all of them.
I’m using Zig 0.14.0.

In Zig “Logical Or” operator is or. || is an “Error Set Merge” operator, see Table of Operators.

Also, it’s better to use @splat for initializing arrays with the same value, but often you can just use undefined if the values will be filled in later.

2 Likes

You want or, not ||! The latter is the “error set merge” operator, which is a binary operator which acts on types and so forces the LHS and RHS to be comptime-evaluated.

Ideally, there’d be an error note here pointing you in the right direction; I’ll look into that.

3 Likes

Damn… I have seen a special error about && and “and” but somehow completely forgot about it. Thanks everyone.

Wow that was a misleading compiler error :slight_smile: