zig
                                
                                 zig copied to clipboard
                                
                                    zig copied to clipboard
                            
                            
                            
                        unreachable when assuming block scope has no instructions
Zig Version
0.11.0-dev.3723+423d7b848
Steps to Reproduce and Observed Behavior
git clone https://github.com/davidgm94/rise.git
cd rise
git switch zig-unreachable
zig build
~/dev/rise| > | ../zig/build/stage3/bin/zig build                                                                                        [Last command returned (130)] [david@david] [22:59:59]
zig build-exe loader ReleaseSmall x86-freestanding-none: error: thread 113739 panic: reached unreachable code
Analyzing src/bootloader/rise/bios/main.zig: bootloader/rise/bios/main.zig:initialization
      %1581 = decl_val("Initialization") token_offset:270:21 to :270:35
      %1582 = as_node(@InternPool.Index.type_type, %1581) node_offset:270:21 to :270:35
      %1583 = as_node(%1582, @InternPool.Index.undef) node_offset:270:38 to :270:47
    > %1584 = extended(variable(%1582, init=%1583))
      %1585 = break_inline(%1580, %1584)
    For full context, use the command
      zig ast-check -t src/bootloader/rise/bios/main.zig
  in src/bootloader/rise/bios/main.zig: bootloader/rise/bios/main.zig:_start
    > %1608 = decl_ref("initialization") token_offset:275:5 to :275:19
  in src/bootloader/rise/bios/main.zig: bootloader/rise/bios/main.zig:_start
    > %1614 = block({%1608..%1613}) node_offset:275:5 to :275:68
  in src/bootloader/rise/bios/main.zig: bootloader/rise/bios/main.zig:_start
    > %1589 = block({%1590..%1662}) node_offset:272:42 to :272:43
/home/david/dev/zig/lib/std/debug.zig:258:14: 0x5dee686 in assert (zig)
    if (!ok) unreachable; // assertion failure
             ^
/home/david/dev/zig/src/Sema.zig:34092:15: 0x662d6ed in semaStructFields (zig)
        assert(block_scope.instructions.items.len == 0);
              ^
/home/david/dev/zig/src/Sema.zig:33891:25: 0x62edb21 in resolveTypeFieldsStruct (zig)
    try semaStructFields(sema.mod, struct_obj);
                        ^
/home/david/dev/zig/src/Sema.zig:33839:49: 0x62f10ff in resolveTypeFields (zig)
                try sema.resolveTypeFieldsStruct(ty, struct_obj);
                                                ^
/home/david/dev/zig/src/Sema.zig:26644:47: 0x62f8b23 in coerceExtra (zig)
    const dest_ty = try sema.resolveTypeFields(dest_ty_unresolved);
                                              ^
/home/david/dev/zig/src/Sema.zig:26592:28: 0x610f604 in coerce (zig)
    return sema.coerceExtra(block, dest_ty_unresolved, inst, inst_src, .{}) catch |err| switch (err) {
                           ^
/home/david/dev/zig/src/Sema.zig:34322:44: 0x662cf97 in semaStructFields (zig)
                const coerced = sema.coerce(&block_scope, field.ty, init, .unneeded) catch |err| switch (err) {
                                           ^
/home/david/dev/zig/src/Sema.zig:33891:25: 0x62edb21 in resolveTypeFieldsStruct (zig)
    try semaStructFields(sema.mod, struct_obj);
                        ^
/home/david/dev/zig/src/Sema.zig:33839:49: 0x62f10ff in resolveTypeFields (zig)
                try sema.resolveTypeFieldsStruct(ty, struct_obj);
                                                ^
/home/david/dev/zig/src/Sema.zig:35025:63: 0x64ea445 in typeHasOnePossibleValue (zig)
                const resolved_ty = try sema.resolveTypeFields(ty);
                                                              ^
/home/david/dev/zig/src/Sema.zig:35043:61: 0x64ea9a5 in typeHasOnePossibleValue (zig)
                        if (try sema.typeHasOnePossibleValue(field.ty)) |field_opv| {
                                                            ^
/home/david/dev/zig/src/Sema.zig:2048:41: 0x66692f5 in resolveMaybeUndefValAllowVariablesMaybeRuntime (zig)
    if (try sema.typeHasOnePossibleValue(sema.typeOf(inst))) |opv| {
                                        ^
/home/david/dev/zig/src/Sema.zig:2026:64: 0x6301a01 in resolveMaybeUndefValAllowVariables (zig)
    if (try sema.resolveMaybeUndefValAllowVariablesMaybeRuntime(inst, &make_runtime)) |val| {
                                                               ^
/home/david/dev/zig/src/Sema.zig:1973:61: 0x652d4ea in resolveMaybeUndefVal (zig)
    const val = (try sema.resolveMaybeUndefValAllowVariables(inst)) orelse return null;
                                                            ^
/home/david/dev/zig/src/Sema.zig:23576:51: 0x65d549d in zirVarExtended (zig)
        break :blk ((try sema.resolveMaybeUndefVal(init)) orelse
                                                  ^
/home/david/dev/zig/src/Sema.zig:1138:70: 0x62df8f9 in analyzeBodyInner (zig)
                    .variable              => try sema.zirVarExtended(       block, extended),
                                                                     ^
/home/david/dev/zig/src/Sema.zig:840:45: 0x6116851 in analyzeBodyBreak (zig)
    const break_inst = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                                            ^
/home/david/dev/zig/src/Module.zig:4556:50: 0x611385e in semaDecl (zig)
    const result_ref = (try sema.analyzeBodyBreak(&block_scope, body)).?.operand;
                                                 ^
/home/david/dev/zig/src/Module.zig:4071:32: 0x5f353d9 in ensureDeclAnalyzed (zig)
        break :blk mod.semaDecl(decl_index) catch |err| switch (err) {
                               ^
/home/david/dev/zig/src/Sema.zig:30238:27: 0x6a92a76 in ensureDeclAnalyzed (zig)
    mod.ensureDeclAnalyzed(decl_index) catch |err| {
                          ^
/home/david/dev/zig/src/Sema.zig:30300:32: 0x6b12910 in analyzeDeclRefInner (zig)
    try sema.ensureDeclAnalyzed(decl_index);
                               ^
/home/david/dev/zig/src/Sema.zig:30290:36: 0x666467e in analyzeDeclRef (zig)
    return sema.analyzeDeclRefInner(decl_index, true);
                                   ^
/home/david/dev/zig/src/Sema.zig:6160:31: 0x65487cc in zirDeclRef (zig)
    return sema.analyzeDeclRef(decl_index);
                              ^
/home/david/dev/zig/src/Sema.zig:954:65: 0x62d79f4 in analyzeBodyInner (zig)
            .decl_ref                     => try sema.zirDeclRef(block, inst),
                                                                ^
/home/david/dev/zig/src/Sema.zig:5661:34: 0x6af7ff9 in resolveBlockBody (zig)
        if (sema.analyzeBodyInner(child_block, body)) |_| {
                                 ^
/home/david/dev/zig/src/Sema.zig:5644:33: 0x661df8c in zirBlock (zig)
    return sema.resolveBlockBody(parent_block, src, &child_block, body, inst, &label.merges);
                                ^
/home/david/dev/zig/src/Sema.zig:1486:49: 0x62e8e74 in analyzeBodyInner (zig)
                    break :blk try sema.zirBlock(block, inst, tags[inst] == .block_comptime);
                                                ^
/home/david/dev/zig/src/Sema.zig:5661:34: 0x6af7ff9 in resolveBlockBody (zig)
        if (sema.analyzeBodyInner(child_block, body)) |_| {
                                 ^
/home/david/dev/zig/src/Sema.zig:5644:33: 0x661df8c in zirBlock (zig)
    return sema.resolveBlockBody(parent_block, src, &child_block, body, inst, &label.merges);
                                ^
/home/david/dev/zig/src/Sema.zig:1486:49: 0x62e8e74 in analyzeBodyInner (zig)
                    break :blk try sema.zirBlock(block, inst, tags[inst] == .block_comptime);
                                                ^
/home/david/dev/zig/src/Sema.zig:823:30: 0x64e34f8 in analyzeBody (zig)
    _ = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                             ^
/home/david/dev/zig/src/Module.zig:5566:21: 0x62b9575 in analyzeFnBody (zig)
    sema.analyzeBody(&inner_block, fn_info.body) catch |err| switch (err) {
                    ^
/home/david/dev/zig/src/Module.zig:4164:40: 0x60f65c9 in ensureFuncBodyAnalyzed (zig)
            var air = mod.analyzeFnBody(func_index, sema_arena) catch |err| switch (err) {
                                       ^
/home/david/dev/zig/src/Compilation.zig:3143:42: 0x60f45a9 in processOneJob (zig)
            module.ensureFuncBodyAnalyzed(func) catch |err| switch (err) {
                                         ^
/home/david/dev/zig/src/Compilation.zig:3080:30: 0x5f901ae in performAllTheWork (zig)
            try processOneJob(comp, work_item, main_progress_node);
                             ^
/home/david/dev/zig/src/Compilation.zig:2029:31: 0x5f8c858 in update (zig)
    try comp.performAllTheWork(main_progress_node);
                              ^
/home/david/dev/zig/src/main.zig:3417:36: 0x5fb8738 in serve (zig)
                    try comp.update(main_progress_node);
                                   ^
/home/david/dev/zig/src/main.zig:3231:22: 0x5e3fc71 in buildOutputType (zig)
            try serve(
                     ^
/home/david/dev/zig/src/main.zig:269:31: 0x5df05a1 in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .{ .build = .Exe });
                              ^
/home/david/dev/zig/src/main.zig:213:20: 0x5dedb05 in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
/home/david/dev/zig/lib/std/start.zig:608:37: 0x5ded588 in main (zig)
            const result = root.main() catch |err| {
                                    ^
zig build-exe loader ReleaseSmall x86-freestanding-none: error: the following command terminated unexpectedly:
/home/david/dev/zig/build/stage3/bin/zig build-exe --entry _start /home/david/dev/rise/src/bootloader/rise/bios/main.zig /home/david/dev/rise/src/bootloader/arch/x86/64/smp_trampoline.S /home/david/dev/rise/src/bootloader/rise/bios/unreal_mode.S -fstrip --compress-debug-sections=zlib --gc-sections -OReleaseSmall --cache-dir /home/david/dev/rise/zig-cache --global-cache-dir /home/david/.cache/zig --name loader -fno-stack-check -fno-stack-protector -mno-red-zone -mcmodel small -target x86-freestanding-none -mcpu pentium4-mmx+soft_float-sse-sse2-x87 --script /home/david/dev/rise/src/bootloader/rise/bios/linker_script.ld --mod lib:lib:/home/david/dev/rise/src/lib.zig --mod bootloader:bootloader,lib,privileged:/home/david/dev/rise/src/bootloader.zig --mod bios:bios,lib,privileged:/home/david/dev/rise/src/bootloader/bios.zig --mod privileged:privileged,lib,bootloader:/home/david/dev/rise/src/privileged.zig --deps lib,bootloader,privileged,bios --main-pkg-path /home/david/dev/rise/src --listen=- 
Build Summary: 5/7 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ zig build-exe loader ReleaseSmall x86-freestanding-none failure
error: the following build command failed with exit code 1:
/home/david/dev/rise/zig-cache/o/fb249fd7a056f57339fc8b8c05d617d8/build /home/david/dev/zig/build/stage3/bin/zig /home/david/dev/rise /home/david/dev/rise/zig-cache /home/david/.cache/zig
Expected Behavior
Not a crash. The release build doesn't crash, although I cannot assume the code generation is not affected.
Doesn't repro "on master":
rise/build.zig:98:93: error: no field named 'architecture' in struct 'json.static.Parsed(src.common.Configuration)'
            .architecture = b.standardTargetOptions(.{ .default_target = .{ .cpu_arch = cfg.architecture } }).getCpuArch(),
                                                                                            ^~~~~~~~~~~~
Yeah because you tried with master HEAD :). The version specified is older and there were some breaking changes. Now the branch is updated to fix all compile errors caused by breaking changes. The issue is still reproducible.
repro:
const S = struct {
    var a: u8 = 0;
    b: u8 = a,
};
export fn entry() void {
    _ = @as(S, undefined);
}
Note that even after fixing the crash, this should still be a compile error. The hacks for allowing runtime references from a comptime context have been removed from the compiler, so the following code is no longer valid:
        .cache_size = buffer.len,
But this would be taking the length of a global array, what's wrong with that? How can I fix it?
The value of the contents of buffer is only known at runtime, so referencing it from comptime is no longer allowed.  You can store the length in a const, which is referenceable from comptime, and reuse that when initializing buffer.  Also, something like @typeInfo(@TypeOf(buffer)).Array.len works.
Edit: I'm not seeing compile errors where I expect them, so I'm not sure if my information about what is valid is out of date, but the above still works as a workaround.
Thank you, that definitely no longer crashes :)