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(
3227232272 var runtime_src: ?LazySrcLoc = null;
3227332273
3227432274 // requirement: start <= end
32275 if (try sema.resolveDefinedValue(block, end_src, end)) |end_val| {
32276 if (try sema.resolveDefinedValue(block, start_src, start)) |start_val| {
32277 if (!by_length and !(try sema.compareAll(start_val, .lte, end_val, .usize))) {
32275 if (try sema.resolveDefinedValue(block, start_src, start)) |start_val| {
32276 if (try sema.compareAll(start_val, .eq, .zero_usize, .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 {
3227832284 return sema.fail(
3227932285 block,
3228032286 start_src,
......@@ -32330,10 +32336,10 @@ fn analyzeSlice(
3233032336 runtime_src = ptr_src;
3233132337 }
3233232338 } else {
32333 runtime_src = start_src;
32339 runtime_src = end_src;
3233432340 }
3233532341 } else {
32336 runtime_src = end_src;
32342 runtime_src = start_src;
3233732343 }
3233832344
3233932345 if (!checked_start_lte_end and block.wantSafety() and !block.isComptime()) {
......@@ -32396,7 +32402,9 @@ fn analyzeSlice(
3239632402 else
3239732403 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);
3240032408 }
3240132409
3240232410 // requirement: result[new_len] == slice_sentinel
......@@ -32461,9 +32469,6 @@ fn analyzeSlice(
3246132469 end;
3246232470 try sema.addSafetyCheckIndexOob(block, src, actual_end, len_inst, .cmp_lte);
3246332471 }
32464
32465 // requirement: start <= end
32466 try sema.addSafetyCheckIndexOob(block, src, start, end, .cmp_lte);
3246732472 }
3246832473 const result = try block.addInst(.{
3246932474 .tag = .slice,