Skip to content

compiler crash with unresolved type chased through a comptime-known pointer to global #12404

@andrewrk

Description

@andrewrk

I'm having trouble coming up with a reduction, but this triggers when building zig-wayland from source with stage2:

[nix-shell:~/Downloads/zig-wayland]$ ~/Downloads/zig/build-release/stage2/bin/zig build
thread 2285961 panic: reached unreachable code
/home/andy/Downloads/zig/lib/std/debug.zig:281:14: 0x2ee6838 in std.debug.assert (zig)
    if (!ok) unreachable; // assertion failure
             ^
/home/andy/Downloads/zig/src/Module.zig:1312:15: 0x3394364 in Module.Union.getLayout (zig)
        assert(u.haveLayout());
              ^
/home/andy/Downloads/zig/src/type.zig:4263:43: 0x30c52ad in type.Type.unionGetLayout (zig)
                return union_obj.getLayout(target, true);
                                          ^
/home/andy/Downloads/zig/src/codegen/llvm.zig:2804:48: 0x332a206 in codegen.llvm.DeclGen.lowerTypeInner (zig)
                const layout = t.unionGetLayout(target);
                                               ^
/home/andy/Downloads/zig/src/codegen/llvm.zig:2501:43: 0x3324791 in codegen.llvm.DeclGen.lowerType (zig)
        const llvm_ty = try lowerTypeInner(dg, t);
                                          ^
/home/andy/Downloads/zig/src/codegen/llvm.zig:2912:53: 0x3508f66 in codegen.llvm.DeclGen.lowerTypeFn (zig)
                const raw_llvm_ty = try dg.lowerType(param_ty);
                                                    ^
/home/andy/Downloads/zig/src/codegen/llvm.zig:2864:38: 0x332a949 in codegen.llvm.DeclGen.lowerTypeInner (zig)
            .Fn => return lowerTypeFn(dg, t),
                                     ^
/home/andy/Downloads/zig/src/codegen/llvm.zig:2501:43: 0x3324791 in codegen.llvm.DeclGen.lowerType (zig)
        const llvm_ty = try lowerTypeInner(dg, t);
                                          ^
/home/andy/Downloads/zig/src/codegen/llvm.zig:2311:41: 0x331cbab in codegen.llvm.DeclGen.resolveLlvmFunction (zig)
        const fn_type = try dg.lowerType(zig_fn_type);
                                        ^
/home/andy/Downloads/zig/src/codegen/llvm.zig:3855:41: 0x332cdb0 in codegen.llvm.DeclGen.lowerDeclRefValue (zig)
            try self.resolveLlvmFunction(decl_index)
                                        ^
/home/andy/Downloads/zig/src/codegen/llvm.zig:3065:54: 0x332262f in codegen.llvm.DeclGen.lowerValue (zig)
                .decl_ref => return lowerDeclRefValue(dg, tv, tv.val.castTag(.decl_ref).?.data),
                                                     ^
/home/andy/Downloads/zig/src/codegen/llvm.zig:3238:45: 0x331fb47 in codegen.llvm.DeclGen.lowerValue (zig)
                        return dg.lowerValue(.{ .ty = payload_ty, .val = payload.data });
                                            ^
/home/andy/Downloads/zig/src/codegen/llvm.zig:4059:48: 0x3953361 in codegen.llvm.FuncGen.resolveInst (zig)
        const llvm_val = try self.dg.lowerValue(.{ .ty = ty, .val = val });
                                               ^
/home/andy/Downloads/zig/src/codegen/llvm.zig:4352:54: 0x372f29a in codegen.llvm.FuncGen.airCall (zig)
                const llvm_arg = try self.resolveInst(arg);
                                                     ^
/home/andy/Downloads/zig/src/codegen/llvm.zig:4213:55: 0x370ae68 in codegen.llvm.FuncGen.genBody (zig)
                .call              => try self.airCall(inst, .Auto),
                                                      ^
/home/andy/Downloads/zig/src/codegen/llvm.zig:1026:19: 0x37051db in codegen.llvm.Object.updateFunc (zig)
        fg.genBody(air.getMainBody()) catch |err| switch (err) {
                  ^
/home/andy/Downloads/zig/src/link/Elf.zig:2386:74: 0x34b76e7 in link.Elf.updateFunc (zig)
        if (self.llvm_object) |llvm_object| return llvm_object.updateFunc(module, func, air, liveness);
                                                                         ^
/home/andy/Downloads/zig/src/link.zig:509:77: 0x32d55dd in link.File.updateFunc (zig)
            .elf   => return @fieldParentPtr(Elf,   "base", base).updateFunc(module, func, air, liveness),
                                                                            ^
/home/andy/Downloads/zig/src/Module.zig:4243:37: 0x32b58d2 in Module.ensureFuncBodyAnalyzed (zig)
            comp.bin_file.updateFunc(mod, func, air, liveness) catch |err| switch (err) {
                                    ^
/home/andy/Downloads/zig/src/Compilation.zig:2966:42: 0x2ff353a in Compilation.processOneJob (zig)
            module.ensureFuncBodyAnalyzed(func) catch |err| switch (err) {
                                         ^
/home/andy/Downloads/zig/src/Compilation.zig:2898:30: 0x2fdc99d in Compilation.performAllTheWork (zig)
            try processOneJob(comp, work_item);
                             ^
/home/andy/Downloads/zig/src/Compilation.zig:2238:31: 0x2fd4e93 in Compilation.update (zig)
    try comp.performAllTheWork(main_progress_node);
                              ^
/home/andy/Downloads/zig/src/main.zig:3327:20: 0x2f438df in updateModule (zig)
    try comp.update();
                   ^
/home/andy/Downloads/zig/src/main.zig:3016:17: 0x2f0c0b4 in buildOutputType (zig)
    updateModule(gpa, comp, hook) catch |err| switch (err) {
                ^
/home/andy/Downloads/zig/src/main.zig:230:31: 0x2eefc07 in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .{ .build = .Exe });
                              ^
/home/andy/Downloads/zig/src/main.zig:174:20: 0x2eeeed4 in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
/home/andy/Downloads/zig/lib/std/start.zig:576:37: 0x2ef17ff in std.start.main (zig)
            const result = root.main() catch |err| {
                                    ^
error: globals...
error: The following command terminated unexpectedly:
/home/andy/Downloads/zig/build-release/stage2/bin/zig build-exe /home/andy/Downloads/zig-wayland/example/globals.zig -lc -I/nix/store/p591y75k9mgx4hhbjpj40dm6xdr5m8hl-wayland-1.19.0-dev/include -L/nix/store/x02dkixg5ppdmpy3k6k1k4ypcf8z3cw4-wayland-1.19.0/lib -lwayland-client -cflags -std=c99 -- /home/andy/Downloads/zig-wayland/zig-cache/zig-wayland/wayland-protocol.c --cache-dir /home/andy/Downloads/zig-wayland/zig-cache --global-cache-dir /home/andy/.cache/zig --name globals --pkg-begin wayland /home/andy/Downloads/zig-wayland/zig-cache/zig-wayland/wayland.zig --pkg-end --enable-cache 

I have checked that Sema is indeed calling queueFullTypeResolution for all of the arguments in the airCall that is being lowered. However, one of the arguments is a pointer to a comptime-known function, whose parameter type is itself the unresolved type. Meanwhile, in resolveTypeFully, pointer types are considered, however pointer values are not considered, so it does not find the function type.

My analysis is sus because I'm having trouble creating a reduction. However, if it is correct, then an elegant solution would be to wait and solve this with LLVM 15 opaque pointers. This would be trivially solved because resolveTypeFully would no longer need to recurse over pointers, and likewise llvm.lowerType would also no longer need to recurse over pointers. I can test this in the llvm15 branch and report back.

In the meantime, a workaround might be to simulate this to a degree by having llvm.lowerType no longer recurse into decl refs, instead always making a *i8 type. Users of lowerType for pointers would then be responsible to bitcast the pointer type correctly if they cared about the pointer child type. This work is already done in the llvm15 branch.

cc @ifreund

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions