authorgravatar for pentuppup@noreply.codeberg.orgpentuppup <pentuppup@noreply.codeberg.org> 2025-09-10 11:59:47-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-08 05:05:39+01:00
logd2d8b969a1674a6583292631ca7decc94cb56145
treea31f406525e313829d841028f69204ff8be042cb
parentcdaf27931412b7806e568a41de1ac1eea4c46cfa

fix redundant safety checks being emitted for slicing


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

src/Sema.zig+14-9
...@@ -32272,9 +32272,15 @@ fn analyzeSlice(...@@ -32272,9 +32272,15 @@ fn analyzeSlice(
32272 var runtime_src: ?LazySrcLoc = null;32272 var runtime_src: ?LazySrcLoc = null;
3227332273
32274 // requirement: start <= end32274 // requirement: start <= end
32275 if (try sema.resolveDefinedValue(block, end_src, end)) |end_val| {32275 if (try sema.resolveDefinedValue(block, start_src, start)) |start_val| {
32276 if (try sema.resolveDefinedValue(block, start_src, start)) |start_val| {32276 if (try sema.compareAll(start_val, .eq, .zero_usize, .usize)) {
32277 if (!by_length and !(try sema.compareAll(start_val, .lte, end_val, .usize))) {32277 checked_start_lte_end = true;
32278 }
32279 if (try sema.resolveDefinedValue(block, end_src, end)) |end_val| {
32280 if (!checked_start_lte_end and
32281 !by_length and
32282 !(try sema.compareAll(start_val, .lte, end_val, .usize)))
32283 {
32278 return sema.fail(32284 return sema.fail(
32279 block,32285 block,
32280 start_src,32286 start_src,
...@@ -32330,10 +32336,10 @@ fn analyzeSlice(...@@ -32330,10 +32336,10 @@ fn analyzeSlice(
32330 runtime_src = ptr_src;32336 runtime_src = ptr_src;
32331 }32337 }
32332 } else {32338 } else {
32333 runtime_src = start_src;32339 runtime_src = end_src;
32334 }32340 }
32335 } else {32341 } else {
32336 runtime_src = end_src;32342 runtime_src = start_src;
32337 }32343 }
3233832344
32339 if (!checked_start_lte_end and block.wantSafety() and !block.isComptime()) {32345 if (!checked_start_lte_end and block.wantSafety() and !block.isComptime()) {
...@@ -32396,7 +32402,9 @@ fn analyzeSlice(...@@ -32396,7 +32402,9 @@ fn analyzeSlice(
32396 else32402 else
32397 end;32403 end;
3239832404
32399 try sema.addSafetyCheckIndexOob(block, src, actual_end, actual_len, .cmp_lte);32405 if (try sema.resolveDefinedValue(block, src, actual_len) == null or
32406 try sema.resolveDefinedValue(block, src, actual_end) == null)
32407 try sema.addSafetyCheckIndexOob(block, src, actual_end, actual_len, .cmp_lte);
32400 }32408 }
3240132409
32402 // requirement: result[new_len] == slice_sentinel32410 // requirement: result[new_len] == slice_sentinel
...@@ -32461,9 +32469,6 @@ fn analyzeSlice(...@@ -32461,9 +32469,6 @@ fn analyzeSlice(
32461 end;32469 end;
32462 try sema.addSafetyCheckIndexOob(block, src, actual_end, len_inst, .cmp_lte);32470 try sema.addSafetyCheckIndexOob(block, src, actual_end, len_inst, .cmp_lte);
32463 }32471 }
32464
32465 // requirement: start <= end
32466 try sema.addSafetyCheckIndexOob(block, src, start, end, .cmp_lte);
32467 }32472 }
32468 const result = try block.addInst(.{32473 const result = try block.addInst(.{
32469 .tag = .slice,32474 .tag = .slice,