authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-23 17:46:50-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-23 17:47:18-07:00
logcc6964c5dc7a4d4c3081db06697b8ba0d7900b85
tree0399e6a434b93734a329452923927f014d9dc99f
parenta7f3c2eab427397846f619dba7abb72378ac2ce9

InternPool: add func_coerced handling to funcIesResolved


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

src/InternPool.zig+11
......@@ -7069,6 +7069,17 @@ pub fn funcIesResolved(ip: *const InternPool, func_index: Index) *Index {
70697069 const extra_index = switch (tags[@intFromEnum(func_index)]) {
70707070 .func_decl => func_start + @typeInfo(Tag.FuncDecl).Struct.fields.len,
70717071 .func_instance => func_start + @typeInfo(Tag.FuncInstance).Struct.fields.len,
7072 .func_coerced => i: {
7073 const uncoerced_func_index: Index = @enumFromInt(ip.extra.items[
7074 func_start + std.meta.fieldIndex(Tag.FuncCoerced, "func").?
7075 ]);
7076 const uncoerced_func_start = datas[@intFromEnum(uncoerced_func_index)];
7077 break :i switch (tags[@intFromEnum(uncoerced_func_index)]) {
7078 .func_decl => uncoerced_func_start + @typeInfo(Tag.FuncDecl).Struct.fields.len,
7079 .func_instance => uncoerced_func_start + @typeInfo(Tag.FuncInstance).Struct.fields.len,
7080 else => unreachable,
7081 };
7082 },
70727083 else => unreachable,
70737084 };
70747085 return @ptrCast(&ip.extra.items[extra_index]);
test/behavior/generics.zig+20
......@@ -456,3 +456,23 @@ test "return type of generic function is function pointer" {
456456
457457 try expect(null == S.b(void));
458458}
459
460test "coerced function body has inequal value with its uncoerced body" {
461 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
462 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
463
464 const S = struct {
465 const A = B(i32, c);
466 fn c() !i32 {
467 return 1234;
468 }
469 fn B(comptime T: type, comptime d: ?fn () anyerror!T) type {
470 return struct {
471 fn do() T {
472 return d.?() catch @panic("fail");
473 }
474 };
475 }
476 };
477 try expect(S.A.do() == 1234);
478}