Skip to content

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

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

Open
Jarred-Sumner opened this issue Apr 14, 2023 · 5 comments
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness. regression It worked in a previous version of Zig, but stopped working.
Milestone

Comments

@Jarred-Sumner
Copy link
Contributor

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.

@Jarred-Sumner Jarred-Sumner added the bug Observed behavior contradicts documented or intended behavior label Apr 14, 2023
@Vexu Vexu added this to the 0.11.0 milestone Apr 14, 2023
@Vexu
Copy link
Member

Vexu commented Apr 14, 2023

Probably caused by #14819

@ominitay
Copy link
Contributor

Possibly related to #12240

@mlugg
Copy link
Member

mlugg commented Apr 14, 2023

Yes caused by that and yes related to that - it happens when you have another layer of field access before len. Here's an example of code that didn't work (at top-level!) prior to the change:

const S = struct {
    arr: [5]u8 = undefined,
};
var x: S = .{};

comptime {
    _ = x.arr.len;
}

Because function-level comptime now works properly, the same issue manifests itself there.

mlugg added a commit to mlugg/zig that referenced this issue Apr 14, 2023
Related to 34e4b07. This makes `struct.arr.len` succeed at comptime.

Resolves: ziglang#15280
mlugg added a commit to mlugg/zig that referenced this issue Apr 14, 2023
Related to 34e4b07. This makes `struct.arr.len` succeed at comptime.

Resolves: ziglang#15280
mlugg added a commit to mlugg/zig that referenced this issue Apr 14, 2023
Related to 34e4b07. This makes `struct.arr.len` succeed at comptime.

Resolves: ziglang#15280
@andrewrk andrewrk added the regression It worked in a previous version of Zig, but stopped working. label Jul 22, 2023
@andrewrk andrewrk modified the milestones: 0.11.0, 0.11.1 Jul 24, 2023
@gavo579
Copy link

gavo579 commented Aug 25, 2023

Workaround:

comptime {
    // broken
    _ = s.arr.len;
    // works
    _ = @typeInfo(std.meta.FieldType(@TypeOf(s), .arr)).Array.len;
}

Would love to see this fixed, rooting for #15287.

@andrewrk andrewrk modified the milestones: 0.11.1, 0.12.0 Jan 29, 2024
Vexu added a commit to Vexu/zig that referenced this issue Mar 18, 2024
Vexu added a commit to Vexu/zig that referenced this issue Mar 19, 2024
Vexu added a commit to Vexu/zig that referenced this issue Mar 22, 2024
@andrewrk andrewrk added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Apr 18, 2024
@andrewrk andrewrk modified the milestones: 0.12.0, 0.13.0 Apr 18, 2024
@andrewrk andrewrk modified the milestones: 0.14.0, 0.14.1 Mar 1, 2025
@gavo579
Copy link

gavo579 commented Apr 2, 2025

This can be closed.
Both repros in this issue and my personal use case are fixed in Zig 0.14.0.

@andrewrk andrewrk modified the milestones: 0.14.1, 0.15.0 Apr 15, 2025
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 frontend Tokenization, parsing, AstGen, Sema, and Liveness. regression It worked in a previous version of Zig, but stopped working.
Projects
None yet
6 participants