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...@@ -19168,8 +19168,8 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
19168 if (inst_data.size != .one) {19168 if (inst_data.size != .one) {
19169 return sema.fail(block, elem_ty_src, "function pointers must be single pointers", .{});19169 return sema.fail(block, elem_ty_src, "function pointers must be single pointers", .{});
19170 }19170 }
19171 } else if (inst_data.size == .many and elem_ty.zigTypeTag(zcu) == .@"opaque") {19171 } else if (inst_data.size != .one and elem_ty.zigTypeTag(zcu) == .@"opaque") {
19172 return sema.fail(block, elem_ty_src, "unknown-length pointer to opaque not allowed", .{});19172 return sema.fail(block, elem_ty_src, "indexable pointer to opaque type '{f}' not allowed", .{elem_ty.fmt(pt)});
19173 } else if (inst_data.size == .c) {19173 } else if (inst_data.size == .c) {
19174 if (!try sema.validateExternType(elem_ty, .other)) {19174 if (!try sema.validateExternType(elem_ty, .other)) {
19175 const msg = msg: {19175 const msg = msg: {
...@@ -19183,9 +19183,6 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -19183,9 +19183,6 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
19183 };19183 };
19184 return sema.failWithOwnedErrorMsg(block, msg);19184 return sema.failWithOwnedErrorMsg(block, msg);
19185 }19185 }
19186 if (elem_ty.zigTypeTag(zcu) == .@"opaque") {
19187 return sema.fail(block, elem_ty_src, "C pointers cannot point to opaque types", .{});
19188 }
19189 }19186 }
1919019187
19191 if (host_size != 0 and !try sema.validatePackedType(elem_ty)) {19188 if (host_size != 0 and !try sema.validatePackedType(elem_ty)) {
...@@ -20674,8 +20671,8 @@ fn zirReify(...@@ -20674,8 +20671,8 @@ fn zirReify(
20674 if (ptr_size != .one) {20671 if (ptr_size != .one) {
20675 return sema.fail(block, src, "function pointers must be single pointers", .{});20672 return sema.fail(block, src, "function pointers must be single pointers", .{});
20676 }20673 }
20677 } else if (ptr_size == .many and elem_ty.zigTypeTag(zcu) == .@"opaque") {20674 } else if (ptr_size != .one and elem_ty.zigTypeTag(zcu) == .@"opaque") {
20678 return sema.fail(block, src, "unknown-length pointer to opaque not allowed", .{});20675 return sema.fail(block, src, "indexable pointer to opaque type '{f}' not allowed", .{elem_ty.fmt(pt)});
20679 } else if (ptr_size == .c) {20676 } else if (ptr_size == .c) {
20680 if (!try sema.validateExternType(elem_ty, .other)) {20677 if (!try sema.validateExternType(elem_ty, .other)) {
20681 const msg = msg: {20678 const msg = msg: {
...@@ -20689,9 +20686,6 @@ fn zirReify(...@@ -20689,9 +20686,6 @@ fn zirReify(
20689 };20686 };
20690 return sema.failWithOwnedErrorMsg(block, msg);20687 return sema.failWithOwnedErrorMsg(block, msg);
20691 }20688 }
20692 if (elem_ty.zigTypeTag(zcu) == .@"opaque") {
20693 return sema.fail(block, src, "C pointers cannot point to opaque types", .{});
20694 }
20695 }20689 }
2069620690
20697 const ty = try pt.ptrTypeSema(.{20691 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 {...@@ -15,12 +15,6 @@ pub export fn entry3() void {
15 const ptr: *const anyopaque = x;15 const ptr: *const anyopaque = x;
16 _ = ptr;16 _ = ptr;
17}17}
18export fn entry4() void {
19 var a: []*u32 = undefined;
20 _ = &a;
21 var b: []anyopaque = undefined;
22 b = a;
23}
2418
25// error19// error
26//20//
...@@ -31,5 +25,3 @@ export fn entry4() void {...@@ -31,5 +25,3 @@ export fn entry4() void {
31// :11:12: note: parameter type declared here25// :11:12: note: parameter type declared here
32// :15:35: error: expected type '*const anyopaque', found '*?*usize'26// :15:35: error: expected type '*const anyopaque', found '*?*usize'
33// :15:35: note: cannot implicitly cast double pointer '*?*usize' to anyopaque pointer '*const anyopaque'27// :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 {...@@ -27,4 +27,4 @@ pub export fn entry2() void {
27//27//
28// :6:20: error: cannot @bitCast to '[]i32'28// :6:20: error: cannot @bitCast to '[]i32'
29// :6:20: note: use @ptrCast to cast from '[]u32'29// :6:20: note: use @ptrCast to cast from '[]u32'
30// :17:12: error: C pointers cannot point to opaque types30// :17:12: error: indexable pointer to opaque type 'anyopaque' not allowed