Skip to content

Commit 6df78c3

Browse files
mluggandrewrk
authored andcommitted
Sema: mark pointers to inline functions as comptime-only
This is supposed to be the case, similar to how pointers to generic functions are comptime-only (several pieces of logic already assumed this). These types being considered runtime was causing `dbg_var_val` AIR instructions to be wrongly emitted for such values, causing codegen backends to create a runtime reference to the inline function, which (at least on the LLVM backend) triggers an error. Resolves: #38
1 parent 61b7077 commit 6df78c3

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

src/Sema.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -36506,7 +36506,7 @@ pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
3650636506
.ptr_type => |ptr_type| {
3650736507
const child_ty = ptr_type.child.toType();
3650836508
switch (child_ty.zigTypeTag(mod)) {
36509-
.Fn => return mod.typeToFunc(child_ty).?.is_generic,
36509+
.Fn => return !try sema.fnHasRuntimeBits(child_ty),
3651036510
.Opaque => return false,
3651136511
else => return sema.typeRequiresComptime(child_ty),
3651236512
}

src/type.zig

+2-4
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,10 @@ pub const Type = struct {
467467
.empty_struct_type => false,
468468
else => switch (ip.indexToKey(ty.toIntern())) {
469469
.int_type => |int_type| int_type.bits != 0,
470-
.ptr_type => |ptr_type| {
470+
.ptr_type => {
471471
// Pointers to zero-bit types still have a runtime address; however, pointers
472472
// to comptime-only types do not, with the exception of function pointers.
473473
if (ignore_comptime_only) return true;
474-
const child_ty = ptr_type.child.toType();
475-
if (child_ty.zigTypeTag(mod) == .Fn) return !mod.typeToFunc(child_ty).?.is_generic;
476474
if (strat == .sema) return !(try strat.sema.typeRequiresComptime(ty));
477475
return !comptimeOnly(ty, mod);
478476
},
@@ -2649,7 +2647,7 @@ pub const Type = struct {
26492647
.ptr_type => |ptr_type| {
26502648
const child_ty = ptr_type.child.toType();
26512649
switch (child_ty.zigTypeTag(mod)) {
2652-
.Fn => return mod.typeToFunc(child_ty).?.is_generic,
2650+
.Fn => return !child_ty.isFnOrHasRuntimeBits(mod),
26532651
.Opaque => return false,
26542652
else => return child_ty.comptimeOnly(mod),
26552653
}

test/behavior/call.zig

+10
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,13 @@ test "argument to generic function has correct result type" {
491491
try S.doTheTest();
492492
try comptime S.doTheTest();
493493
}
494+
495+
test "call inline fn through pointer" {
496+
const S = struct {
497+
inline fn foo(x: u8) !void {
498+
try expect(x == 123);
499+
}
500+
};
501+
const f = &S.foo;
502+
try f(123);
503+
}

0 commit comments

Comments
 (0)