Skip to content

Commit

Permalink
go/callgraph/rta: audit for types.Alias safety
Browse files Browse the repository at this point in the history
Updates golang/go#65294

Change-Id: I90fe0cc3ab5a6171e112e2eb0e452efb172d9980
Reviewed-on: https://go-review.googlesource.com/c/tools/+/559937
Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
adonovan committed Feb 6, 2024
1 parent 08393e0 commit a297bfd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 8 additions & 1 deletion go/callgraph/rta/rta.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/types/typeutil"
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/compat"
)

Expand Down Expand Up @@ -416,6 +417,9 @@ func (r *rta) implementations(I *types.Interface) []types.Type {
// dynamic type of some interface or reflect.Value.
// Adapted from needMethods in go/ssa/builder.go
func (r *rta) addRuntimeType(T types.Type, skip bool) {
// Never record aliases.
T = aliases.Unalias(T)

if prev, ok := r.result.RuntimeTypes.At(T).(bool); ok {
if skip && !prev {
r.result.RuntimeTypes.Set(T, skip)
Expand Down Expand Up @@ -457,7 +461,7 @@ func (r *rta) addRuntimeType(T types.Type, skip bool) {
case *types.Named:
n = T
case *types.Pointer:
n, _ = T.Elem().(*types.Named)
n, _ = aliases.Unalias(T.Elem()).(*types.Named)
}
if n != nil {
owner := n.Obj().Pkg()
Expand All @@ -476,6 +480,9 @@ func (r *rta) addRuntimeType(T types.Type, skip bool) {
}

switch t := T.(type) {
case *aliases.Alias:
panic("unreachable")

case *types.Basic:
// nop

Expand Down
3 changes: 2 additions & 1 deletion go/callgraph/rta/rta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
"golang.org/x/tools/internal/aliases"
)

// TestRTA runs RTA on each testdata/*.go file and compares the
Expand Down Expand Up @@ -200,7 +201,7 @@ func check(t *testing.T, f *ast.File, pkg *ssa.Package, res *rta.Result) {
got := make(stringset)
res.RuntimeTypes.Iterate(func(key types.Type, value interface{}) {
if !value.(bool) { // accessible to reflection
typ := types.TypeString(key, types.RelativeTo(pkg.Pkg))
typ := types.TypeString(aliases.Unalias(key), types.RelativeTo(pkg.Pkg))
got[typ] = true
}
})
Expand Down

0 comments on commit a297bfd

Please sign in to comment.