Skip to content

Commit b020bdb

Browse files
committed
go/callgraph/vta: add type alias test
Change-Id: Id3b3157d916a63e82a48fba066ac8bab56c21c98 Reviewed-on: https://go-review.googlesource.com/c/tools/+/579256 Run-TryBot: Zvonimir Pavlinovic <[email protected]> TryBot-Result: Gopher Robot <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Tim King <[email protected]>
1 parent cc29c91 commit b020bdb

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// go:build ignore
6+
7+
// This file is the same as callgraph_interfaces.go except for
8+
// types J, X, Y, and Z aliasing types I, A, B, and C, resp.
9+
10+
package testdata
11+
12+
type I interface {
13+
Foo()
14+
}
15+
16+
type A struct{}
17+
18+
func (a A) Foo() {}
19+
20+
type B struct{}
21+
22+
func (b B) Foo() {}
23+
24+
type C struct{}
25+
26+
func (c C) Foo() {}
27+
28+
type J = I
29+
type X = A
30+
type Y = B
31+
type Z = C
32+
33+
func NewY() Y {
34+
return Y{}
35+
}
36+
37+
func Do(b bool) J {
38+
if b {
39+
return X{}
40+
}
41+
42+
z := Z{}
43+
z.Foo()
44+
45+
return NewY()
46+
}
47+
48+
func Baz(b bool) {
49+
Do(b).Foo()
50+
}
51+
52+
// Relevant SSA:
53+
// func Baz(b bool):
54+
// t0 = Do(b)
55+
// t1 = invoke t0.Foo()
56+
// return
57+
58+
// func Do(b bool) I:
59+
// ...
60+
// t1 = (C).Foo(struct{}{}:C)
61+
// t2 = NewY()
62+
// t3 = make I <- B (t2)
63+
// return t3
64+
65+
// WANT:
66+
// Baz: Do(b) -> Do; invoke t0.Foo() -> A.Foo, B.Foo
67+
// Do: (C).Foo(struct{}{}:C) -> C.Foo; NewY() -> NewY

go/callgraph/vta/vta_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func TestVTACallGraph(t *testing.T) {
2727
"testdata/src/callgraph_recursive_types.go",
2828
"testdata/src/callgraph_issue_57756.go",
2929
"testdata/src/callgraph_comma_maps.go",
30+
"testdata/src/callgraph_type_aliases.go",
3031
} {
3132
t.Run(file, func(t *testing.T) {
3233
prog, want, err := testProg(file, ssa.BuilderMode(0))
@@ -40,7 +41,7 @@ func TestVTACallGraph(t *testing.T) {
4041
g := CallGraph(ssautil.AllFunctions(prog), cha.CallGraph(prog))
4142
got := callGraphStr(g)
4243
if diff := setdiff(want, got); len(diff) > 0 {
43-
t.Errorf("computed callgraph %v should contain %v (diff: %v)", got, want, diff)
44+
t.Errorf("computed callgraph %v\nshould contain\n%v\n(diff: %v)", got, want, diff)
4445
}
4546
})
4647
}

0 commit comments

Comments
 (0)