authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-02-05 19:53:12+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-02-05 19:53:12+00:00
logfbe0ae4fd4128a3652071b4b5985d68ae56e9992
treea122df94fa0b93d6c0b4c28bf5d5ae9d9385d282
parent0f38558435a0f73c4c025b5641bd8e531f063e0c
signaturelock-open Commit is signed but in an unrecognized format.

Sema: fix PTR of slice of sentinel-terminated array

Resolves: #20901

2 files changed, 18 insertions(+), 2 deletions(-)

src/Sema.zig+2-2
......@@ -34603,14 +34603,14 @@ fn resolvePeerTypesInner(
3460334603 }
3460434604 // Clear existing sentinel
3460534605 ptr_info.sentinel = .none;
34606 switch (ip.indexToKey(ptr_info.child)) {
34606 if (ptr_info.flags.size == .one) switch (ip.indexToKey(ptr_info.child)) {
3460734607 .array_type => |array_type| ptr_info.child = (try pt.arrayType(.{
3460834608 .len = array_type.len,
3460934609 .child = array_type.child,
3461034610 .sentinel = .none,
3461134611 })).toIntern(),
3461234612 else => {},
34613 }
34613 };
3461434614 }
3461534615
3461634616 opt_ptr_info = ptr_info;
test/behavior/cast.zig+16
......@@ -2649,3 +2649,19 @@ test "bitcast vector" {
26492649 const bigsum: u32x8 = @bitCast(zerox32);
26502650 try std.testing.expectEqual(0, @reduce(.Add, bigsum));
26512651}
2652
2653test "peer type resolution: slice of sentinel-terminated array" {
2654 var f: bool = undefined;
2655 f = false;
2656
2657 const a: [][2:0]u8 = &.{};
2658 const b: []const [2:0]u8 = &.{.{ 10, 20 }};
2659
2660 const result = if (f) a else b;
2661
2662 comptime assert(@TypeOf(result) == []const [2:0]u8);
2663 try expect(result.len == 1);
2664 try expect(result[0].len == 2);
2665 try expect(result[0][0] == 10);
2666 try expect(result[0][1] == 20);
2667}