Skip to content

Regression with "Unable to evaluate constant expression" in len of fixed size arrays #15280

@Jarred-Sumner

Description

@Jarred-Sumner

Zig Version

0.11.0-dev.2571+31738de28

Steps to Reproduce and Observed Behavior

Run zig build-exe repro.zig:

// repro.zig
const std = @import("std");
pub fn main() anyerror!void {
    var hasher = std.hash.Wyhash.init(0);
    var remain: []const u8 = "hello world!";
    var buf: [hasher.buf.len]u8 = undefined;
    std.debug.print("buf {s} {s}", .{ &buf, remain });
}
zig build-exe repro.zig
/Users/jarred/Desktop/repro.zig:5:21: error: unable to evaluate comptime expression
    var buf: [hasher.buf.len]u8 = undefined;
              ~~~~~~^~~~
referenced by:
    comptime_0: /Users/jarred/zig/0.11.0-dev.2571+31738de28/files/lib/std/start.zig:66:50
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

hasher.buf.len is comptime known because it is a fixed size array and therefore can be determined by the type definition of the Wyhash struct's buf field.

pub const Wyhash = struct {
    state: WyhashStateless,

    buf: [32]u8,
       // ^
    buf_len: usize,
// ...

Similarly, this fails to compile:

pub const Wyhash = struct {
    buf: [32]u8 = undefined,
};
const std = @import("std");
pub fn main() anyerror!void {
    var remain: []const u8 = "hello world!";
    var hasher = Wyhash{};
    var buf: [hasher.buf.len]u8 = undefined;
    std.debug.print("buf {s} {s}", .{ &buf, remain });
}

Expected Behavior

Compile successfully

This worked until at least 0.11.0-dev.2371+a31450375, which means it is a regression.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.regressionIt worked in a previous version of Zig, but stopped working.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions