authorgravatar for 61335294+fmaggi@users.noreply.github.comfmaggi <61335294+fmaggi@users.noreply.github.com> 2024-07-15 04:58:33-03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-07-15 10:58:33+03:00
log583e698256a2a26f26738c983e319d76926ef048
treefa3ac65a89e627f5c4b361282f8bf0f20dbc78d1
parentd404d8a3637bc30dffc736e5fa1a68b8af0e19cb
signaturebadge-check Signed by PGP key B5690EEEBB952194

Sema: disallow casting to opaque


2 files changed, 13 insertions(+), 8 deletions(-)

src/Sema.zig+6
...@@ -10188,9 +10188,15 @@ fn analyzeAs(...@@ -10188,9 +10188,15 @@ fn analyzeAs(
10188 const dest_ty_tag = dest_ty.zigTypeTagOrPoison(mod) catch |err| switch (err) {10188 const dest_ty_tag = dest_ty.zigTypeTagOrPoison(mod) catch |err| switch (err) {
10189 error.GenericPoison => return operand,10189 error.GenericPoison => return operand,
10190 };10190 };
10191
10192 if (dest_ty_tag == .Opaque) {
10193 return sema.fail(block, src, "cannot cast to opaque type '{}'", .{dest_ty.fmt(pt)});
10194 }
10195
10191 if (dest_ty_tag == .NoReturn) {10196 if (dest_ty_tag == .NoReturn) {
10192 return sema.fail(block, src, "cannot cast to noreturn", .{});10197 return sema.fail(block, src, "cannot cast to noreturn", .{});
10193 }10198 }
10199
10194 const is_ret = if (zir_dest_type.toIndex()) |ptr_index|10200 const is_ret = if (zir_dest_type.toIndex()) |ptr_index|
10195 sema.code.instructions.items(.tag)[@intFromEnum(ptr_index)] == .ret_type10201 sema.code.instructions.items(.tag)[@intFromEnum(ptr_index)] == .ret_type
10196 else10202 else
test/cases/compile_errors/directly_embedding_opaque_type_in_struct_and_union.zig+7-8
...@@ -15,14 +15,13 @@ export fn b() void {...@@ -15,14 +15,13 @@ export fn b() void {
15 _ = &bar;15 _ = &bar;
16}16}
17export fn c() void {17export fn c() void {
18 const baz = &@as(O, undefined);18 const baz = @as(O, undefined);
19 const qux = .{baz.*};19 _ = baz;
20 _ = qux;
21}20}
22export fn d() void {21export fn d() void {
23 const baz = &@as(O, undefined);22 const ptr: *O = @ptrFromInt(0x1000);
24 const qux = .{ .a = baz.* };23 const x = .{ptr.*};
25 _ = qux;24 _ = x;
26}25}
2726
28// error27// error
...@@ -33,7 +32,7 @@ export fn d() void {...@@ -33,7 +32,7 @@ export fn d() void {
33// :1:11: note: opaque declared here32// :1:11: note: opaque declared here
34// :7:10: error: opaque types have unknown size and therefore cannot be directly embedded in unions33// :7:10: error: opaque types have unknown size and therefore cannot be directly embedded in unions
35// :1:11: note: opaque declared here34// :1:11: note: opaque declared here
36// :19:22: error: cannot load opaque type 'tmp.O'35// :18:24: error: cannot cast to opaque type 'tmp.O'
37// :1:11: note: opaque declared here36// :1:11: note: opaque declared here
38// :24:28: error: cannot load opaque type 'tmp.O'37// :23:20: error: cannot load opaque type 'tmp.O'
39// :1:11: note: opaque declared here38// :1:11: note: opaque declared here