From fe6249348f615c975a2db53bb0c93f83bd2a281b Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Wed, 16 Nov 2022 00:31:09 +0200 Subject: Sema: ensure comptime reference to function points to original decl This prevents sema from creating new decls for the functions and passing them to the backends as non-function decls. Closes #12501 --- src/Sema.zig | 7 +++++++ test/behavior/eval.zig | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Sema.zig b/src/Sema.zig index 4b37fc603a..454722728b 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -27404,6 +27404,13 @@ fn analyzeRef( const operand_ty = sema.typeOf(operand); if (try sema.resolveMaybeUndefVal(operand)) |val| { + switch (val.tag()) { + .extern_fn, .function => { + const decl_index = val.pointerDecl().?; + return sema.analyzeDeclRef(decl_index); + }, + else => {}, + } var anon_decl = try block.startAnonDecl(); defer anon_decl.deinit(); return sema.analyzeDeclRef(try anon_decl.finish( diff --git a/test/behavior/eval.zig b/test/behavior/eval.zig index cd0891990c..145be9dede 100644 --- a/test/behavior/eval.zig +++ b/test/behavior/eval.zig @@ -1507,3 +1507,18 @@ test "inline call in @TypeOf inherits is_inline property" { }; try expect(S.T == void); } + +test "comptime function turns function value to function pointer" { + const S = struct { + fn fnPtr(function: anytype) *const @TypeOf(function) { + return &function; + } + fn Nil() u8 { + return 0; + } + const foo = &[_]*const fn () u8{ + fnPtr(Nil), + }; + }; + comptime try expect(S.foo[0] == &S.Nil); +} -- cgit v1.2.3