ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.32k stars 2.51k forks source link

Compiler panic: Assertion failed at /home/vsts/work/1/s/src/stage1/analyze.cpp:621 in get_pointer_to_type_extra2. #9789

Open cincodenada opened 3 years ago

cincodenada commented 3 years ago

Hello, I was digging into generics today and managed to get the compiler to crash and tell me that it was a bug in the compiler. I don't have nearly enough context to understand what's going on here, but I did manage to get a pretty minimal reproduction by hacking things away. Here's the code that I get the crash with:

fn MakeType() type {
    return struct {
        // Make the bug not happen by commenting out this line...
        state: anytype = undefined,
        // ...or, alternately, this line and its initialization below
        callback: *const fn (*@This()) void,
    };
}

fn cb(param: *MakeType()) void {
    _ = param;
}

test {
    const Thing = MakeType();
    const parser = Thing{
        .callback = &cb,
    };
    _ = parser;
}

Running that gets me:

› zig version                                         
0.9.0-dev.927+eb5e4ac49
› zig test src/test.zig                               
Assertion failed at /home/vsts/work/1/s/src/stage1/analyze.cpp:621 in get_pointer_to_type_extra2. This is a bug in the Zig compiler.thread 4164719 panic: 
Unable to dump stack trace: debug info stripped
zsh: abort (core dumped)  zig test src/test.zig

As noted above, removing either of the struct properties (and adjusting accordingly) results in a program that at least compiles, so this as far as I got it stripped down. Replacing anytype with a concrete type (e.g. u8) also gets it compiling, but replacing it with e.g. @TypeOf(u8) does not.

I don't know if this is even valid Zig code, I'm coming from a background of modern C++ (among others) and have only been wading into Zig for a week or so. But in any case, it seems like it shouldn't be crashing the compiler. Happy to do further tests or what have you as useful!

RetroDev256 commented 1 month ago

The error goes away if you remove the anytype field. I don't think we can put a regression test in master because we can't have anytype fields in structs anymore.