authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-05-27 13:24:43+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-06-02 21:42:13+03:00
log17a0458e530242254f21fc0b6825e1303ec06028
treebc7ab6012b943d6f6435351555ed0f6ee3d3429b
parent2cf8e73781b81f38c50a40b42cad49242ef7c98b

Sema: add missing error for runtime `@ptrFromInt` to comptime-only type

Closes #20083

2 files changed, 25 insertions(+), 0 deletions(-)

src/Sema.zig+9
...@@ -22722,7 +22722,16 @@ fn zirPtrFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!...@@ -22722,7 +22722,16 @@ fn zirPtrFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
22722 .storage = .{ .elems = new_elems },22722 .storage = .{ .elems = new_elems },
22723 } }));22723 } }));
22724 }22724 }
22725 if (try sema.typeRequiresComptime(ptr_ty)) {
22726 return sema.failWithOwnedErrorMsg(block, msg: {
22727 const msg = try sema.errMsg(block, src, "pointer to comptime-only type '{}' must be comptime-known, but operand is runtime-known", .{ptr_ty.fmt(mod)});
22728 errdefer msg.destroy(sema.gpa);
2272522729
22730 const src_decl = mod.declPtr(block.src_decl);
22731 try sema.explainWhyTypeIsComptime(msg, src_decl.toSrcLoc(src, mod), ptr_ty);
22732 break :msg msg;
22733 });
22734 }
22726 try sema.requireRuntimeBlock(block, src, operand_src);22735 try sema.requireRuntimeBlock(block, src, operand_src);
22727 if (!is_vector) {22736 if (!is_vector) {
22728 if (block.wantSafety() and (try sema.typeHasRuntimeBits(elem_ty) or elem_ty.zigTypeTag(mod) == .Fn)) {22737 if (block.wantSafety() and (try sema.typeHasRuntimeBits(elem_ty) or elem_ty.zigTypeTag(mod) == .Fn)) {
test/cases/compile_errors/runtime_@ptrFromInt_to_comptime_only_type.zig created+16
...@@ -0,0 +1,16 @@
1const GuSettings = struct {
2 fin: ?fn (c_int) callconv(.C) void,
3};
4pub export fn callbackFin(id: c_int, arg: ?*anyopaque) void {
5 const settings: ?*GuSettings = @as(?*GuSettings, @ptrFromInt(@intFromPtr(arg)));
6 if (settings.?.fin != null) {
7 settings.?.fin.?(id & 0xffff);
8 }
9}
10
11// error
12// target=native
13//
14// :5:54: error: pointer to comptime-only type '?*tmp.GuSettings' must be comptime-known, but operand is runtime-known
15// :2:10: note: struct requires comptime because of this field
16// :2:10: note: use '*const fn (c_int) callconv(.C) void' for a function pointer type