authorgravatar for 51252236+xdBronch@users.noreply.github.comxdBronch <51252236+xdBronch@users.noreply.github.com> 2025-11-15 14:53:32-05:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-11-16 22:59:51+00:00
log29a1d0c06ff80836d744d2ab5db3626bf17fa0d1
treea6724fdcb3fe2f8d548c2f3928a04effc54406c9
parent35d87c4406f53e74f5cdc6d003faac3ec376d0ba

sema: improve codegen of for loop safety checks


1 files changed, 11 insertions(+), 2 deletions(-)

src/Sema.zig+11-2
......@@ -4563,6 +4563,9 @@ fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
45634563 // This argument is a range.
45644564 const range_start = try sema.resolveInst(zir_arg_pair[0]);
45654565 const range_end = try sema.resolveInst(zir_arg_pair[1]);
4566 if (try sema.resolveDefinedValue(block, arg_src, range_start)) |start| {
4567 if (try sema.valuesEqual(start, .zero_usize, .usize)) break :l range_end;
4568 }
45664569 break :l try sema.analyzeArithmetic(block, .sub, range_end, range_start, arg_src, arg_src, arg_src, true);
45674570 };
45684571 const arg_len = try sema.coerce(block, .usize, arg_len_uncoerced, arg_src);
......@@ -4626,12 +4629,18 @@ fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
46264629
46274630 // Now for the runtime checks.
46284631 if (any_runtime and block.wantSafety()) {
4632 var ok: Air.Inst.Ref = .none;
46294633 for (runtime_arg_lens, 0..) |arg_len, i| {
46304634 if (arg_len == .none) continue;
46314635 if (i == len_idx) continue;
4632 const ok = try block.addBinOp(.cmp_eq, len, arg_len);
4633 try sema.addSafetyCheck(block, src, ok, .for_len_mismatch);
4636 const eq = try block.addBinOp(.cmp_eq, len, arg_len);
4637 ok = if (ok != .none)
4638 try block.addBinOp(.bool_and, ok, eq)
4639 else
4640 eq;
46344641 }
4642 if (ok != .none)
4643 try sema.addSafetyCheck(block, src, ok, .for_len_mismatch);
46354644 }
46364645
46374646 return len;