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

Closes #20083
This commit is contained in:
Veikka Tuominen 2024-05-27 13:24:43 +03:00
parent 2cf8e73781
commit 17a0458e53
2 changed files with 25 additions and 0 deletions

View File

@ -22722,7 +22722,16 @@ fn zirPtrFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
.storage = .{ .elems = new_elems },
} }));
}
if (try sema.typeRequiresComptime(ptr_ty)) {
return sema.failWithOwnedErrorMsg(block, msg: {
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)});
errdefer msg.destroy(sema.gpa);
const src_decl = mod.declPtr(block.src_decl);
try sema.explainWhyTypeIsComptime(msg, src_decl.toSrcLoc(src, mod), ptr_ty);
break :msg msg;
});
}
try sema.requireRuntimeBlock(block, src, operand_src);
if (!is_vector) {
if (block.wantSafety() and (try sema.typeHasRuntimeBits(elem_ty) or elem_ty.zigTypeTag(mod) == .Fn)) {

View File

@ -0,0 +1,16 @@
const GuSettings = struct {
fin: ?fn (c_int) callconv(.C) void,
};
pub export fn callbackFin(id: c_int, arg: ?*anyopaque) void {
const settings: ?*GuSettings = @as(?*GuSettings, @ptrFromInt(@intFromPtr(arg)));
if (settings.?.fin != null) {
settings.?.fin.?(id & 0xffff);
}
}
// error
// target=native
//
// :5:54: error: pointer to comptime-only type '?*tmp.GuSettings' must be comptime-known, but operand is runtime-known
// :2:10: note: struct requires comptime because of this field
// :2:10: note: use '*const fn (c_int) callconv(.C) void' for a function pointer type