internal/lib/reflect: TypeOf check closure

This commit is contained in:
visualfc
2024-11-04 10:07:40 +08:00
parent 85a736d49b
commit 7bbd3a7e36
3 changed files with 454 additions and 0 deletions

View File

@@ -1007,6 +1007,12 @@ func (t *structType) FieldByName(name string) (f StructField, present bool) {
// If i is a nil interface value, TypeOf returns nil.
func TypeOf(i any) Type {
eface := *(*emptyInterface)(unsafe.Pointer(&i))
// closure type
if eface.typ.TFlag&abi.TFlagClosure != 0 {
ft := *eface.typ.StructType().Fields[0].Typ.FuncType()
ft.In = ft.In[1:]
return toType(&ft.Type)
}
// Noescape so this doesn't make i to escape. See the comment
// at Value.typ for why this is safe.
return toType((*abi.Type)(unsafe.Pointer(eface.typ)))