From 9ffee5abed1e57ceb24e0c8e20aa2fd8c242ca38 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Thu, 24 Oct 2024 13:34:07 +0300 Subject: [PATCH] Sema: fix check for whether current AnalUnit is a test function Closes #21159 --- src/Sema.zig | 7 ++++--- ...rror_in_generic_fn_called_from_non_fn_scope.zig | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 test/cases/compile_errors/ret_coercion_error_in_generic_fn_called_from_non_fn_scope.zig diff --git a/src/Sema.zig b/src/Sema.zig index 63af9686a0d3..9912a610ff6d 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -29836,6 +29836,7 @@ fn coerceExtra( if (dest_ty.isGenericPoison()) return inst; const pt = sema.pt; const zcu = pt.zcu; + const ip = &zcu.intern_pool; const dest_ty_src = inst_src; // TODO better source location try dest_ty.resolveFields(pt); const inst_ty = sema.typeOf(inst); @@ -30458,7 +30459,7 @@ fn coerceExtra( errdefer msg.destroy(sema.gpa); const ret_ty_src: LazySrcLoc = .{ - .base_node_inst = sema.getOwnerFuncDeclInst(), + .base_node_inst = ip.getNav(zcu.funcInfo(sema.func_index).owner_nav).srcInst(ip), .offset = .{ .node_offset_fn_type_ret_ty = 0 }, }; try sema.errNote(ret_ty_src, msg, "'noreturn' declared here", .{}); @@ -30496,10 +30497,10 @@ fn coerceExtra( // Add notes about function return type if (opts.is_ret and - !zcu.test_functions.contains(zcu.funcInfo(sema.owner.unwrap().func).owner_nav)) + !zcu.test_functions.contains(zcu.funcInfo(sema.func_index).owner_nav)) { const ret_ty_src: LazySrcLoc = .{ - .base_node_inst = sema.getOwnerFuncDeclInst(), + .base_node_inst = ip.getNav(zcu.funcInfo(sema.func_index).owner_nav).srcInst(ip), .offset = .{ .node_offset_fn_type_ret_ty = 0 }, }; if (inst_ty.isError(zcu) and !dest_ty.isError(zcu)) { diff --git a/test/cases/compile_errors/ret_coercion_error_in_generic_fn_called_from_non_fn_scope.zig b/test/cases/compile_errors/ret_coercion_error_in_generic_fn_called_from_non_fn_scope.zig new file mode 100644 index 000000000000..ba17172b42b7 --- /dev/null +++ b/test/cases/compile_errors/ret_coercion_error_in_generic_fn_called_from_non_fn_scope.zig @@ -0,0 +1,14 @@ +fn foo() fn () void { + return struct {}; +} +comptime { + _ = foo(); +} + +// error +// backend=stage2 +// target=native +// +// :2:12: error: expected type 'fn () void', found 'type' +// :1:10: note: function return type declared here +// :5:12: note: called from here