agiledragon / agiledragon/gomonkey

ApplyMethod possibly not work with generic types

未关闭
#104 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Go
星标
2.3k
派生
192
平均合并
1 天 6 小时
30 天内合并 PR
1

描述

这里定义了一个泛型的结构体,大致是这样:
```golang
type Table[T any] struct {
...
}

func (t *Table[T]) Add(network net.IPNet, value T) bool {
...
}
```
这里内容无关紧要,在使用gomonkey patch了叫Add的方法后,发现单元测试并未成功,调用的依旧是原本的Add。

进一步定位发现,reflect包获取到的方法名和实际的Add方法的signature并不相同,实际上方法地址也并不相同

reflect获取的方法名和地址:
```
0x1c56aa0
XXX.(*Table[string]).Add(SB)
:1 0x1c56aa0 493b6610 cmp rsp, qword ptr [r14+0x10]
:1 0x1c56aa4 0f86cd000000 jbe 0x1c56b77
```

实际调用的方法名和地址:
```
XXX.(*Table[go.shape.string_0]).Add(SB) .../cidrtable.go
=> cidrtable.go:29 0x1c56020 4c8da42458ffffff lea r12, ptr [rsp+0xffffff58]
cidrtable.go:29 0x1c56028 4d3b6610 cmp r12, qword ptr [r14+0x10]
```

gomonkey是通过取reflect记录的地址来修改实际调用的,但是看来这个方式对于泛型方法暂且不适用

golang版本1.18.2和1.18.5均存在上述问题

贡献指南

这个仓库没有索引到贡献指南

调研方向

Reproduce the generic Table[T].Add case on Go 1.18.2 and 1.18.5, comparing the reflect-reported method with the call at cidrtable.go:29. Start by tracing gomonkey's reflect-based patch entry point and verify whether the generated and shape-specific method addresses differ. Done means the unit test calls the patched method for a generic type.

由索引模型根据 Issue 内容生成。

评估

技术栈
go
领域
devtools, testing
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。