zig icon indicating copy to clipboard operation
zig copied to clipboard

Zig compiler bug: attempted to destroy declaration with an attached error

Open ehaas opened this issue 3 years ago • 2 comments
trafficstars

Zig Version

0.10.0-dev.4707+209a0d2a8

Steps to Reproduce and Observed Behavior

pub fn Bar(comptime T: type) type {
    _ = T;
    @compileError("error");
}

pub fn foo(x: anytype) Bar(x) {}

test {
    _ = foo(1);
}

zig test test.zig

Output:

test.zig:6:28: error: expected type 'type', found 'comptime_int'
pub fn foo(x: anytype) Bar(x) {}
                           ^
referenced by:
    foo__anon_957: test.zig:6:24
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

thread 2633781 panic: Zig compiler bug: attempted to destroy declaration with an attached error
/home/ehaas/source/zig/src/Module.zig:3493:13: 0x966c7a3 in destroyDecl (zig)
            @panic("Zig compiler bug: attempted to destroy declaration with an attached error");
            ^
/home/ehaas/source/zig/src/Sema.zig:6936:33: 0xa3b8c5f in instantiateGenericCall (zig)
        errdefer mod.destroyDecl(new_decl_index);
                                ^
/home/ehaas/source/zig/src/Sema.zig:6238:40: 0xa14b270 in analyzeCall (zig)
        if (sema.instantiateGenericCall(
                                       ^
/home/ehaas/source/zig/src/Sema.zig:6017:42: 0x9e118ff in zirCall (zig)
            break :b try sema.analyzeCall(block, func, func_src, call_src, modifier, ensure_result_used, resolved_args, bound_arg_src);
                                         ^
/home/ehaas/source/zig/src/Sema.zig:820:62: 0x9b722ca in analyzeBodyInner (zig)
            .call                         => try sema.zirCall(block, inst),
                                                             ^
/home/ehaas/source/zig/src/Sema.zig:5150:34: 0xa186cfc in resolveBlockBody (zig)
        if (sema.analyzeBodyInner(child_block, body)) |_| {
                                 ^
/home/ehaas/source/zig/src/Sema.zig:5133:33: 0x9ed10d7 in zirBlock (zig)
    return sema.resolveBlockBody(parent_block, src, &child_block, body, inst, &label.merges);
                                ^
/home/ehaas/source/zig/src/Sema.zig:1353:69: 0x9b8175e in analyzeBodyInner (zig)
                if (!block.is_comptime) break :blk try sema.zirBlock(block, inst);
                                                                    ^
/home/ehaas/source/zig/src/Sema.zig:704:30: 0x9b70838 in analyzeBody (zig)
    _ = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                             ^
/home/ehaas/source/zig/src/Module.zig:5652:21: 0x99689af in analyzeFnBody (zig)
    sema.analyzeBody(&inner_block, fn_info.body) catch |err| switch (err) {
                    ^
/home/ehaas/source/zig/src/Module.zig:4344:40: 0x97d5882 in ensureFuncBodyAnalyzed (zig)
            var air = mod.analyzeFnBody(func, sema_arena) catch |err| switch (err) {
                                       ^
/home/ehaas/source/zig/src/Compilation.zig:3064:42: 0x97d36fd in processOneJob (zig)
            module.ensureFuncBodyAnalyzed(func) catch |err| switch (err) {
                                         ^
/home/ehaas/source/zig/src/Compilation.zig:3002:30: 0x96ad80a in performAllTheWork (zig)
            try processOneJob(comp, work_item);
                             ^
/home/ehaas/source/zig/src/Compilation.zig:2330:31: 0x96a978c in update (zig)
    try comp.performAllTheWork(main_progress_node);
                              ^
/home/ehaas/source/zig/src/main.zig:3326:20: 0x96d3016 in updateModule (zig)
    try comp.update();
                   ^
/home/ehaas/source/zig/src/main.zig:3008:17: 0x95ea091 in buildOutputType (zig)
    updateModule(gpa, comp, hook) catch |err| switch (err) {
                ^
/home/ehaas/source/zig/src/main.zig:236:31: 0x95c8c35 in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .zig_test);
                              ^
/home/ehaas/source/zig/src/stage1.zig:56:24: 0x973b167 in main (zig)
        stage2.mainArgs(gpa, arena, args) catch unreachable;
                       ^
???:?:?: 0x7f59f7660082 in ??? (???)
Aborted

Expected Behavior

error: expected type 'type', found 'comptime_int' with no crash

ehaas avatar Oct 30 '22 06:10 ehaas

This is what https://github.com/ziglang/zig/pull/13017 is attempting to fix. I think this will have a major impact on the quality of the release so I'm going to bump the priority up and focus on it tomorrow.

andrewrk avatar Oct 30 '22 06:10 andrewrk

Another example:

test "bug" {
    const array: [8:0]u8 = undefined;
    _ = @import("std").mem.span(array);
}
$ zig test bug.zig
/home/dom/.compiled/zig/zig-linux-x86_64-0.10.0/lib/std/mem.zig:644:17: error: invalid type given to std.mem.Span
        else => @compileError("invalid type given to std.mem.Span"),
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/dom/.compiled/zig/zig-linux-x86_64-0.10.0/lib/std/mem.zig:678:31: note: called from here
pub fn span(ptr: anytype) Span(@TypeOf(ptr)) {
                          ~~~~^~~~~~~~~~~~~~
thread 138783 panic: Zig compiler bug: attempted to destroy declaration with an attached error
Unable to dump stack trace: debug info stripped
zsh: IOT instruction (core dumped)  zig test bug.zig

$ zig version
0.10.0

DomWilliams0 avatar Nov 05 '22 19:11 DomWilliams0

And another one using io.MultiWriter:

/Users/***/zig/zig-macos-x86_64-0.11.0-dev.116+41b7e40d7/lib/std/io/multi_writer.zig:10:38: error: type '*xxh.xxh32' has no members
        ErrSet = ErrSet || StreamType.Error;
                           ~~~~~~~~~~^~~~~~
/Users/***/zig/zig-macos-x86_64-0.11.0-dev.116+41b7e40d7/lib/std/io/multi_writer.zig:39:49: note: called from here
pub fn multiWriter(streams: anytype) MultiWriter(@TypeOf(streams)) {
                                     ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
thread 2264875 panic: Zig compiler bug: attempted to destroy declaration with an attached error

pierrec avatar Nov 13 '22 10:11 pierrec

Fixed by #13017

Vexu avatar Jan 05 '23 12:01 Vexu