clean up uses of encoding/gob and go/ssa

encoding/gob.Decoder already has "map copy" semantics;
any decoded map keys get inserted, and other existing map keys are left.
This is exactly the same behavior that the new code did with maps.Copy,
so the new code is unnecessary.

If there is any bug with this code, it needs to be shown via a test.
As far as I can tell, there is no bug, because no two packages
should clash in terms of map keys.

Add a helper to obtain a go/ssa.Function's origin if there is one,
falling back to itself if there is none.
This mimics how go/types.Func.Origin works,
and lets us simplify quite a bit of code.

Finally, fieldToStruct uses origin objects only,
so don't do a double lookup in reverse.go.

While here, fix up the func mentions in two type aliases.
This commit is contained in:
Daniel Martí
2026-03-01 23:09:49 +00:00
committed by Paul
parent cb98b4daab
commit 5c2470f075
3 changed files with 13 additions and 31 deletions
+4 -14
View File
@@ -20,8 +20,8 @@ import (
)
type (
funcFullName = string // as per go/types.Func.FullName
objectString = string // as per recordedObjectString
funcFullName = string // the result of [types.Func.FullName] plus [stripTypeArgs]
objectString = string // the result of [reflectInspector.obfuscatedObjectName]
)
// pkgCache contains information about a package that will be stored in fsCache.
@@ -45,17 +45,6 @@ func (c *pkgCache) CopyFrom(c2 pkgCache) {
maps.Copy(c.ReflectObjectNames, c2.ReflectObjectNames)
}
func decodePkgCache(r io.Reader, dst *pkgCache) error {
// Decode into a fresh value first: gob merges into non-nil maps, so decoding
// directly into dst can leave stale entries in nested maps.
var decoded pkgCache
if err := gob.NewDecoder(r).Decode(&decoded); err != nil {
return fmt.Errorf("gob decode: %w", err)
}
dst.CopyFrom(decoded)
return nil
}
func ssaBuildPkg(pkg *types.Package, files []*ast.File, info *types.Info) *ssa.Package {
// Create SSA packages for all imports. Order is not significant.
ssaProg := ssa.NewProgram(fset, 0)
@@ -190,7 +179,8 @@ func computePkgCache(fsCache *cache.Cache, lpkg *listedPackage, pkg *types.Packa
return err
}
defer f.Close()
if err := decodePkgCache(f, &computed); err != nil {
// The gob decoder
if err := gob.NewDecoder(f).Decode(&computed); err != nil {
return err
}
return nil
+9 -14
View File
@@ -235,11 +235,7 @@ func (ri *reflectInspector) checkFunction(fun *ssa.Function) {
// fun.WriteTo(os.Stdout)
// }
originFun := fun.Origin()
if originFun == nil {
originFun = fun
}
f, _ := originFun.Object().(*types.Func)
f, _ := ssaFuncOrigin(fun).Object().(*types.Func)
var funcName string
genericFunc := false
if f != nil {
@@ -281,16 +277,8 @@ func (ri *reflectInspector) checkFunction(fun *ssa.Function) {
case *ssa.Call:
callName := ""
if callee := inst.Call.StaticCallee(); callee != nil {
if obj, ok := callee.Object().(*types.Func); ok && obj != nil {
if obj, ok := ssaFuncOrigin(callee).Object().(*types.Func); ok && obj != nil {
callName = obj.FullName()
} else {
originCallee := callee.Origin()
if originCallee == nil {
originCallee = callee
}
if obj, ok := originCallee.Object().(*types.Func); ok && obj != nil {
callName = obj.FullName()
}
}
}
if callName == "" && inst.Call.Method != nil {
@@ -648,3 +636,10 @@ func stripTypeArgs(name string) (string, bool) {
}
return b.String(), true
}
func ssaFuncOrigin(fn *ssa.Function) *ssa.Function {
if orig := fn.Origin(); orig != nil {
return orig
}
return fn
}
-3
View File
@@ -105,9 +105,6 @@ One can reverse a captured panic stack trace as follows:
}
originObj := obj.Origin()
strct := fieldToStruct[originObj]
if strct == nil {
strct = fieldToStruct[obj]
}
if strct == nil {
panic("could not find struct for field " + name.Name)
}