From 3031d813874f6d6ad9ae4b793e3af6cdf632fa66 Mon Sep 17 00:00:00 2001 From: mlugg Date: Wed, 5 Feb 2025 21:26:04 +0000 Subject: [PATCH] Sema: fix `@typeInfo` of function with generic return type and IES Resolves: #20088 --- src/Sema.zig | 14 ++++++++++---- test/behavior/type_info.zig | 9 +++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 32f611ce3e..5f92e12b04 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -18145,10 +18145,16 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai const ret_ty_opt = try pt.intern(.{ .opt = .{ .ty = try pt.intern(.{ .opt_type = .type_type }), - .val = if (func_ty_info.return_type == .generic_poison_type) - .none - else - func_ty_info.return_type, + .val = opt_val: { + const ret_ty: Type = .fromInterned(func_ty_info.return_type); + if (ret_ty.toIntern() == .generic_poison_type) break :opt_val .none; + if (ret_ty.zigTypeTag(zcu) == .error_union) { + if (ret_ty.errorUnionPayload(zcu).toIntern() == .generic_poison_type) { + break :opt_val .none; + } + } + break :opt_val ret_ty.toIntern(); + }, } }); const callconv_ty = try sema.getBuiltinType(src, .CallingConvention); diff --git a/test/behavior/type_info.zig b/test/behavior/type_info.zig index 2b7a66c763..86e2eccc4c 100644 --- a/test/behavior/type_info.zig +++ b/test/behavior/type_info.zig @@ -675,3 +675,12 @@ test "@typeInfo only contains pub decls" { try std.testing.expectEqualStrings("Enum", decls[0].name); try std.testing.expectEqualStrings("Struct", decls[1].name); } + +test "@typeInfo function with generic return type and inferred error set" { + const S = struct { + fn testFn(comptime T: type) !T {} + }; + + const ret_ty = @typeInfo(@TypeOf(S.testFn)).@"fn".return_type; + comptime assert(ret_ty == null); +}