zig icon indicating copy to clipboard operation
zig copied to clipboard

ErrorUnion typeInfo debug print causes GenericPoison

Open NicoElbers opened this issue 1 year ago • 2 comments

Zig Version

0.13.0-dev.267+793f820b3

Steps to Reproduce and Observed Behavior

Paste the following source code in bug.zig:

const std = @import("std");

fn testFn(comptime T: type) !T {}

pub fn main() !void {
    const type_info = @typeInfo(@TypeOf(testFn));
    const return_type_info = @typeInfo(type_info.Fn.return_type.?);
    std.debug.print("{}", .{return_type_info.ErrorUnion});
}

Run zig build-exe bug.zig

This returns:

❯ zig build-exe bug.zig     
thread 28166 panic: zig compiler bug: GenericPoison
Unable to dump stack trace: debug info stripped
[1]    28166 IOT instruction (core dumped)  zig build-exe bug.zig

Expected Behavior

A compiler error about something like type_info.Fn.return_type.? being null.

NicoElbers avatar May 27 '24 17:05 NicoElbers

shouldnt this be a compile error from the type_info.Fn.return_type.?? the return type is generic so iiuc it should be null

xdBronch avatar May 27 '24 17:05 xdBronch

shouldnt this be a compile error from the type_info.Fn.return_type.?? the return type is generic so iiuc it should be null

Could very well be, regardless the compiler shouldn't crash. I'll change the original issue.

EDIT: I just looked in some other code of mine where @compileLog(..) says the function is generic but type_info.Fn.return_type.? works fine. So I'm not sure if that's right. But ultimately that doesn't change the issue

EDIT2: I'm not completely sure if this is the same bug or a similar bug, since the build output is slightly different.

I created this small program:

const std = @import("std");

fn testFn(comptime T: type) !T {}

pub fn main() !void {
    const type_info = @typeInfo(@TypeOf(testFn));
    comptime if (type_info.Fn.return_type == null) @compileError("return_type was null");
    comptime if (type_info.Fn.is_generic == false) @compileError("testFn was not generic");

    std.debug.print("yay", .{});

    // const return_type_info = @typeInfo(@typeInfo(@TypeOf(testFn)).Fn.return_type.?);
    // @compileLog(return_type_info);
}

When the bottom 2 lines are commented out this compiles and prints "yay", ensuring that the function was generic and the return type not null. Then when the bottom 2 lines are uncommented (optionally the first few lines removed), running zig build-exe bug.zig returns:

❯ zig build-exe bug.zig
[1]    35562 segmentation fault (core dumped)  zig build-exe bug.zig

NicoElbers avatar May 27 '24 17:05 NicoElbers

Caused by this check which doesn't check inferred error set payload being generic poison: https://github.com/ziglang/zig/blob/2008d0f7c9cfb0ab5ea908c216751dbc335d1735/src/Sema.zig#L17855

Vexu avatar May 29 '24 20:05 Vexu