Skip to content

Commit d42f4ab

Browse files
committed
llvm: correctly lower references to generic functions
Closes #13522
1 parent e01ec96 commit d42f4ab

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/codegen/llvm.zig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3198,7 +3198,8 @@ pub const DeclGen = struct {
31983198
/// There are other similar cases handled here as well.
31993199
fn lowerPtrElemTy(dg: *DeclGen, elem_ty: Type) Allocator.Error!*llvm.Type {
32003200
const lower_elem_ty = switch (elem_ty.zigTypeTag()) {
3201-
.Opaque, .Fn => true,
3201+
.Opaque => true,
3202+
.Fn => !elem_ty.fnInfo().is_generic,
32023203
.Array => elem_ty.childType().hasRuntimeBitsIgnoreComptime(),
32033204
else => elem_ty.hasRuntimeBitsIgnoreComptime(),
32043205
};
@@ -4145,7 +4146,9 @@ pub const DeclGen = struct {
41454146
}
41464147

41474148
const is_fn_body = decl.ty.zigTypeTag() == .Fn;
4148-
if (!is_fn_body and !decl.ty.hasRuntimeBits()) {
4149+
if ((!is_fn_body and !decl.ty.hasRuntimeBits()) or
4150+
(is_fn_body and decl.ty.fnInfo().is_generic))
4151+
{
41494152
return self.lowerPtrToVoid(tv.ty);
41504153
}
41514154

test/behavior/pointers.zig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,3 +489,20 @@ test "ptrCast comptime known slice to C pointer" {
489489
var p = @ptrCast([*c]const u8, s);
490490
try std.testing.expectEqualStrings(s, std.mem.sliceTo(p, 0));
491491
}
492+
493+
test "ptrToInt on a generic function" {
494+
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
495+
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
496+
if (builtin.zig_backend == .stage2_aarch64 and builtin.os.tag != .linux) return error.SkipZigTest; // TODO
497+
if (builtin.zig_backend == .stage2_x86_64 and builtin.os.tag != .linux) return error.SkipZigTest; // TODO
498+
499+
const S = struct {
500+
fn generic(i: anytype) @TypeOf(i) {
501+
return i;
502+
}
503+
fn doTheTest(a: anytype) !void {
504+
try expect(@ptrToInt(a) != 0);
505+
}
506+
};
507+
try S.doTheTest(&S.generic);
508+
}

0 commit comments

Comments
 (0)