diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2025-03-31 09:11:18 +0100 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-03-31 19:03:08 -0400 |
| commit | d53cc5e5b2ac51793ea19a847d8cee409af1dee3 (patch) | |
| tree | e60cee6a989fd62a64d93d9acdc19729f4c44ed7 /src | |
| parent | 0bdc0bb534ed6d3cc787b5bca44173d2b49480da (diff) | |
| download | zig-d53cc5e5b2ac51793ea19a847d8cee409af1dee3.tar.gz zig-d53cc5e5b2ac51793ea19a847d8cee409af1dee3.zip | |
Sema: allow `@ptrCast` slice of zero-bit type to slice of non-zero-bit type
This is actually completely well-defined. The resulting slice always has
0 elements. The only disallowed case is casting *to* a slice of a
zero-bit type, because in that case, you cna't figure out how many
destination elements to use (and there's *no* valid destination length
if the source slice corresponds to more than 0 bits).
Diffstat (limited to 'src')
| -rw-r--r-- | src/Sema.zig | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 3f231410a8..012bfb3a5c 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -22837,11 +22837,14 @@ fn ptrCastFull( if (src_slice_like_elem.comptimeOnly(zcu) or dest_elem.comptimeOnly(zcu)) { return sema.fail(block, src, "cannot infer length of slice of '{}' from slice of '{}'", .{ dest_elem.fmt(pt), src_slice_like_elem.fmt(pt) }); } - const src_elem_size = src_slice_like_elem.abiSize(zcu); + // It's okay for `src_slice_like_elem` to be 0-bit; the resulting slice will just always have 0 elements. + // However, `dest_elem` can't be 0-bit. If it were, then either the source slice has 0 bits and we don't + // know how what `result.len` should be, or the source has >0 bits and there is no valid `result.len`. const dest_elem_size = dest_elem.abiSize(zcu); - if (src_elem_size == 0 or dest_elem_size == 0) { + if (dest_elem_size == 0) { return sema.fail(block, src, "cannot infer length of slice of '{}' from slice of '{}'", .{ dest_elem.fmt(pt), src_slice_like_elem.fmt(pt) }); } + const src_elem_size = src_slice_like_elem.abiSize(zcu); break :need_len_change src_elem_size != dest_elem_size; } else false; |
