Skip to content

Commit a297bfd

Browse files
committed
go/callgraph/rta: audit for types.Alias safety
Updates golang/go#65294 Change-Id: I90fe0cc3ab5a6171e112e2eb0e452efb172d9980 Reviewed-on: https://go-review.googlesource.com/c/tools/+/559937 Reviewed-by: Zvonimir Pavlinovic <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 08393e0 commit a297bfd

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

go/callgraph/rta/rta.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
"golang.org/x/tools/go/callgraph"
4646
"golang.org/x/tools/go/ssa"
4747
"golang.org/x/tools/go/types/typeutil"
48+
"golang.org/x/tools/internal/aliases"
4849
"golang.org/x/tools/internal/compat"
4950
)
5051

@@ -416,6 +417,9 @@ func (r *rta) implementations(I *types.Interface) []types.Type {
416417
// dynamic type of some interface or reflect.Value.
417418
// Adapted from needMethods in go/ssa/builder.go
418419
func (r *rta) addRuntimeType(T types.Type, skip bool) {
420+
// Never record aliases.
421+
T = aliases.Unalias(T)
422+
419423
if prev, ok := r.result.RuntimeTypes.At(T).(bool); ok {
420424
if skip && !prev {
421425
r.result.RuntimeTypes.Set(T, skip)
@@ -457,7 +461,7 @@ func (r *rta) addRuntimeType(T types.Type, skip bool) {
457461
case *types.Named:
458462
n = T
459463
case *types.Pointer:
460-
n, _ = T.Elem().(*types.Named)
464+
n, _ = aliases.Unalias(T.Elem()).(*types.Named)
461465
}
462466
if n != nil {
463467
owner := n.Obj().Pkg()
@@ -476,6 +480,9 @@ func (r *rta) addRuntimeType(T types.Type, skip bool) {
476480
}
477481

478482
switch t := T.(type) {
483+
case *aliases.Alias:
484+
panic("unreachable")
485+
479486
case *types.Basic:
480487
// nop
481488

go/callgraph/rta/rta_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"golang.org/x/tools/go/loader"
2424
"golang.org/x/tools/go/ssa"
2525
"golang.org/x/tools/go/ssa/ssautil"
26+
"golang.org/x/tools/internal/aliases"
2627
)
2728

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

0 commit comments

Comments
 (0)