authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-24 17:44:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-24 17:47:39-07:00
log9a1d5001d4bf1f28bd0f23e8b936d677e0e5aac8
tree610bf3b91a5a02521b956fbb3ac7187f6f8449fc
parentf5f5b9373deae53a544497147cfd1380df34c000

Sema: fix false negative detecting comptime const

The code for detecting when a local const initialization expression ended up being comptime-known gave up when it encountered dbg_stmt instructions, but such instructions are not supposed to matter.

1 files changed, 41 insertions(+), 9 deletions(-)

src/Sema.zig+41-9
......@@ -2731,17 +2731,49 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
27312731 // instructions from the block, replacing the inst_map entry
27322732 // corresponding to the ZIR alloc instruction with a constant
27332733 // decl_ref pointing at our new Decl.
2734 // dbg_stmt instructions may be interspersed into this pattern
2735 // which must be ignored.
27342736 if (block.instructions.items.len < 3) break :ct;
2735 // zig fmt: off
2736 const const_inst = block.instructions.items[block.instructions.items.len - 3];
2737 const bitcast_inst = block.instructions.items[block.instructions.items.len - 2];
2738 const store_inst = block.instructions.items[block.instructions.items.len - 1];
2739 const air_tags = sema.air_instructions.items(.tag);
2737 var search_index: usize = block.instructions.items.len;
2738 const air_tags = sema.air_instructions.items(.tag);
27402739 const air_datas = sema.air_instructions.items(.data);
2741 if (air_tags[const_inst] != .constant) break :ct;
2742 if (air_tags[bitcast_inst] != .bitcast ) break :ct;
2743 if (air_tags[store_inst] != .store ) break :ct;
2744 // zig fmt: on
2740
2741 const store_inst = while (true) {
2742 if (search_index == 0) break :ct;
2743 search_index -= 1;
2744
2745 const candidate = block.instructions.items[search_index];
2746 switch (air_tags[candidate]) {
2747 .dbg_stmt => continue,
2748 .store => break candidate,
2749 else => break :ct,
2750 }
2751 } else unreachable; // TODO shouldn't need this
2752
2753 const bitcast_inst = while (true) {
2754 if (search_index == 0) break :ct;
2755 search_index -= 1;
2756
2757 const candidate = block.instructions.items[search_index];
2758 switch (air_tags[candidate]) {
2759 .dbg_stmt => continue,
2760 .bitcast => break candidate,
2761 else => break :ct,
2762 }
2763 } else unreachable; // TODO shouldn't need this
2764
2765 const const_inst = while (true) {
2766 if (search_index == 0) break :ct;
2767 search_index -= 1;
2768
2769 const candidate = block.instructions.items[search_index];
2770 switch (air_tags[candidate]) {
2771 .dbg_stmt => continue,
2772 .constant => break candidate,
2773 else => break :ct,
2774 }
2775 } else unreachable; // TODO shouldn't need this
2776
27452777 const store_op = air_datas[store_inst].bin_op;
27462778 const store_val = (try sema.resolveMaybeUndefVal(block, src, store_op.rhs)) orelse break :ct;
27472779 if (store_op.lhs != Air.indexToRef(bitcast_inst)) break :ct;