Selfhosted x86_64 backend hits unreachable code when passing sliced array of structs with field that has type "type" #30602

Open
opened 2025-12-27 15:32:21 +01:00 by Vidar · 0 comments

Zig Version

0.16.0-dev.1854+e55e6b552

Steps to Reproduce and Observed Behavior

repro.zig:

const Foo = struct {
    type: type,
};

fn bar(a: []const Foo) void {
    _ = a;
}

pub fn main() void {
    const foos = [1]Foo{.{
        .type = u8,
    }};
    bar(foos[1..]);
}
❯ ../../zig/build/stage3/bin/zig run repro.zig
Compiler crash context:
(no context)

thread 1052961 panic: reached unreachable code
/home/vidar/dev/zig/src/link/Dwarf.zig:4210:38: 0x3d928b0 in updateLazyValue (main.zig)
                        .arr_elem => unreachable,
                                     ^
/home/vidar/dev/zig/src/link/Dwarf.zig:2342:46: 0x36174f2 in updateLazy (main.zig)
            try wip_nav.dwarf.updateLazyValue(wip_nav.pt, src_loc, pending_val, &wip_nav.pending_lazy)
                                             ^
/home/vidar/dev/zig/src/link/Dwarf.zig:3049:27: 0x3c4c526 in finishWipNavWriterError (main.zig)
    try wip_nav.updateLazy(zcu.navSrcLoc(nav_index));
                          ^
/home/vidar/dev/zig/src/link/Dwarf.zig:3023:41: 0x3428370 in finishWipNav (main.zig)
    return dwarf.finishWipNavWriterError(pt, nav_index, wip_nav) catch |err| switch (err) {
                                        ^
/home/vidar/dev/zig/src/link/Dwarf.zig:3014:27: 0x471098e in finishWipNavFuncWriterError (main.zig)
    try dwarf.finishWipNav(pt, nav_index, wip_nav);
                          ^
/home/vidar/dev/zig/src/link/Dwarf.zig:2907:45: 0x4075c8f in finishWipNavFunc (main.zig)
    return dwarf.finishWipNavFuncWriterError(pt, nav_index, code_size, wip_nav) catch |err| switch (err) {
                                            ^
/home/vidar/dev/zig/src/link/Elf/ZigObject.zig:1581:64: 0x392fd70 in updateFunc (main.zig)
    if (debug_wip_nav) |*wip_nav| self.dwarf.?.finishWipNavFunc(pt, func.owner_nav, code.len, wip_nav) catch |err|
                                                               ^
/home/vidar/dev/zig/src/link/Elf.zig:1696:44: 0x32c8d11 in updateFunc (main.zig)
    return self.zigObjectPtr().?.updateFunc(self, pt, func_index, mir);
                                           ^
/home/vidar/dev/zig/src/link.zig:820:82: 0x2c7c0c8 in updateFunc (main.zig)
                return @as(*tag.Type(), @fieldParentPtr("base", base)).updateFunc(pt, func_index, mir);
                                                                                 ^
/home/vidar/dev/zig/src/link.zig:1541:30: 0x279b542 in doZcuTask (main.zig)
                lf.updateFunc(pt, func, &mir) catch |err| switch (err) {
                             ^
/home/vidar/dev/zig/src/link/Queue.zig:196:27: 0x2c21dfe in runLinkTasks (main.zig)
            link.doZcuTask(comp, tid, task);
                          ^
/home/vidar/dev/zig/lib/std/Io.zig:2126:13: 0x277a4e2 in start (std.zig)
            result_casted.* = @call(.auto, function, args_casted.*);
            ^
/home/vidar/dev/zig/lib/std/Io/Threaded.zig:1087:16: 0x1b6168e in start (std.zig)
        ac.func(ac.contextPointer(), ac.resultPointer());
               ^
/home/vidar/dev/zig/lib/std/Io/Threaded.zig:759:26: 0x1beeb45 in worker (std.zig)
            closure.start(closure, t);
                         ^
/home/vidar/dev/zig/lib/std/Thread.zig:558:13: 0x1baae20 in callFn__anon_37240 (std.zig)
            @call(.auto, f, args);
            ^
/home/vidar/dev/zig/lib/std/Thread.zig:829:30: 0x1b62209 in entryFn (std.zig)
                return callFn(f, args_ptr.*);
                             ^
???:?:?: 0x7f4adb49698a in ??? (/usr/lib/libc.so.6)
???:?:?: 0x7f4adb51a9cb in ??? (/usr/lib/libc.so.6)
zsh: IOT instruction (core dumped)  ../../zig/build/stage3/bin/zig run repro.zig

Compiling with LLVM/LLD (-fllvm) seems to work fine. Originally ran into this on 0.15.2 but seems to still be present on master. The context where this came up was passing struct fields (std.builtin.Type.StructField) to a function while excluding the first two entries. repro.zig compiles just fine if you get rid of the slicing of foos (also replacing the 1 with a 0 works). I was able to work around this for my use case by making the offending function (bar in this case) inline.

Expected Behavior

For the program run and compile like it does when using the LLVM backend

### Zig Version 0.16.0-dev.1854+e55e6b552 ### Steps to Reproduce and Observed Behavior repro.zig: ```zig const Foo = struct { type: type, }; fn bar(a: []const Foo) void { _ = a; } pub fn main() void { const foos = [1]Foo{.{ .type = u8, }}; bar(foos[1..]); } ``` ```shell ❯ ../../zig/build/stage3/bin/zig run repro.zig Compiler crash context: (no context) thread 1052961 panic: reached unreachable code /home/vidar/dev/zig/src/link/Dwarf.zig:4210:38: 0x3d928b0 in updateLazyValue (main.zig) .arr_elem => unreachable, ^ /home/vidar/dev/zig/src/link/Dwarf.zig:2342:46: 0x36174f2 in updateLazy (main.zig) try wip_nav.dwarf.updateLazyValue(wip_nav.pt, src_loc, pending_val, &wip_nav.pending_lazy) ^ /home/vidar/dev/zig/src/link/Dwarf.zig:3049:27: 0x3c4c526 in finishWipNavWriterError (main.zig) try wip_nav.updateLazy(zcu.navSrcLoc(nav_index)); ^ /home/vidar/dev/zig/src/link/Dwarf.zig:3023:41: 0x3428370 in finishWipNav (main.zig) return dwarf.finishWipNavWriterError(pt, nav_index, wip_nav) catch |err| switch (err) { ^ /home/vidar/dev/zig/src/link/Dwarf.zig:3014:27: 0x471098e in finishWipNavFuncWriterError (main.zig) try dwarf.finishWipNav(pt, nav_index, wip_nav); ^ /home/vidar/dev/zig/src/link/Dwarf.zig:2907:45: 0x4075c8f in finishWipNavFunc (main.zig) return dwarf.finishWipNavFuncWriterError(pt, nav_index, code_size, wip_nav) catch |err| switch (err) { ^ /home/vidar/dev/zig/src/link/Elf/ZigObject.zig:1581:64: 0x392fd70 in updateFunc (main.zig) if (debug_wip_nav) |*wip_nav| self.dwarf.?.finishWipNavFunc(pt, func.owner_nav, code.len, wip_nav) catch |err| ^ /home/vidar/dev/zig/src/link/Elf.zig:1696:44: 0x32c8d11 in updateFunc (main.zig) return self.zigObjectPtr().?.updateFunc(self, pt, func_index, mir); ^ /home/vidar/dev/zig/src/link.zig:820:82: 0x2c7c0c8 in updateFunc (main.zig) return @as(*tag.Type(), @fieldParentPtr("base", base)).updateFunc(pt, func_index, mir); ^ /home/vidar/dev/zig/src/link.zig:1541:30: 0x279b542 in doZcuTask (main.zig) lf.updateFunc(pt, func, &mir) catch |err| switch (err) { ^ /home/vidar/dev/zig/src/link/Queue.zig:196:27: 0x2c21dfe in runLinkTasks (main.zig) link.doZcuTask(comp, tid, task); ^ /home/vidar/dev/zig/lib/std/Io.zig:2126:13: 0x277a4e2 in start (std.zig) result_casted.* = @call(.auto, function, args_casted.*); ^ /home/vidar/dev/zig/lib/std/Io/Threaded.zig:1087:16: 0x1b6168e in start (std.zig) ac.func(ac.contextPointer(), ac.resultPointer()); ^ /home/vidar/dev/zig/lib/std/Io/Threaded.zig:759:26: 0x1beeb45 in worker (std.zig) closure.start(closure, t); ^ /home/vidar/dev/zig/lib/std/Thread.zig:558:13: 0x1baae20 in callFn__anon_37240 (std.zig) @call(.auto, f, args); ^ /home/vidar/dev/zig/lib/std/Thread.zig:829:30: 0x1b62209 in entryFn (std.zig) return callFn(f, args_ptr.*); ^ ???:?:?: 0x7f4adb49698a in ??? (/usr/lib/libc.so.6) ???:?:?: 0x7f4adb51a9cb in ??? (/usr/lib/libc.so.6) zsh: IOT instruction (core dumped) ../../zig/build/stage3/bin/zig run repro.zig ``` Compiling with LLVM/LLD (-fllvm) seems to work fine. Originally ran into this on 0.15.2 but seems to still be present on master. The context where this came up was passing struct fields (std.builtin.Type.StructField) to a function while excluding the first two entries. repro.zig compiles just fine if you get rid of the slicing of foos (also replacing the 1 with a 0 works). I was able to work around this for my use case by making the offending function (bar in this case) inline. ### Expected Behavior For the program run and compile like it does when using the LLVM backend
Vidar changed title from Selfhosted x86_64 backend hits unreachable code when passing sliced array of structs with field with type "type" to Selfhosted x86_64 backend hits unreachable code when passing sliced array of structs with field that has type "type" 2025-12-27 15:33:04 +01:00
Sign in to join this conversation.
No labels
abi/f32
abi/ilp32
abi/n32
abi/sf
abi/x32
accepted
arch/1750a
arch/21k
arch/6502
arch/a29k
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/avr32
arch/bfin
arch/bpf
arch/clipper
arch/colossus
arch/cr16
arch/cris
arch/csky
arch/dlx
arch/dsp16xx
arch/elxsi
arch/epiphany
arch/fr30
arch/frv
arch/h8300
arch/h8500
arch/hexagon
arch/hppa
arch/hppa64
arch/i370
arch/i860
arch/i960
arch/ia64
arch/ip2k
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/maxq
arch/mcore
arch/metag
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/mn10200
arch/mn10300
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/nios2
arch/ns32k
arch/nvptx
arch/or1k
arch/pdp10
arch/pdp11
arch/pj
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390
arch/s390x
arch/sh
arch/sh64
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/st200
arch/starcore
arch/tilegx
arch/tilepro
arch/tricore
arch/ts
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/we32k
arch/x86
arch/x86_16
arch/x86_64
arch/xcore
arch/xgate
arch/xstormy16
arch/xtensa
autodoc
backend/c
backend/llvm
backend/self-hosted
binutils
breaking
build system
debug info
docs
error message
frontend
fuzzing
incremental
lib/c
lib/compiler-rt
lib/cxx
lib/std
lib/tsan
lib/ubsan-rt
lib/unwind
linking
miscompilation
os/aix
os/android
os/bridgeos
os/contiki
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hermit
os/hurd
os/illumos
os/ios
os/kfreebsd
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/solaris
os/tvos
os/uefi
os/visionos
os/wali
os/wasi
os/watchos
os/windows
os/zos
proposal
release notes
testing
tier system
zig cc
zig fmt
bounty
bug
contributor-friendly
downstream
enhancement
infra
optimization
question
regression
upstream
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ziglang/zig#30602
No description provided.