From cc6964c5dc7a4d4c3081db06697b8ba0d7900b85 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 23 Jul 2023 17:46:50 -0700 Subject: [PATCH] InternPool: add func_coerced handling to funcIesResolved --- src/InternPool.zig | 11 +++++++++++ test/behavior/generics.zig | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/InternPool.zig b/src/InternPool.zig index 0e9e1d4559..8a160d9e39 100644 --- a/src/InternPool.zig +++ b/src/InternPool.zig @@ -7069,6 +7069,17 @@ pub fn funcIesResolved(ip: *const InternPool, func_index: Index) *Index { const extra_index = switch (tags[@intFromEnum(func_index)]) { .func_decl => func_start + @typeInfo(Tag.FuncDecl).Struct.fields.len, .func_instance => func_start + @typeInfo(Tag.FuncInstance).Struct.fields.len, + .func_coerced => i: { + const uncoerced_func_index: Index = @enumFromInt(ip.extra.items[ + func_start + std.meta.fieldIndex(Tag.FuncCoerced, "func").? + ]); + const uncoerced_func_start = datas[@intFromEnum(uncoerced_func_index)]; + break :i switch (tags[@intFromEnum(uncoerced_func_index)]) { + .func_decl => uncoerced_func_start + @typeInfo(Tag.FuncDecl).Struct.fields.len, + .func_instance => uncoerced_func_start + @typeInfo(Tag.FuncInstance).Struct.fields.len, + else => unreachable, + }; + }, else => unreachable, }; return @ptrCast(&ip.extra.items[extra_index]); diff --git a/test/behavior/generics.zig b/test/behavior/generics.zig index a8a7d3c0ed..0954615223 100644 --- a/test/behavior/generics.zig +++ b/test/behavior/generics.zig @@ -456,3 +456,23 @@ test "return type of generic function is function pointer" { try expect(null == S.b(void)); } + +test "coerced function body has inequal value with its uncoerced body" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + + const S = struct { + const A = B(i32, c); + fn c() !i32 { + return 1234; + } + fn B(comptime T: type, comptime d: ?fn () anyerror!T) type { + return struct { + fn do() T { + return d.?() catch @panic("fail"); + } + }; + } + }; + try expect(S.A.do() == 1234); +}