1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package inline_test
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"go/types"
"testing"
"golang.org/x/tools/internal/refactor/inline"
)
// TestCalleeEffects is a unit test of the calleefx analysis.
func TestCalleeEffects(t *testing.T) {
// Each callee must declare a function or method named f.
const funcName = "f"
var tests = []struct {
descr string
callee string // Go source file (sans package decl) containing callee decl
want string // expected effects string (-1=R∞ -2=W∞)
}{
{
"Assignments have unknown effects.",
`func f(x, y int) { x = y }`,
`[0 1 -2]`,
},
{
"Reads from globals are impure.",
`func f() { _ = g }; var g int`,
`[-1]`,
},
{
"Writes to globals have effects.",
`func f() { g = 0 }; var g int`,
`[-1 -2]`, // the -1 is spurious but benign
},
{
"Blank assign has no effect.",
`func f(x int) { _ = x }`,
`[0]`,
},
{
"Short decl of new var has has no effect.",
`func f(x int) { y := x; _ = y }`,
`[0]`,
},
{
"Short decl of existing var (y) is an assignment.",
`func f(x int) { y := x; y, z := 1, 2; _, _ = y, z }`,
`[0 -2]`,
},
{
"Unreferenced parameters are excluded.",
`func f(x, y, z int) { _ = z + x }`,
`[2 0]`,
},
{
"Built-in len has no effect.",
`func f(x, y string) { _ = len(y) + len(x) }`,
`[1 0]`,
},
{
"Built-in println has effects.",
`func f(x, y int) { println(y, x) }`,
`[1 0 -2]`,
},
{
"Return has no effect, and no control successor.",
`func f(x, y int) int { return x + y; panic(1) }`,
`[0 1]`,
},
{
"Loops (etc) have unknown effects.",
`func f(x, y bool) { for x { _ = y } }`,
`[0 -2 1]`,
},
{
"Calls have unknown effects.",
`func f(x, y int) { _, _, _ = x, g(), y }; func g() int`,
`[0 -2 1]`,
},
{
"Calls to some built-ins are pure.",
`func f(x, y int) { _, _, _ = x, len("hi"), y }`,
`[0 1]`,
},
{
"Calls to some built-ins are pure (variant).",
`func f(x, y int) { s := "hi"; _, _, _ = x, len(s), y; s = "bye" }`,
`[0 1 -2]`,
},
{
"Calls to some built-ins are pure (another variants).",
`func f(x, y int) { s := "hi"; _, _, _ = x, len(s), y }`,
`[0 1]`,
},
{
"Reading a local var is impure but does not have effects.",
`func f(x, y bool) { for x { _ = y } }`,
`[0 -2 1]`,
},
}
for _, test := range tests {
test := test
t.Run(test.descr, func(t *testing.T) {
fset := token.NewFileSet()
mustParse := func(filename string, content any) *ast.File {
f, err := parser.ParseFile(fset, filename, content, parser.ParseComments|parser.SkipObjectResolution)
if err != nil {
t.Fatalf("ParseFile: %v", err)
}
return f
}
// Parse callee file and find first func decl named f.
calleeContent := "package p\n" + test.callee
calleeFile := mustParse("callee.go", calleeContent)
var decl *ast.FuncDecl
for _, d := range calleeFile.Decls {
if d, ok := d.(*ast.FuncDecl); ok && d.Name.Name == funcName {
decl = d
break
}
}
if decl == nil {
t.Fatalf("declaration of func %s not found: %s", funcName, test.callee)
}
info := &types.Info{
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Types: make(map[ast.Expr]types.TypeAndValue),
Implicits: make(map[ast.Node]types.Object),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
Scopes: make(map[ast.Node]*types.Scope),
}
conf := &types.Config{Error: func(err error) { t.Error(err) }}
pkg, err := conf.Check("p", fset, []*ast.File{calleeFile}, info)
if err != nil {
t.Fatal(err)
}
callee, err := inline.AnalyzeCallee(t.Logf, fset, pkg, info, decl, []byte(calleeContent))
if err != nil {
t.Fatal(err)
}
if got := fmt.Sprint(callee.Effects()); got != test.want {
t.Errorf("for effects of %s, got %s want %s",
test.callee, got, test.want)
}
})
}
}
|