Skip to content

Commit 92f6489

Browse files
xdBronchmlugg
authored andcommitted
sema: disallow slices of opaque types
1 parent 19af9fa commit 92f6489

File tree

7 files changed

+60
-43
lines changed

7 files changed

+60
-43
lines changed

src/Sema.zig

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/cases/compile_errors/double_pointer_to_anyopaque_pointer.zig

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ pub export fn entry3() void {
1515
const ptr: *const anyopaque = x;
1616
_ = ptr;
1717
}
18-
export 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'
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
export fn a() void {
2+
_ = []anyopaque;
3+
}
4+
export fn b() void {
5+
_ = [*]anyopaque;
6+
}
7+
export fn c() void {
8+
_ = [*c]anyopaque;
9+
}
10+
11+
export 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+
}
23+
export 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+
}
35+
export 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

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/cases/compile_errors/unknown_length_pointer_to_opaque.zig

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/cases/error_in_nested_declaration.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)