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 sometimes crashes when an error impacts comptime evaluation #18929

Open
mpfaff opened this issue Feb 14, 2024 · 0 comments
Open

Compiler sometimes crashes when an error impacts comptime evaluation #18929

mpfaff opened this issue Feb 14, 2024 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@mpfaff
Copy link
Contributor

mpfaff commented Feb 14, 2024

Zig Version

0.12.0-dev.2710+c3eb592a3

Steps to Reproduce and Observed Behavior

I don't have a simple reproduction for this, unfortunately. I see crashes identical or very similar to this when there are errors in my code, but not always. The pattern seems to be that if there is some code that has an error preventing it from being comptime evaluated, and code elsewhere tries to comptime evaluate it, there is a chance of a compiler crash.

In this case, the real cause of the compiler crash was an unused parameter in a completely different file (patch represents the fix):

 pub fn bytecode_handler_call_modifier(comptime tag: Bytecode.Tag) std.builtin.CallModifier {
+    _ = tag;
     comptime {
         // TODO: possibly return .never_inline for infrequently used Bytecodes
         if (lib.is_debug) return .never_inline;

         return .always_inline;
     }
 }
error: thread 16809377 panic: reached unreachable code
Analyzing src/clib.zig: clib.zig:wreath_VM_deinit
    > %783 = param("vm", {
        %781 = decl_val("wreath_VM_ptr") token_offset:144:32 to :144:45
        %782 = break_inline(%783, %781)
      }) token_offset:144:28 to :144:30
      %796 = func(ret_ty=@void_type, body={
        %784 = block({
          %785 = dbg_block_begin()
          %786 = dbg_stmt(2, 5)
          %787 = ref(%783) token_offset:145:5 to :145:7
          %788 = dbg_stmt(2, 7)
          %789 = dbg_stmt(2, 14)
          %790 = field_call(nodiscard .auto, %787, "deinit", []) node_offset:145:5 to :145:16
          %791 = dbg_block_end()
          %792 = restore_err_ret_index(%784.none)
          %793 = break(%784, @void_value)
        }) node_offset:144:52 to :144:52
        %794 = restore_err_ret_index(.none.none)
        %795 = ret_implicit(@void_value) token_offset:146:1 to :146:1
      }) (lbrace=1:52,rbrace=3:1) node_offset:144:1 to :144:10
      %797 = break_inline(%780, %796)
    For full context, use the command
      zig ast-check -t src/clib.zig


~/.tools/zig/0.12.0-dev.2710+c3eb592a3/lib/std/debug.zig:403:14: 0x10fd3ba0c in assert (zig)
    if (!ok) unreachable; // assertion failure
             ^
~/repo/zig/src/type.zig:443:15: 0x10feb04c3 in fromInterned (zig)
        assert(i != .none);
              ^
~/repo/zig/src/type.zig:2674:58: 0x11044b2e4 in comptimeOnlyAdvanced (zig)
                                if (try Type.fromInterned(field_ty).comptimeOnlyAdvanced(mod, opt_sema)) {
                                                         ^
~/repo/zig/src/type.zig:2583:69: 0x11044a858 in comptimeOnlyAdvanced (zig)
                        else => return child_ty.comptimeOnlyAdvanced(mod, opt_sema),
                                                                    ^
~/repo/zig/src/Sema.zig:38019:35: 0x110139c11 in typeRequiresComptime (zig)
    return ty.comptimeOnlyAdvanced(sema.mod, sema);
                                  ^
~/repo/zig/src/Sema.zig:9840:50: 0x1108fc375 in zirParam (zig)
    const is_comptime = sema.typeRequiresComptime(param_ty) catch |err| switch (err) {
                                                 ^
~/repo/zig/src/Sema.zig:1453:34: 0x11043f1eb in analyzeBodyInner (zig)
                try sema.zirParam(block, inst, false);
                                 ^
~/repo/zig/src/Sema.zig:939:45: 0x110152615 in analyzeBodyBreak (zig)
    const break_inst = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                                            ^
~/repo/zig/src/Module.zig:3945:50: 0x11014ed67 in semaDecl (zig)
    const result_ref = (try sema.analyzeBodyBreak(&block_scope, decl_bodies.value_body)).?.operand;
                                                 ^
~/repo/zig/src/Module.zig:3462:32: 0x10fe7e656 in ensureDeclAnalyzed (zig)
        break :blk mod.semaDecl(decl_index) catch |err| switch (err) {
                               ^
~/repo/zig/src/Compilation.zig:3643:38: 0x11012a62c in processOneJob (zig)
            module.ensureDeclAnalyzed(decl_index) catch |err| switch (err) {
                                     ^
~/repo/zig/src/Compilation.zig:3508:30: 0x10fed7723 in performAllTheWork (zig)
            try processOneJob(comp, work_item, main_progress_node);
                             ^
~/repo/zig/src/Compilation.zig:2191:31: 0x10fed33af in update (zig)
    try comp.performAllTheWork(main_progress_node);
                              ^
~/repo/zig/src/main.zig:4049:36: 0x10ff02750 in serve (zig)
                    try comp.update(main_progress_node);
                                   ^
~/repo/zig/src/main.zig:3310:22: 0x10ff1feea in buildOutputType (zig)
            try serve(
                     ^
~/repo/zig/src/main.zig:279:31: 0x10fd22a35 in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .{ .build = .Lib });
                              ^
~/repo/zig/src/main.zig:223:20: 0x10fd1fd27 in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
~/.tools/zig/0.12.0-dev.2710+c3eb592a3/lib/std/start.zig:511:37: 0x10fd1f976 in main (zig)
            const result = root.main() catch |err| {

Would modifying Type.fromInterned or its call-site to handle i == .none more gracefully solve this problem?

Expected Behavior

The compiler emits the correct error and does not hard crash.

@mpfaff mpfaff added the bug Observed behavior contradicts documented or intended behavior label Feb 14, 2024
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
Projects
None yet
Development

No branches or pull requests

1 participant