authorgravatar for 51252236+xdBronch@users.noreply.github.comxdBronch <51252236+xdBronch@users.noreply.github.com> 2025-10-27 19:41:32-04:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-11-07 12:12:32+00:00
log92f64899c110808a80c70d05e572bbd8e2448732
tree55aac0928bd9e8e941bb8d75ed91e12536478795
parent19af9fa488ba7ddf2e7fd8f2f06ab594f7e23f91

sema: disallow slices of opaque types


7 files changed, 60 insertions(+), 43 deletions(-)

src/Sema.zig+4-10
......@@ -19168,8 +19168,8 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1916819168 if (inst_data.size != .one) {
1916919169 return sema.fail(block, elem_ty_src, "function pointers must be single pointers", .{});
1917019170 }
19171 } else if (inst_data.size == .many and elem_ty.zigTypeTag(zcu) == .@"opaque") {
19172 return sema.fail(block, elem_ty_src, "unknown-length pointer to opaque not allowed", .{});
19171 } else if (inst_data.size != .one and elem_ty.zigTypeTag(zcu) == .@"opaque") {
19172 return sema.fail(block, elem_ty_src, "indexable pointer to opaque type '{f}' not allowed", .{elem_ty.fmt(pt)});
1917319173 } else if (inst_data.size == .c) {
1917419174 if (!try sema.validateExternType(elem_ty, .other)) {
1917519175 const msg = msg: {
......@@ -19183,9 +19183,6 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1918319183 };
1918419184 return sema.failWithOwnedErrorMsg(block, msg);
1918519185 }
19186 if (elem_ty.zigTypeTag(zcu) == .@"opaque") {
19187 return sema.fail(block, elem_ty_src, "C pointers cannot point to opaque types", .{});
19188 }
1918919186 }
1919019187
1919119188 if (host_size != 0 and !try sema.validatePackedType(elem_ty)) {
......@@ -20674,8 +20671,8 @@ fn zirReify(
2067420671 if (ptr_size != .one) {
2067520672 return sema.fail(block, src, "function pointers must be single pointers", .{});
2067620673 }
20677 } else if (ptr_size == .many and elem_ty.zigTypeTag(zcu) == .@"opaque") {
20678 return sema.fail(block, src, "unknown-length pointer to opaque not allowed", .{});
20674 } else if (ptr_size != .one and elem_ty.zigTypeTag(zcu) == .@"opaque") {
20675 return sema.fail(block, src, "indexable pointer to opaque type '{f}' not allowed", .{elem_ty.fmt(pt)});
2067920676 } else if (ptr_size == .c) {
2068020677 if (!try sema.validateExternType(elem_ty, .other)) {
2068120678 const msg = msg: {
......@@ -20689,9 +20686,6 @@ fn zirReify(
2068920686 };
2069020687 return sema.failWithOwnedErrorMsg(block, msg);
2069120688 }
20692 if (elem_ty.zigTypeTag(zcu) == .@"opaque") {
20693 return sema.fail(block, src, "C pointers cannot point to opaque types", .{});
20694 }
2069520689 }
2069620690
2069720691 const ty = try pt.ptrTypeSema(.{
test/cases/compile_errors/C_pointer_to_anyopaque.zig deleted-9
......@@ -1,9 +0,0 @@
1export fn a() void {
2 var x: *anyopaque = undefined;
3 var y: [*c]anyopaque = x;
4 _ = .{ &x, &y };
5}
6
7// error
8//
9// :3:16: error: C pointers cannot point to opaque types
test/cases/compile_errors/double_pointer_to_anyopaque_pointer.zig-8
......@@ -15,12 +15,6 @@ pub export fn entry3() void {
1515 const ptr: *const anyopaque = x;
1616 _ = ptr;
1717}
18export fn entry4() void {
19 var a: []*u32 = undefined;
20 _ = &a;
21 var b: []anyopaque = undefined;
22 b = a;
23}
2418
2519// error
2620//
......@@ -31,5 +25,3 @@ export fn entry4() void {
3125// :11:12: note: parameter type declared here
3226// :15:35: error: expected type '*const anyopaque', found '*?*usize'
3327// :15:35: note: cannot implicitly cast double pointer '*?*usize' to anyopaque pointer '*const anyopaque'
34// :22:9: error: expected type '[]anyopaque', found '[]*u32'
35// :22:9: note: cannot implicitly cast double pointer '[]*u32' to anyopaque pointer '[]anyopaque'
test/cases/compile_errors/invalid_pointer_to_opaque.zig created+55
......@@ -0,0 +1,55 @@
1export fn a() void {
2 _ = []anyopaque;
3}
4export fn b() void {
5 _ = [*]anyopaque;
6}
7export fn c() void {
8 _ = [*c]anyopaque;
9}
10
11export fn d() void {
12 _ = @Type(.{ .pointer = .{
13 .size = .slice,
14 .is_const = false,
15 .is_volatile = false,
16 .alignment = 1,
17 .address_space = .generic,
18 .child = anyopaque,
19 .is_allowzero = false,
20 .sentinel_ptr = null,
21 } });
22}
23export fn e() void {
24 _ = @Type(.{ .pointer = .{
25 .size = .many,
26 .is_const = false,
27 .is_volatile = false,
28 .alignment = 1,
29 .address_space = .generic,
30 .child = anyopaque,
31 .is_allowzero = false,
32 .sentinel_ptr = null,
33 } });
34}
35export fn f() void {
36 _ = @Type(.{ .pointer = .{
37 .size = .c,
38 .is_const = false,
39 .is_volatile = false,
40 .alignment = 1,
41 .address_space = .generic,
42 .child = anyopaque,
43 .is_allowzero = false,
44 .sentinel_ptr = null,
45 } });
46}
47
48// error
49//
50// :2:11: error: indexable pointer to opaque type 'anyopaque' not allowed
51// :5:12: error: indexable pointer to opaque type 'anyopaque' not allowed
52// :8:13: error: indexable pointer to opaque type 'anyopaque' not allowed
53// :12:9: error: indexable pointer to opaque type 'anyopaque' not allowed
54// :24:9: error: indexable pointer to opaque type 'anyopaque' not allowed
55// :36:9: error: indexable pointer to opaque type 'anyopaque' not allowed
test/cases/compile_errors/pointer_to_anyopaque_slice.zig deleted-10
......@@ -1,10 +0,0 @@
1export fn x() void {
2 var a: *u32 = undefined;
3 var b: []anyopaque = undefined;
4 b = a;
5 _ = &a;
6}
7
8// error
9//
10// :4:9: error: expected type '[]anyopaque', found '*u32'
test/cases/compile_errors/unknown_length_pointer_to_opaque.zig deleted-5
......@@ -1,5 +0,0 @@
1export const T = [*]opaque {};
2
3// error
4//
5// :1:21: error: unknown-length pointer to opaque not allowed
test/cases/error_in_nested_declaration.zig+1-1
......@@ -27,4 +27,4 @@ pub export fn entry2() void {
2727//
2828// :6:20: error: cannot @bitCast to '[]i32'
2929// :6:20: note: use @ptrCast to cast from '[]u32'
30// :17:12: error: C pointers cannot point to opaque types
30// :17:12: error: indexable pointer to opaque type 'anyopaque' not allowed