authorgravatar for 51252236+xdBronch@users.noreply.github.comxdBronch <51252236+xdBronch@users.noreply.github.com> 2025-01-13 00:28:53-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-01-13 05:28:53+00:00
logfb43e91b226a9cde51967455c57989c0371d4b0a
treec411f5cbbbf9b162dd3bdc8b4b494145cc91cca7
parent5de880c288b0f340b57442d68685c79cf515fc78
signaturebadge-check Signed by PGP key B5690EEEBB952194

Sema: disallow non scalar sentinels in array types and reified types (#22473)


3 files changed, 58 insertions(+), 14 deletions(-)

src/Sema.zig+3
......@@ -8423,6 +8423,7 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil
84238423 .sentinel = sentinel_val.toIntern(),
84248424 .child = elem_type.toIntern(),
84258425 });
8426 try sema.checkSentinelType(block, sentinel_src, elem_type);
84268427
84278428 return Air.internedToRef(array_ty.toIntern());
84288429}
......@@ -21650,6 +21651,7 @@ fn zirReify(
2165021651 const sentinel_ptr_val = sentinel_val.optionalValue(zcu).?;
2165121652 const ptr_ty = try pt.singleMutPtrType(elem_ty);
2165221653 const sent_val = (try sema.pointerDeref(block, src, sentinel_ptr_val, ptr_ty)).?;
21654 try sema.checkSentinelType(block, src, elem_ty);
2165321655 break :s sent_val.toIntern();
2165421656 }
2165521657 break :s .none;
......@@ -21714,6 +21716,7 @@ fn zirReify(
2171421716 const child_ty = child_val.toType();
2171521717 const sentinel = if (sentinel_val.optionalValue(zcu)) |p| blk: {
2171621718 const ptr_ty = try pt.singleMutPtrType(child_ty);
21719 try sema.checkSentinelType(block, src, child_ty);
2171721720 break :blk (try sema.pointerDeref(block, src, p, ptr_ty)).?;
2171821721 } else null;
2171921722
test/cases/compile_errors/array slice sentinel mismatch non-scalar.zig deleted-14
......@@ -1,14 +0,0 @@
1export fn foo() void {
2 const S = struct { a: u32 };
3 const sentinel: S = .{ .a = 1 };
4 var arr = [_]S{ .{ .a = 1 }, .{ .a = 2 } };
5 const s = arr[0..1 :sentinel];
6 _ = s;
7}
8
9// error
10// backend=stage2
11// target=native
12//
13// :5:25: error: non-scalar sentinel type 'tmp.foo.S'
14// :2:15: note: struct declared here
test/cases/compile_errors/non_scalar_sentinel.zig created+55
......@@ -0,0 +1,55 @@
1const S = struct {};
2const sentinel: S = .{};
3
4comptime {
5 _ = [0:sentinel]S;
6}
7comptime {
8 _ = [:sentinel]S;
9}
10comptime {
11 _ = [*:sentinel]S;
12}
13
14comptime {
15 _ = @Type(.{ .array = .{ .child = S, .len = 0, .sentinel = &sentinel } });
16}
17comptime {
18 _ = @Type(.{ .pointer = .{
19 .size = .Many,
20 .is_const = false,
21 .is_volatile = false,
22 .alignment = @alignOf(S),
23 .address_space = .generic,
24 .child = S,
25 .is_allowzero = false,
26 .sentinel = &sentinel,
27 } });
28}
29comptime {
30 _ = @Type(.{ .pointer = .{
31 .size = .Many,
32 .is_const = false,
33 .is_volatile = false,
34 .alignment = @alignOf(S),
35 .address_space = .generic,
36 .child = S,
37 .is_allowzero = false,
38 .sentinel = &sentinel,
39 } });
40}
41
42// error
43//
44// :5:12: error: non-scalar sentinel type 'tmp.S'
45// :1:11: note: struct declared here
46// :8:11: error: non-scalar sentinel type 'tmp.S'
47// :1:11: note: struct declared here
48// :11:12: error: non-scalar sentinel type 'tmp.S'
49// :1:11: note: struct declared here
50// :15:9: error: non-scalar sentinel type 'tmp.S'
51// :1:11: note: struct declared here
52// :18:9: error: non-scalar sentinel type 'tmp.S'
53// :1:11: note: struct declared here
54// :30:9: error: non-scalar sentinel type 'tmp.S'
55// :1:11: note: struct declared here