Skip to content

Commit 29a1d0c

Browse files
xdBronchmlugg
authored andcommitted
sema: improve codegen of for loop safety checks
1 parent 35d87c4 commit 29a1d0c

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/Sema.zig

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

0 commit comments

Comments
 (0)