From f19731948e934e8e9877729cab871ed05463d3c4 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 18 Mar 2022 22:21:51 +0200 Subject: [PATCH] Sema: balance dbg_block_begins in case of early return --- src/Sema.zig | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Sema.zig b/src/Sema.zig index c7e98e35b6..13c3db372c 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -614,6 +614,8 @@ fn analyzeBodyInner( crash_info.push(); defer crash_info.pop(); + var dbg_block_begins: u32 = 0; + // We use a while(true) loop here to avoid a redundant way of breaking out of // the loop. The only way to break out of the loop is with a `noreturn` // instruction. @@ -884,11 +886,13 @@ fn analyzeBodyInner( .prefetch => try sema.zirPrefetch( block, extended), // zig fmt: on .dbg_block_begin => { + dbg_block_begins += 1; try sema.zirDbgBlockBegin(block); i += 1; continue; }, .dbg_block_end => { + dbg_block_begins -= 1; try sema.zirDbgBlockEnd(block); i += 1; continue; @@ -1221,6 +1225,19 @@ fn analyzeBodyInner( i += 1; } else unreachable; + // balance out dbg_block_begins in case of early noreturn + const noreturn_inst = block.instructions.popOrNull(); + while (dbg_block_begins > 0) { + dbg_block_begins -= 1; + if (block.is_comptime or sema.mod.comp.bin_file.options.strip) continue; + + _ = try block.addInst(.{ + .tag = .dbg_block_end, + .data = undefined, + }); + } + if (noreturn_inst) |some| try block.instructions.append(sema.gpa, some); + if (!wip_captures.finalized) { try wip_captures.finalize(); block.wip_capture_scope = parent_capture_scope;