Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler crash when importing a non-existent file that is referenced indirectly #18264

Open
emulbreh opened this issue Dec 12, 2023 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@emulbreh
Copy link

emulbreh commented Dec 12, 2023

Zig Version

0.12.0-dev.1802+56deb5b05

Steps to Reproduce and Observed Behavior

// This file is referred to as 'bug.zig' below

const std = @import("std");

const ImportedStruct = @import("this-file-does-not-exist");

const WrappedImportedStruct = struct {
    x: ImportedStruct, // indirection is important
};

const A = struct {
    // removing the `x` field prevents the crash but also reports `duplicate struct field: 'f'`
    x: u64,
    f: ?*const fn () WrappedImportedStruct,
};

fn init() ?std.hash_map.AutoHashMap(*A, void) {
    return null;
}

pub fn main() void {
    _ = init();
}
$ zig version
0.12.0-dev.1817+f88b52306
$ zig test bug.zig
thread 52088666 panic: reached unreachable code
Analyzing /Users/emulbreh/projects/ziglang/zig/lib/std/hash_map.zig: hash_map.zig:HashMapUnmanaged(*bug.A,void,hash_map.AutoContext(*bug.A),80).dbHelper
      %9777 = param("self", {
        %9774 = decl_val("Self") 
        %9775 = ptr_type(%9774, One) 
        %9776 = break_inline(%9777, %9775)
      }) 
    > %9781 = param("hdr", {
        %9778 = decl_val("Header") 
        %9779 = ptr_type(%9778, One) 
        %9780 = break_inline(%9781, %9779)
      }) 
      %9785 = param("entry", {
        %9782 = decl_val("Entry") 
        %9783 = ptr_type(%9782, One) 
        %9784 = break_inline(%9785, %9783)
      }) 
      %9799 = func(ret_ty=@void_type, body={
        %9786 = block({
          %9787 = dbg_block_begin()
          %9788 = dbg_stmt(2, 13)
          %9789 = ensure_result_non_error(%9777) 
          %9790 = dbg_stmt(3, 13)
          %9791 = ensure_result_non_error(%9781) 
          %9792 = dbg_stmt(4, 13)
          %9793 = ensure_result_non_error(%9785) 
          %9794 = dbg_block_end()
          %9795 = restore_err_ret_index(%9786.none)
          %9796 = break(%9786, @void_value)
        }) 
        %9797 = restore_err_ret_index(.none.none)
        %9798 = ret_implicit(@void_value) 
      }) (lbrace=1:68,rbrace=5:9) 
      %9800 = break_inline(%9773, %9799)
    For full context, use the command
      zig ast-check -t /Users/emulbreh/projects/ziglang/zig/lib/std/hash_map.zig

  in /Users/emulbreh/projects/ziglang/zig/lib/std/hash_map.zig: hash_map.zig:HashMapUnmanaged(*bug.A,void,hash_map.AutoContext(*bug.A),80).comptime_2
    > %9809 = decl_ref("dbHelper") 
  in /Users/emulbreh/projects/ziglang/zig/lib/std/hash_map.zig: hash_map.zig:HashMapUnmanaged(*bug.A,void,hash_map.AutoContext(*bug.A),80).comptime_2
    > %9807 = condbr(%9806, {%9809..%9811}, {%9812}) 
  in /Users/emulbreh/projects/ziglang/zig/lib/std/hash_map.zig: hash_map.zig:HashMapUnmanaged(*bug.A,void,hash_map.AutoContext(*bug.A),80).comptime_2
    > %9808 = block({%9802..%9807}) 

/Users/emulbreh/projects/ziglang/zig/build/stage3/lib/zig/std/debug.zig:342:14: 0x10423f69c in assert (zig)
    if (!ok) unreachable; // assertion failure
             ^
/Users/emulbreh/projects/ziglang/zig/src/type.zig:439:15: 0x10438eb63 in fromInterned (zig)
        assert(i != .none);
              ^
/Users/emulbreh/projects/ziglang/zig/src/type.zig:2655:58: 0x1048964a4 in comptimeOnlyAdvanced (zig)
                                if (try Type.fromInterned(field_ty).comptimeOnlyAdvanced(mod, opt_sema)) {
                                                         ^
/Users/emulbreh/projects/ziglang/zig/src/type.zig:2564:69: 0x104895a18 in comptimeOnlyAdvanced (zig)
                        else => return child_ty.comptimeOnlyAdvanced(mod, opt_sema),
                                                                    ^
/Users/emulbreh/projects/ziglang/zig/src/type.zig:2564:69: 0x104895a18 in comptimeOnlyAdvanced (zig)
                        else => return child_ty.comptimeOnlyAdvanced(mod, opt_sema),
                                                                    ^
/Users/emulbreh/projects/ziglang/zig/src/type.zig:2655:89: 0x1048964d8 in comptimeOnlyAdvanced (zig)
                                if (try Type.fromInterned(field_ty).comptimeOnlyAdvanced(mod, opt_sema)) {
                                                                                        ^
/Users/emulbreh/projects/ziglang/zig/src/type.zig:2564:69: 0x104895a18 in comptimeOnlyAdvanced (zig)
                        else => return child_ty.comptimeOnlyAdvanced(mod, opt_sema),
                                                                    ^
/Users/emulbreh/projects/ziglang/zig/src/Sema.zig:37337:35: 0x1045e86d1 in typeRequiresComptime (zig)
    return ty.comptimeOnlyAdvanced(sema.mod, sema);
                                  ^
/Users/emulbreh/projects/ziglang/zig/src/Sema.zig:9709:50: 0x104cab575 in zirParam (zig)
    const is_comptime = sema.typeRequiresComptime(param_ty) catch |err| switch (err) {
                                                 ^
/Users/emulbreh/projects/ziglang/zig/src/Sema.zig:1443:34: 0x10488a5ee in analyzeBodyInner (zig)
                try sema.zirParam(block, inst, false);
                                 ^
/Users/emulbreh/projects/ziglang/zig/src/Sema.zig:933:45: 0x1045ffe75 in analyzeBodyBreak (zig)
    const break_inst = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                                            ^
/Users/emulbreh/projects/ziglang/zig/src/Module.zig:3681:50: 0x1045fd348 in semaDecl (zig)
    const result_ref = (try sema.analyzeBodyBreak(&block_scope, @ptrCast(body))).?.operand;
                                                 ^
/Users/emulbreh/projects/ziglang/zig/src/Module.zig:3255:32: 0x104372e11 in ensureDeclAnalyzed (zig)
        break :blk mod.semaDecl(decl_index) catch |err| switch (err) {
                               ^
/Users/emulbreh/projects/ziglang/zig/src/Sema.zig:31668:27: 0x1051f371a in ensureDeclAnalyzed (zig)
    mod.ensureDeclAnalyzed(decl_index) catch |err| {
                          ^
/Users/emulbreh/projects/ziglang/zig/src/Sema.zig:31713:32: 0x10528d09b in analyzeDeclRefInner (zig)
    try sema.ensureDeclAnalyzed(decl_index);
                               ^
/Users/emulbreh/projects/ziglang/zig/src/Sema.zig:31704:36: 0x104cfa5af in analyzeDeclRef (zig)
    return sema.analyzeDeclRefInner(decl_index, true);
                                   ^
/Users/emulbreh/projects/ziglang/zig/src/Sema.zig:6371:31: 0x104bf848f in zirDeclRef (zig)
    return sema.analyzeDeclRef(decl_index);
                              ^
/Users/emulbreh/projects/ziglang/zig/src/Sema.zig:1037:65: 0x10487ecc5 in analyzeBodyInner (zig)
            .decl_ref                     => try sema.zirDeclRef(block, inst),
                                                                ^
/Users/emulbreh/projects/ziglang/zig/src/Sema.zig:933:45: 0x1045ffe75 in analyzeBodyBreak (zig)
    const break_inst = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                                            ^
/Users/emulbreh/projects/ziglang/zig/src/Sema.zig:1702:62: 0x10488cbdc in analyzeBodyInner (zig)
                const break_data = (try sema.analyzeBodyBreak(block, inline_body)) orelse
                                                             ^
/Users/emulbreh/projects/ziglang/zig/src/Sema.zig:933:45: 0x1045ffe75 in analyzeBodyBreak (zig)
    const break_inst = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                                            ^
/Users/emulbreh/projects/ziglang/zig/src/Sema.zig:1589:62: 0x10488f11d in analyzeBodyInner (zig)
                const break_data = (try sema.analyzeBodyBreak(block, inline_body)) orelse
                                                             ^
/Users/emulbreh/projects/ziglang/zig/src/Sema.zig:933:45: 0x1045ffe75 in analyzeBodyBreak (zig)
    const break_inst = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                                            ^
/Users/emulbreh/projects/ziglang/zig/src/Module.zig:3681:50: 0x1045fd348 in semaDecl (zig)
    const result_ref = (try sema.analyzeBodyBreak(&block_scope, @ptrCast(body))).?.operand;
                                                 ^
/Users/emulbreh/projects/ziglang/zig/src/Module.zig:3255:32: 0x104372e11 in ensureDeclAnalyzed (zig)
        break :blk mod.semaDecl(decl_index) catch |err| switch (err) {
                               ^
/Users/emulbreh/projects/ziglang/zig/src/Compilation.zig:3831:38: 0x1045e0110 in processOneJob (zig)
            module.ensureDeclAnalyzed(decl_index) catch |err| switch (err) {
                                     ^
/Users/emulbreh/projects/ziglang/zig/src/Compilation.zig:3702:30: 0x1043b3f5b in performAllTheWork (zig)
            try processOneJob(comp, work_item, main_progress_node);
                             ^
/Users/emulbreh/projects/ziglang/zig/src/Compilation.zig:2508:31: 0x1043af6cf in update (zig)
    try comp.performAllTheWork(main_progress_node);
                              ^
/Users/emulbreh/projects/ziglang/zig/src/main.zig:4300:24: 0x1043e10d3 in updateModule (zig)

                       ^
/Users/emulbreh/projects/ziglang/zig/src/main.zig:3690:17: 0x1044018d8 in buildOutputType (zig)
        error.SemanticAnalyzeFail => {
                ^
/Users/emulbreh/projects/ziglang/zig/src/main.zig:278:31: 0x1042261d5 in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .{ .build = .Exe });
                              ^
/Users/emulbreh/projects/ziglang/zig/src/main.zig:222:20: 0x104223777 in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
/Users/emulbreh/projects/ziglang/zig/build/stage3/lib/zig/std/start.zig:585:37: 0x1042233be in main (zig)
            const result = root.main() catch |err| {
                                    ^
???:?:?: 0x21c7b941e in ??? (???)
Unwind information for `???:0x21c7b941e` was not available, trace may be incomplete

???:?:?: 0x0 in ??? (???)
Abort trap: 6

Other versions

$ zig version
0.12.0-dev.1817+f88b52306
$ zig build-exe bug.zig
Segmentation fault: 11
$ zig version
0.11.0
$ zig build-exe bug.zig
bug.zig:3:32: error: no package named 'this-file-does-not-exist' available within package 'root'
const ImportedStruct = @import("this-file-does-not-exist");
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    WrappedImportedStruct: bug.zig:6:8
    WrappedImportedStruct: bug.zig:5:35
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

Expected Behavior

No crash

@emulbreh emulbreh added the bug Observed behavior contradicts documented or intended behavior label Dec 12, 2023
@emulbreh
Copy link
Author

This might be a duplicate of #17859

@Vexu Vexu added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Dec 13, 2023
@Vexu Vexu added this to the 0.13.0 milestone Dec 13, 2023
@ianprime0509
Copy link
Contributor

As of 0.14.0-dev.2546+0ff0bdb4a, this bug appears to have been fixed. Running zig build-exe bug.zig on the code provided in the issue description gives this output:

bug.zig:3:32: error: no module named 'this-file-does-not-exist' available within module bug
const ImportedStruct = @import("this-file-does-not-exist");
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    bug.WrappedImportedStruct: bug.zig:6:8
    WrappedImportedStruct: bug.zig:5:31
    9 reference(s) hidden; use '-freference-trace=11' to see all references

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

No branches or pull requests

3 participants