Skip to content

Zig segfaults when compiling: runtime struct input to type returning function #24592

@dimchee

Description

@dimchee

Zig Version

0.14.1

Steps to Reproduce and Observed Behavior

The following minimal snippet reproduces the issue:

fn Fn(x: struct { []const u8, type }) type {
    _ = x;
    return struct {};
}

pub fn main() void {
    var runtime_val: []const u8 = "true";
    runtime_val = "false";
    const T = Fn(.{ runtime_val, struct {} });
    _ = T;
}
$ zig run problem.zig
Segmentation fault (core dumped)

Expected Behavior

Code should not compile, and return a helpful error message:

$ zig run problem.zig 
problem.zig:9:18: error: unable to resolve comptime value
    const T = Fn(.{ runtime_val, struct {} });
                 ^~~~~~~~~~~~~~~~~~~~~~~
problem.zig:9:17: note: call to function with comptime-only return type 'type' is evaluated at comptime
    const T = Fn(.{ runtime_val, struct {} });
              ~~^~~~~~~~~~~~~~~~~~~~~~
problem.zig:1:22: note: return type declared here
fn Fn(x: struct { []const u8, type }) type {
                                                     ^~~~
problem.zig:9:17: note: types are not available at runtime
    const T = Fn(.{ runtime_val, struct {} });
              ~~^~~~~~~~~~~~~~~~~~~~~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions