Skip to content

zig comptime memoization broken with comptime slices #7948

@marler8997

Description

@marler8997

The following code will cause the compiler to go into an infinite function instantiation loop when compiling with zig build-exe bug.zig:

const std = @import("std");

fn comptimeFun(comptime s: []const u8) void {
    @compileLog("comptimeFun with ", s);
    // note: this next line causes the issue
    comptimeFun(s ++ "");
    // note: if we replace the line above with `comptimeFun(s)` then the infinite loop goes away
    // note: the issue also manifests with: comptimeFun(s ++ "something")[0..s.len]);
}

pub fn main() void {
    comptimeFun("foo");
}

The problem here is that when Zig checks whether comptimeFun("foo") has already been instantiated, it's not seeing that slices with the same contents are equivalent. So even though comptimeFun is always called with the same comptime string "foo", it doesn't see them as equivalent so it just keeps instantiating them forever.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavioruse caseDescribes a real use case that is difficult or impossible, but does not propose a solution.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions