diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2024-03-20 17:28:51 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2024-03-20 17:29:06 -0700 |
| commit | 4d5e0a0434a69059f623e5c51862fb1a2c553abc (patch) | |
| tree | 359645e37191ea162492f9814885d720421d52d2 /src/Sema.zig | |
| parent | 7741aca96c8cc6df7e8c4bd10ada741d6a3ffb9d (diff) | |
| download | zig-4d5e0a0434a69059f623e5c51862fb1a2c553abc.tar.gz zig-4d5e0a0434a69059f623e5c51862fb1a2c553abc.zip | |
Revert the last two commits in this branch
When the slice-by-length start position is runtime-known, it is likely
protected by a runtime-known condition and therefore a compile error is
less appropriate than a runtime panic check.
This is demonstrated in the json code that was updated and then reverted
in this commit.
When #3806 is implemented, this decision can be reassessed.
Revert "std: work around compiler unable to evaluate condition at compile time"
Revert "frontend: comptime array slice-by-length OOB detection"
This reverts commit 7741aca96c8cc6df7e8c4bd10ada741d6a3ffb9d.
This reverts commit 2583b389eaf5f7aaa0eb79b51126506c1e172d15.
Diffstat (limited to 'src/Sema.zig')
| -rw-r--r-- | src/Sema.zig | 37 |
1 files changed, 9 insertions, 28 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 764aa95ebe..6194e6c1bc 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -33285,34 +33285,15 @@ fn analyzeSlice( } bounds_check: { - const actual_len = l: { - if (array_ty.zigTypeTag(mod) == .Array) { - const len = array_ty.arrayLenIncludingSentinel(mod); - // If the end is comptime-known, we can emit a - // compile error if it would be out-of-bounds even - // with a start value of 0. - if (uncasted_end_opt != .none) { - if (try sema.resolveDefinedValue(block, end_src, uncasted_end_opt)) |end_val| { - const end_int = end_val.getUnsignedInt(mod).?; - if (end_int > len) return sema.fail( - block, - end_src, - "slice end index {d} exceeds array length of type '{}'", - .{ end_int, array_ty.fmt(mod) }, - ); - } - } - break :l try mod.intRef(Type.usize, len); - } - if (slice_ty.isSlice(mod)) { - const slice_len_inst = try block.addTyOp(.slice_len, Type.usize, ptr_or_slice); - break :l if (slice_ty.sentinel(mod) == null) - slice_len_inst - else - try sema.analyzeArithmetic(block, .add, slice_len_inst, .one, src, end_src, end_src, true); - } - break :bounds_check; - }; + const actual_len = if (array_ty.zigTypeTag(mod) == .Array) + try mod.intRef(Type.usize, array_ty.arrayLenIncludingSentinel(mod)) + else if (slice_ty.isSlice(mod)) l: { + const slice_len_inst = try block.addTyOp(.slice_len, Type.usize, ptr_or_slice); + break :l if (slice_ty.sentinel(mod) == null) + slice_len_inst + else + try sema.analyzeArithmetic(block, .add, slice_len_inst, .one, src, end_src, end_src, true); + } else break :bounds_check; const actual_end = if (slice_sentinel != null) try sema.analyzeArithmetic(block, .add, end, .one, src, end_src, end_src, true) |
