zig icon indicating copy to clipboard operation
zig copied to clipboard

Infinite recursion in the compiler

Open matrizzo opened this issue 2 years ago • 0 comments

Zig Version

0.11.0-dev.1929+4ea2f441d

Steps to Reproduce and Observed Behavior

The following code causes infinite recursion in the Zig compiler which causes it to run out of stack space and crash. I'm on mac OS but it also seems to work on Godbolt (https://godbolt.org/z/Es3397cG8).

const std = @import("std");

const A = struct {
    b: B,
};

const B = struct {
    a: A,
};

pub fn main() !void {
    _ = std.heap.c_allocator.create(A);
}

Repro by creating a new project with zig init-exe, pasting the code in main.zig and running zig build.

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x16ddfffc0)
    frame #0: 0x00000001001f8804 zig`main + 2029932
zig`Sema.resolveTypeFields:
->  0x1001f8804 <+8>:  stp    x20, x19, [sp, #0xb0]
    0x1001f8808 <+12>: mov    x20, x2
    0x1001f880c <+16>: mov    x19, x0
    0x1001f8810 <+20>: stp    x29, x30, [sp, #0xc0]
Target 0: (zig) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x16ddfffc0)
  * frame #0: 0x00000001001f8804 zig`main + 2029932
    frame #1: 0x00000001001fc5e0 zig`type.Type.hasRuntimeBitsAdvanced + 1128
    frame #2: 0x00000001001fc674 zig`type.Type.hasRuntimeBitsAdvanced + 1276
    frame #3: 0x00000001001fc674 zig`type.Type.hasRuntimeBitsAdvanced + 1276
    frame #4: 0x00000001001fc674 zig`type.Type.hasRuntimeBitsAdvanced + 1276
    frame #5: 0x00000001001fc674 zig`type.Type.hasRuntimeBitsAdvanced + 1276
    frame #6: 0x00000001001fc674 zig`type.Type.hasRuntimeBitsAdvanced + 1276
    frame #7: 0x00000001001fc674 zig`type.Type.hasRuntimeBitsAdvanced + 1276
    frame #8: 0x00000001001fc674 zig`type.Type.hasRuntimeBitsAdvanced + 1276
    frame #9: 0x00000001001fc674 zig`type.Type.hasRuntimeBitsAdvanced + 1276
    [...]
    frame #77526: 0x00000001001fc674 zig`type.Type.hasRuntimeBitsAdvanced + 1276
    frame #77527: 0x00000001001fc674 zig`type.Type.hasRuntimeBitsAdvanced + 1276
    frame #77528: 0x00000001001fc674 zig`type.Type.hasRuntimeBitsAdvanced + 1276
    frame #77529: 0x00000001004e0f30 zig`value.Value.orderAgainstZeroAdvanced + 732

Expected Behavior

The compiler doesn't crash and/or produces an error.

matrizzo avatar Mar 13 '23 18:03 matrizzo