Consider the following program:
package p
func f[T any]() func() {
return func() {}
}
func g() func() {
return func() {}
}
var x = f[int]
var y = g
One expects f and g to be identical, because they have identical bodies. However, because f is generic, the type dictionary for its type arguments is captured, meaning that f's closure escapes to the heap.
TEXT .g(SB)
LEAQ .g.func1·f(SB), AX
RET
TEXT .f[int](SB)
PUSHQ BP
MOVQ SP, BP
SUBQ $16, SP
NOP
LEAQ type:noalg.struct { F uintptr; X0 *[0]uintptr }(SB), AX
CALL runtime.newobject(SB)
LEAQ .f[int].f[go.shape.int].func1(SB), CX
MOVQ CX, (AX)
LEAQ ..dict.f[int](SB), CX
MOVQ CX, 8(AX)
ADDQ $16, SP
POPQ BP
RET
Consider the following program:
One expects f and g to be identical, because they have identical bodies. However, because f is generic, the type dictionary for its type arguments is captured, meaning that f's closure escapes to the heap.