Sema: emit error when pointer to extern function is called

This commit is contained in:
Bogdan Romanyuk 2023-11-06 18:26:28 +03:00 committed by GitHub
parent e74ced21b7
commit 62e67a2b56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

@ -7253,7 +7253,15 @@ fn analyzeCall(
}),
.func => func_val.toIntern(),
.ptr => |ptr| switch (ptr.addr) {
.decl => |decl| mod.declPtr(decl).val.toIntern(),
.decl => |decl| blk: {
const func_val_ptr = mod.declPtr(decl).val.toIntern();
const intern_index = mod.intern_pool.indexToKey(func_val_ptr);
if (intern_index == .extern_func or (intern_index == .variable and intern_index.variable.is_extern))
return sema.fail(block, call_src, "{s} call of extern function pointer", .{
@as([]const u8, if (is_comptime_call) "comptime" else "inline"),
});
break :blk func_val_ptr;
},
else => {
assert(callee_ty.isPtrAtRuntime(mod));
return sema.fail(block, call_src, "{s} call of function pointer", .{

View File

@ -0,0 +1,16 @@
const x = @extern(*const fn() callconv(.C) void, .{ .name = "foo" });
pub fn entry0() void {
comptime x();
}
pub fn entry1() void {
@call(.always_inline, x, .{});
}
// error
// backend=stage2
// target=native
//
// :4:15: error: comptime call of extern function pointer
// :8:5: error: inline call of extern function pointer