authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-15 11:08:07+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-15 11:09:04+00:00
log72ba7f7e98b8cd00787e2903641e752e15264522
tree7ad2a593be813469b9ac72f8542acb00625d6273
parentb5d3db5fc6104f9af8ab7d3f5cf05a5d4f30f721
signaturelock-open Commit is signed but in an unrecognized format.

Sema: disallow runtime stores to pointers with comptime-only element types


2 files changed, 58 insertions(+), 1 deletions(-)

src/Sema.zig+16-1
...@@ -3628,6 +3628,10 @@ fn zirAllocExtended(...@@ -3628,6 +3628,10 @@ fn zirAllocExtended(
3628 }3628 }
3629 }3629 }
36303630
3631 if (small.has_type and try var_ty.comptimeOnlySema(pt)) {
3632 return sema.analyzeComptimeAlloc(block, var_ty, alignment);
3633 }
3634
3631 if (small.has_type) {3635 if (small.has_type) {
3632 if (!small.is_const) {3636 if (!small.is_const) {
3633 try sema.validateVarType(block, ty_src, var_ty, false);3637 try sema.validateVarType(block, ty_src, var_ty, false);
...@@ -4075,7 +4079,7 @@ fn zirAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I...@@ -4075,7 +4079,7 @@ fn zirAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
4075 const ty_src = block.src(.{ .node_offset_var_decl_ty = inst_data.src_node });4079 const ty_src = block.src(.{ .node_offset_var_decl_ty = inst_data.src_node });
40764080
4077 const var_ty = try sema.resolveType(block, ty_src, inst_data.operand);4081 const var_ty = try sema.resolveType(block, ty_src, inst_data.operand);
4078 if (block.is_comptime) {4082 if (block.is_comptime or try var_ty.comptimeOnlySema(pt)) {
4079 return sema.analyzeComptimeAlloc(block, var_ty, .none);4083 return sema.analyzeComptimeAlloc(block, var_ty, .none);
4080 }4084 }
4081 if (sema.func_is_naked and try var_ty.hasRuntimeBitsSema(pt)) {4085 if (sema.func_is_naked and try var_ty.hasRuntimeBitsSema(pt)) {
...@@ -31938,6 +31942,17 @@ fn storePtr2(...@@ -31938,6 +31942,17 @@ fn storePtr2(
31938 } else break :rs ptr_src;31942 } else break :rs ptr_src;
31939 } else ptr_src;31943 } else ptr_src;
3194031944
31945 // We're performing the store at runtime; as such, we need to make sure the pointee type
31946 // is not comptime-only. We can hit this case with a `@ptrFromInt` pointer.
31947 if (try elem_ty.comptimeOnlySema(pt)) {
31948 return sema.failWithOwnedErrorMsg(block, msg: {
31949 const msg = try sema.errMsg(src, "cannot store comptime-only type '{}' at runtime", .{elem_ty.fmt(pt)});
31950 errdefer msg.destroy(sema.gpa);
31951 try sema.errNote(ptr_src, msg, "operation is runtime due to this pointer", .{});
31952 break :msg msg;
31953 });
31954 }
31955
31941 // We do this after the possible comptime store above, for the case of field_ptr stores31956 // We do this after the possible comptime store above, for the case of field_ptr stores
31942 // to unions because we want the comptime tag to be set, even if the field type is void.31957 // to unions because we want the comptime tag to be set, even if the field type is void.
31943 if ((try sema.typeHasOnePossibleValue(elem_ty)) != null) {31958 if ((try sema.typeHasOnePossibleValue(elem_ty)) != null) {
test/cases/compile_errors/store_comptime_only_type_to_runtime_pointer.zig created+42
...@@ -0,0 +1,42 @@
1export fn a() void {
2 const p: *fn () void = @ptrFromInt(4);
3 p.* = undefined;
4}
5
6export fn b(p: *anyopaque) void {
7 p.* = undefined;
8}
9
10export fn c(p: *anyopaque, q: *anyopaque) void {
11 p.* = q.*;
12}
13
14const Opaque = opaque {};
15export fn d(p: *Opaque) void {
16 p.* = undefined;
17}
18
19export fn e() void {
20 const p: *comptime_int = @ptrFromInt(16);
21 p.* = undefined;
22}
23
24export fn f() void {
25 const p: **comptime_int = @ptrFromInt(16); // double pointer ('*comptime_int' is comptime-only)
26 p.* = undefined;
27}
28
29// error
30//
31// :3:9: error: cannot store comptime-only type 'fn () void' at runtime
32// :3:6: note: operation is runtime due to this pointer
33// :7:11: error: expected type 'anyopaque', found '@TypeOf(undefined)'
34// :7:11: note: cannot coerce to 'anyopaque'
35// :11:12: error: cannot load opaque type 'anyopaque'
36// :16:11: error: expected type 'tmp.Opaque', found '@TypeOf(undefined)'
37// :16:11: note: cannot coerce to 'tmp.Opaque'
38// :14:16: note: opaque declared here
39// :21:9: error: cannot store comptime-only type 'comptime_int' at runtime
40// :21:6: note: operation is runtime due to this pointer
41// :26:9: error: cannot store comptime-only type '*comptime_int' at runtime
42// :26:6: note: operation is runtime due to this pointer