From 20d0018d79c25a0def440040eab2970e7a314130 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sun, 21 Aug 2022 12:48:26 +0300 Subject: [PATCH] Sema: ignore dbg_block instructions when checking for comptimeness Closes #12514 --- src/Sema.zig | 10 +++++----- test/behavior/eval.zig | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 28ff4cc1c2..f3ddf21206 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -3087,7 +3087,7 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro const candidate = block.instructions.items[search_index]; switch (air_tags[candidate]) { - .dbg_stmt => continue, + .dbg_stmt, .dbg_block_begin, .dbg_block_end => continue, .store => break candidate, else => break :ct, } @@ -3099,7 +3099,7 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro const candidate = block.instructions.items[search_index]; switch (air_tags[candidate]) { - .dbg_stmt => continue, + .dbg_stmt, .dbg_block_begin, .dbg_block_end => continue, .alloc => { if (Air.indexToRef(candidate) != alloc) break :ct; break; @@ -3317,7 +3317,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com const candidate = block.instructions.items[search_index]; switch (air_tags[candidate]) { - .dbg_stmt => continue, + .dbg_stmt, .dbg_block_begin, .dbg_block_end => continue, .store => break candidate, else => break :ct, } @@ -3329,7 +3329,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com const candidate = block.instructions.items[search_index]; switch (air_tags[candidate]) { - .dbg_stmt => continue, + .dbg_stmt, .dbg_block_begin, .dbg_block_end => continue, .bitcast => break candidate, else => break :ct, } @@ -3341,7 +3341,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com const candidate = block.instructions.items[search_index]; switch (air_tags[candidate]) { - .dbg_stmt => continue, + .dbg_stmt, .dbg_block_begin, .dbg_block_end => continue, .constant => break candidate, else => break :ct, } diff --git a/test/behavior/eval.zig b/test/behavior/eval.zig index 8a872f6378..49183227b5 100644 --- a/test/behavior/eval.zig +++ b/test/behavior/eval.zig @@ -1310,3 +1310,18 @@ test "repeated value is correctly expanded" { } }, res); } } + +test "value in if block is comptime known" { + if (builtin.zig_backend == .stage1) return error.SkipZigTest; + + const first = blk: { + const s = if (false) "a" else "b"; + break :blk "foo" ++ s; + }; + const second = blk: { + const S = struct { str: []const u8 }; + const s = if (false) S{ .str = "a" } else S{ .str = "b" }; + break :blk "foo" ++ s.str; + }; + comptime try expect(std.mem.eql(u8, first, second)); +}