Commit 3031d81387

mlugg <mlugg@mlugg.co.uk>
2025-02-05 22:26:04
Sema: fix `@typeInfo` of function with generic return type and IES
Resolves: #20088
1 parent 5317d88
Changed files (2)
src
test
behavior
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);
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);
+}