Skip to content

refactor: 将 rtype 等类型重构为 internal/abi 中对应的类型#79

Merged
visualfc merged 2 commits into
mainfrom
fennoai/issue-78-1780908370
Jun 8, 2026
Merged

refactor: 将 rtype 等类型重构为 internal/abi 中对应的类型#79
visualfc merged 2 commits into
mainfrom
fennoai/issue-78-1780908370

Conversation

@fennoai

@fennoai fennoai Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

摘要

reflectx 包中本地定义的类型替换为 internal/abi 包中对应类型的别名,解决 issue #78

类型映射关系:

原类型 新类型(别名)
rtype abi.Type
uncommonType abi.UncommonType
method abi.Method
name abi.Name
imethod abi.Imethod
structField abi.StructField
arrayType abi.ArrayType
chanType abi.ChanType
funcType abi.FuncType
interfaceType abi.InterfaceType
ptrType abi.PtrType
sliceType abi.SliceType
structType abi.StructType
mapType abi.MapType
nameOff/typeOff/textOff abi.NameOff/TypeOff/TextOff
tflag abi.TFlag

主要变更:

  • 将上述类型改为 type alias(type rtype = abi.Type 等)
  • 更新所有字段访问以匹配 abi 的命名规范(如 sizeSize_kindKind_tflagTFlag 等)
  • 由于 Go 不允许在外部包的类型别名上定义方法,将原有方法调用转换为包级别函数(如 t.methods()rtypeMethods(t)
  • 更新 name.go 中的辅助函数以使用 abi.Name 的字段(n.Bytes 而非 n.bytes
  • map.go/map_go124.go 直接使用 abi.MapType(noswiss/swiss 分别对应不同的字段)

测试计划

  • go build ./... 成功
  • go test ./... 所有测试通过

Closes #78

🤖 Generated with FennoAI

Replace locally-defined rtype, uncommonType, method, name, imethod,
structField and compound types (arrayType, chanType, etc.) with type
aliases pointing to the corresponding types in internal/abi.

- type rtype = abi.Type
- type uncommonType = abi.UncommonType
- type method = abi.Method
- type name = abi.Name
- type imethod = abi.Imethod
- type structField = abi.StructField
- type arrayType = abi.ArrayType (and all other compound types)
- type nameOff/typeOff/textOff = abi.NameOff/TypeOff/TextOff
- type tflag = abi.TFlag
- type mapType = abi.MapType

Update all field accesses to match abi field naming (e.g. size→Size_,
kind→Kind_, tflag→TFlag, etc.) and convert method calls on alias types
to package-level functions.

Generated with [FennoAI](https://github.com/apps/fennoai)
Co-authored-by: visualfc <714279+visualfc@users.noreply.github.com>
@fennoai fennoai Bot mentioned this pull request Jun 8, 2026

@fennoai fennoai Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码审查

整体方向正确——将本地 ABI 类型副本替换为 internal/abi 类型别名,减少了维护负担。机械性的字段重命名看起来是正确的,测试也已通过。以下是发现的几个需要关注的问题。

主要问题:

  1. DumpType 的格式化字符串回归(见内联注释)
  2. 多个死代码函数:rtypeKindstructFieldOffsetstructFieldEmbeddednameIsExportednameEmbeddednamePkgPath(均有内联注释)
  3. rtype.go 中的 abi 导入保留 hack(见内联注释)
  4. tflagUserMethod = 1 << 7 使用了 TFlag uint8 中最后可用的位(位 0-5 已被 abi 占用,位 6 为 TFlagDirectIface),建议添加注释说明此 bit 的占用情况,以避免未来 Go 版本添加新的 TFlag 时发生冲突
  5. rtypeMethodX 中重复调用 funcTypeIn/funcTypeOut

View job run

Comment thread rtype.go
rt.nameOff(m.name).pkgPath(),
toType(rt.typeOff(m.mtyp)),
for _, m := range rtypeMethods(rt) {
fmt.Fprintf(w, "%v (%v)\t\t%#v\n",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Bug] DumpType 格式化参数丢失:格式字符串从 "%v %v (%v)\t\t%#v\n"(4 个参数:名称、pkgPath、类型、结构体)变为 "%v (%v)\t\t%#v\n"(3 个参数)。方法的包路径信息被静默丢弃。建议:在 rtype_nameOff(rt, m.Name).Name() 之后补充 namePkgPath(rtype_nameOff(rt, m.Name)) 作为第二个参数,并恢复原有格式字符串。

Comment thread rtype.go Outdated
}

// Ensure abi import is used
var _ = abi.TFlagUncommon

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[冗余代码] abi 导入保留 hackvar _ = abi.TFlagUncommon 仅用于防止编译器将 abi 包标记为未使用。实际上 rtype.go 中使用的所有类型和常量(uncommonTypetflagUncommon 等)均通过 type.go 中已定义的别名引用。建议直接删除 rtype.go 中的 abi 导入和这行 hack,因为这些别名已在 type.go 中声明(type.go 已正确导入 abi)。

Comment thread rtype.go
}
ft := (*funcType)(unsafe.Pointer(mtyp))
in := make([]reflect.Type, 0, 1+len(ft.in()))
in := make([]reflect.Type, 0, 1+len(funcTypeIn(ft)))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[性能] funcTypeIn/funcTypeOut 被重复调用funcTypeIn(ft) 在本行(用于 len)和第 442 行(用于 range)各调用一次;funcTypeOut(ft) 在第 445 行和第 446 行同样重复调用。每次调用都会执行 InSlice()/OutSlice() 的指针运算。建议赋值给本地变量后复用:

ins := funcTypeIn(ft)
in := make([]reflect.Type, 0, 1+len(ins))
in = append(in, toType(t))
for _, arg := range ins {
    in = append(in, toType(arg))
}

Comment thread type.go Outdated
func (t *rtype) Kind() reflect.Kind {
return reflect.Kind(t.kind & kindMask)
// rtypeKind returns reflect.Kind for an rtype, converting from abi.Kind.
func rtypeKind(t *rtype) reflect.Kind {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[死代码] rtypeKind 未被调用:此函数在整个包中没有任何调用点——所有需要 reflect.Kind 的地方均直接使用 reflect.Kind(t.Kind())。建议删除 rtypeKind 及相关的 kindMaskkindDirectIfacekindGCProg 常量(这些常量同样未被使用)。

Comment thread structfield.go Outdated

func (f *structField) offset() uintptr {
return f._offset
func structFieldOffset(f *structField) uintptr {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[死代码] structFieldOffsetstructFieldEmbedded 未被调用:这两个函数在包中没有任何调用点。调用方已直接使用 f.Offsetf.Name.IsEmbedded()(abi 类型别名暴露的导出字段/方法)。建议删除这两个函数;setEmbedded(第 13 行)有调用点,应保留。

Comment thread name.go Outdated
// go1.19
func (n name) embedded() bool {
return (*n.bytes)&(1<<3) != 0
func nameIsExported(n name) bool {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[死代码] nameIsExportednameEmbedded 未被调用:这两个函数在包中没有任何调用点。对应功能现在可直接通过 abi.Namen.IsExported()n.IsEmbedded() 方法访问。建议删除。

Comment thread name.go Outdated

func (n name) pkgPath() string {
if n.bytes == nil || *n.data(0, "name flag field")&(1<<2) == 0 {
func namePkgPath(n name) string {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[死代码 / 可疑回归] namePkgPath 定义但从未调用:此函数在重构后没有任何调用点,但 DumpType 原本调用了 rt.nameOff(m.name).pkgPath() 来打印方法的包路径(见 rtype.go 第 404 行的格式字符串回归)。这可能是一个有意为之的删除,也可能是遗漏。如果 DumpType 的包路径输出是有意省略的,那么 namePkgPath 本身也应一并删除。

@fennoai

fennoai Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

An internal error occurred. Please try again later.

View job run

@visualfc

visualfc commented Jun 8, 2026

Copy link
Copy Markdown
Member

@fennoai 删除未使用的代码

@fennoai

fennoai Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

删除未使用的代码

已删除以下未使用的代码,所有测试通过。

rtype.go

  • 删除 funcTypeFixed1/4/8/16/32/64/128(未使用的结构体类型)
  • 删除 bitVector(未使用的结构体类型)
  • 删除 rtypeIsVariadic(未使用的函数)
  • 移除冗余的 abi 包导入哨兵变量

structfield.go

  • 删除 structFieldOffset(未使用的函数)
  • 删除 structFieldEmbedded(未使用的函数)

name.go

  • 删除 nameIsExported(未使用的函数)
  • 删除 nameEmbedded(未使用的函数)
  • 删除 nameStrnameTagnamePkgPath(未使用的函数)

type.go

  • 删除 rtypeKind(未使用的函数)
  • 删除 kindDirectIfacekindGCProgkindMask(未使用的常量)

reflectx.go

  • 删除 fnv1(未使用的函数)

验证go build ./...go test ./... 均通过,共删除 124 行代码。

View job run

- 删除 rtype.go 中的 funcTypeFixed1/4/8/16/32/64/128、bitVector、rtypeIsVariadic
- 删除 structfield.go 中的 structFieldOffset、structFieldEmbedded
- 删除 name.go 中的 nameIsExported、nameEmbedded、nameStr、nameTag、namePkgPath
- 删除 type.go 中的 rtypeKind、kindDirectIface、kindGCProg、kindMask
- 删除 reflectx.go 中的 fnv1

Generated with [FennoAI](https://github.com/apps/fennoai)
Co-authored-by: visualfc <714279+visualfc@users.noreply.github.com>
@visualfc visualfc merged commit fad0325 into main Jun 8, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

重构

1 participant