authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-16 00:31:09+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-16 01:12:54+02:00
logfe6249348f615c975a2db53bb0c93f83bd2a281b
treee059a560361900c2d6e0d226be20cebc9ea0c74e
parentfb09093d95f2dc9bf454ba0d1d489316311adffc

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

2 files changed, 22 insertions(+), 0 deletions(-)

src/Sema.zig+7
...@@ -27404,6 +27404,13 @@ fn analyzeRef(...@@ -27404,6 +27404,13 @@ fn analyzeRef(
27404 const operand_ty = sema.typeOf(operand);27404 const operand_ty = sema.typeOf(operand);
2740527405
27406 if (try sema.resolveMaybeUndefVal(operand)) |val| {27406 if (try sema.resolveMaybeUndefVal(operand)) |val| {
27407 switch (val.tag()) {
27408 .extern_fn, .function => {
27409 const decl_index = val.pointerDecl().?;
27410 return sema.analyzeDeclRef(decl_index);
27411 },
27412 else => {},
27413 }
27407 var anon_decl = try block.startAnonDecl();27414 var anon_decl = try block.startAnonDecl();
27408 defer anon_decl.deinit();27415 defer anon_decl.deinit();
27409 return sema.analyzeDeclRef(try anon_decl.finish(27416 return sema.analyzeDeclRef(try anon_decl.finish(
test/behavior/eval.zig+15
...@@ -1507,3 +1507,18 @@ test "inline call in @TypeOf inherits is_inline property" {...@@ -1507,3 +1507,18 @@ test "inline call in @TypeOf inherits is_inline property" {
1507 };1507 };
1508 try expect(S.T == void);1508 try expect(S.T == void);
1509}1509}
1510
1511test "comptime function turns function value to function pointer" {
1512 const S = struct {
1513 fn fnPtr(function: anytype) *const @TypeOf(function) {
1514 return &function;
1515 }
1516 fn Nil() u8 {
1517 return 0;
1518 }
1519 const foo = &[_]*const fn () u8{
1520 fnPtr(Nil),
1521 };
1522 };
1523 comptime try expect(S.foo[0] == &S.Nil);
1524}