| 1 |
// Copyright 2013 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 |
package ssa_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/ |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 9 |
"errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/" |
| 10 |
"fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 11 |
"go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 12 |
"go/importerscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/" |
| 13 |
"go/parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/" |
| 14 |
"go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 15 |
"go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 16 |
"io/fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/" |
| 17 |
"osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 18 |
"os/execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/" |
| 19 |
"path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 20 |
"reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/" |
| 21 |
"runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/" |
| 22 |
"sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 23 |
"stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 24 |
"testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 25 |
|
| 26 |
"golang.org/x/sync/errgroupscip-go gomod golang.org/x/sync v0.20.0 `golang.org/x/sync/errgroup`/" |
| 27 |
"golang.org/x/tools/go/analysis/analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/" |
| 28 |
"golang.org/x/tools/go/packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/" |
| 29 |
"golang.org/x/tools/go/ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/" |
| 30 |
"golang.org/x/tools/go/ssa/ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/" |
| 31 |
"golang.org/x/tools/internal/expectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/expect`/" |
| 32 |
"golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/" |
| 33 |
) |
| 34 |
|
| 35 |
func isEmptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/isEmpty().(f *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#) bool { return f.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks. == nil } |
| 36 |
|
| 37 |
// Tests that programs partially loaded from gc object files contain |
| 38 |
// functions with no code for the external portions, but are otherwise ok. |
| 39 |
func TestBuildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestBuildPackage().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 40 |
testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsGoBuildscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsGoBuild().(t) // for importer.Default() |
| 41 |
|
| 42 |
input := ` |
| 43 |
package main |
| 44 |
|
| 45 |
import ( |
| 46 |
"bytes" |
| 47 |
"io" |
| 48 |
"testing" |
| 49 |
) |
| 50 |
|
| 51 |
func main() { |
| 52 |
var t testing.T |
| 53 |
t.Parallel() // static call to external declared method |
| 54 |
t.Fail() // static call to promoted external declared method |
| 55 |
testing.Short() // static call to external package-level function |
| 56 |
|
| 57 |
var w io.Writer = new(bytes.Buffer) |
| 58 |
w.Write(nil) // interface invoke of external declared method |
| 59 |
} |
| 60 |
` |
| 61 |
|
| 62 |
// Parse the file. |
| 63 |
fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().() |
| 64 |
f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, "input.go", input, 0) |
| 65 |
if err != nil { |
| 66 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 67 |
return |
| 68 |
} |
| 69 |
|
| 70 |
// Build an SSA program from the parsed file. |
| 71 |
// Load its dependencies from gc binary export data. |
| 72 |
mode := ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.SanityCheckFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/SanityCheckFunctions. |
| 73 |
mainPkg, _, err := ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.BuildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/BuildPackage().(&typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Configscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#{Importerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Importer.: importerscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/.Defaultscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/Default().()}, fset, |
| 74 |
typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPackagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPackage().("main", ""), []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#{f}, mode) |
| 75 |
if err != nil { |
| 76 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 77 |
return |
| 78 |
} |
| 79 |
|
| 80 |
// The main package, its direct and indirect dependencies are loaded. |
| 81 |
deps := []string{ |
| 82 |
// directly imported dependencies: |
| 83 |
"bytes", "io", "testing", |
| 84 |
// indirect dependencies mentioned by |
| 85 |
// the direct imports' export data |
| 86 |
"sync", "unicode", "time", |
| 87 |
} |
| 88 |
|
| 89 |
prog := mainPkg.Progscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Prog. |
| 90 |
all := prog.AllPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#AllPackages().() |
| 91 |
if len(all) <= len(deps) { |
| 92 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("unexpected set of loaded packages: %q", all) |
| 93 |
} |
| 94 |
for _, path := range deps { |
| 95 |
pkg := prog.ImportedPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#ImportedPackage().(path) |
| 96 |
if pkg == nil { |
| 97 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("package not loaded: %q", path) |
| 98 |
continue |
| 99 |
} |
| 100 |
|
| 101 |
// External packages should have no function bodies (except for wrappers). |
| 102 |
isExt := pkg != mainPkg |
| 103 |
|
| 104 |
// init() |
| 105 |
if isExt && !isEmptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/isEmpty().(pkg.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Func().("init")) { |
| 106 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("external package %s has non-empty init", pkg) |
| 107 |
} else if !isExt && isEmptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/isEmpty().(pkg.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Func().("init")) { |
| 108 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("main package %s has empty init", pkg) |
| 109 |
} |
| 110 |
|
| 111 |
for _, mem := range pkg.Membersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Members. { |
| 112 |
switch mem := mem.(type) { |
| 113 |
case *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#: |
| 114 |
// Functions at package level. |
| 115 |
if isExt && !isEmptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/isEmpty().(mem) { |
| 116 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("external function %s is non-empty", mem) |
| 117 |
} else if !isExt && isEmptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/isEmpty().(mem) { |
| 118 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("function %s is empty", mem) |
| 119 |
} |
| 120 |
|
| 121 |
case *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Type#: |
| 122 |
// Methods of named types T. |
| 123 |
// (In this test, all exported methods belong to *T not T.) |
| 124 |
if !isExt { |
| 125 |
t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("unexpected name type in main package: %s", mem) |
| 126 |
} |
| 127 |
mset := prog.MethodSetsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#MethodSets..MethodSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#MethodSet().(typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPointerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPointer().(mem.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Type#Type().())) |
| 128 |
for i, n := 0, mset.Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#Len().(); i < n; i++ { |
| 129 |
m := prog.MethodValuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#MethodValue().(mset.Atscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#At().(i)) |
| 130 |
// For external types, only synthetic wrappers have code. |
| 131 |
expExt := !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(m.Syntheticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Synthetic., "wrapper") |
| 132 |
if expExt && !isEmptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/isEmpty().(m) { |
| 133 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("external method %s is non-empty: %s", |
| 134 |
m, m.Syntheticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Synthetic.) |
| 135 |
} else if !expExt && isEmptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/isEmpty().(m) { |
| 136 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("method function %s is empty: %s", |
| 137 |
m, m.Syntheticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Synthetic.) |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
expectedCallee := []string{ |
| 145 |
"(*testing.T).Parallel", |
| 146 |
"(*testing.common).Fail", |
| 147 |
"testing.Short", |
| 148 |
"N/A", |
| 149 |
} |
| 150 |
callNum := 0 |
| 151 |
for _, b := range mainPkg.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Func().("main").Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks. { |
| 152 |
for _, instr := range b.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. { |
| 153 |
switch instr := instr.(type) { |
| 154 |
case ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.CallInstructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#: |
| 155 |
call := instr.Commonscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#Common.() |
| 156 |
if want := expectedCallee[callNum]; want != "N/A" { |
| 157 |
got := call.StaticCalleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#StaticCallee().().Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#String().() |
| 158 |
if want != got { |
| 159 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("call #%d from main.main: got callee %s, want %s", |
| 160 |
callNum, got, want) |
| 161 |
} |
| 162 |
} |
| 163 |
callNum++ |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
if callNum != 4 { |
| 168 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("in main.main: got %d calls, want %d", callNum, 4) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
// Tests that methods from indirect dependencies not subject to |
| 173 |
// CreatePackage are created as needed. |
| 174 |
func TestNoIndirectCreatePackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestNoIndirectCreatePackage().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 175 |
testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsGoBuildscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsGoBuild().(t) // for go/packages |
| 176 |
|
| 177 |
fs := openTxtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/openTxtar().(t, filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/.TestDatascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/TestData.(), "indirect.txtar")) |
| 178 |
pkgs := loadPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/loadPackages().(t, fs, "testdata/a") |
| 179 |
a := pkgs[0] |
| 180 |
|
| 181 |
// Create a from syntax, its direct deps b from types, but not indirect deps c. |
| 182 |
prog := ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.NewProgramscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/NewProgram().(a.Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Fset., ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.SanityCheckFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/SanityCheckFunctions.|ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.PrintFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/PrintFunctions.) |
| 183 |
aSSA := prog.CreatePackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#CreatePackage().(a.Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Types., a.Syntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Syntax., a.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#TypesInfo., false) |
| 184 |
for _, p := range a.Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Types..Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Imports().() { |
| 185 |
prog.CreatePackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#CreatePackage().(p, nil, nil, true) |
| 186 |
} |
| 187 |
|
| 188 |
// Build SSA for package a. |
| 189 |
aSSA.Buildscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Build().() |
| 190 |
|
| 191 |
// Find the function in the sole call in the sole block of function a.A. |
| 192 |
var got string |
| 193 |
for _, instr := range aSSA.Membersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Members.["A"].(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#).Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks.[0].Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. { |
| 194 |
if call, ok := instr.(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Call#); ok { |
| 195 |
f := call.Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Call#Call..Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Value..(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#) |
| 196 |
got = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%v # %s", f, f.Syntheticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Synthetic.) |
| 197 |
break |
| 198 |
} |
| 199 |
} |
| 200 |
want := "(testdata/c.C).F # from type information (on demand)" |
| 201 |
if got != want { |
| 202 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("for sole call in a.A, got: <<%s>>, want <<%s>>", got, want) |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// TestRuntimeTypes tests that (*Program).RuntimeTypes() includes all necessary types. |
| 207 |
func TestRuntimeTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestRuntimeTypes().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 208 |
testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsGoBuildscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsGoBuild().(t) // for importer.Default() |
| 209 |
|
| 210 |
// TODO(adonovan): these test cases don't really make logical |
| 211 |
// sense any more. Rethink. |
| 212 |
|
| 213 |
tests := []struct { |
| 214 |
input string |
| 215 |
want []string |
| 216 |
}{ |
| 217 |
// A package-level type is needed. |
| 218 |
{`package A; type T struct{}; func (T) f() {}; var x any = T{}`, |
| 219 |
[]string{"*p.T", "p.T"}, |
| 220 |
}, |
| 221 |
// An unexported package-level type is not needed. |
| 222 |
{`package B; type t struct{}; func (t) f() {}`, |
| 223 |
nil, |
| 224 |
}, |
| 225 |
// Subcomponents of type of exported package-level var are needed. |
| 226 |
{`package C; import "bytes"; var V struct {*bytes.Buffer}; var x any = &V`, |
| 227 |
[]string{"*bytes.Buffer", "*struct{*bytes.Buffer}", "struct{*bytes.Buffer}"}, |
| 228 |
}, |
| 229 |
// Subcomponents of type of unexported package-level var are not needed. |
| 230 |
{`package D; import "bytes"; var v struct {*bytes.Buffer}; var x any = v`, |
| 231 |
[]string{"*bytes.Buffer", "struct{*bytes.Buffer}"}, |
| 232 |
}, |
| 233 |
// Subcomponents of type of exported package-level function are needed. |
| 234 |
{`package E; import "bytes"; func F(struct {*bytes.Buffer}) {}; var v any = F`, |
| 235 |
[]string{"*bytes.Buffer", "struct{*bytes.Buffer}"}, |
| 236 |
}, |
| 237 |
// Subcomponents of type of unexported package-level function are not needed. |
| 238 |
{`package F; import "bytes"; func f(struct {*bytes.Buffer}) {}; var v any = f`, |
| 239 |
[]string{"*bytes.Buffer", "struct{*bytes.Buffer}"}, |
| 240 |
}, |
| 241 |
// Subcomponents of type of exported method of uninstantiated unexported type are not needed. |
| 242 |
{`package G; import "bytes"; type x struct{}; func (x) G(struct {*bytes.Buffer}) {}; var v x`, |
| 243 |
nil, |
| 244 |
}, |
| 245 |
// ...unless used by MakeInterface. |
| 246 |
{`package G2; import "bytes"; type x struct{}; func (x) G(struct {*bytes.Buffer}) {}; var v interface{} = x{}`, |
| 247 |
[]string{"*bytes.Buffer", "*p.x", "p.x", "struct{*bytes.Buffer}"}, |
| 248 |
}, |
| 249 |
// Subcomponents of type of unexported method are not needed. |
| 250 |
{`package I; import "bytes"; type X struct{}; func (X) G(struct {*bytes.Buffer}) {}; var x any = X{}`, |
| 251 |
[]string{"*bytes.Buffer", "*p.X", "p.X", "struct{*bytes.Buffer}"}, |
| 252 |
}, |
| 253 |
// Local types aren't needed. |
| 254 |
{`package J; import "bytes"; func f() { type T struct {*bytes.Buffer}; var t T; _ = t }`, |
| 255 |
nil, |
| 256 |
}, |
| 257 |
// ...unless used by MakeInterface. |
| 258 |
{`package K; import "bytes"; func f() { type T struct {*bytes.Buffer}; _ = interface{}(T{}) }`, |
| 259 |
[]string{"*bytes.Buffer", "*p.T", "p.T"}, |
| 260 |
}, |
| 261 |
// Types used as operand of MakeInterface are needed. |
| 262 |
{`package L; import "bytes"; func f() { _ = interface{}(struct{*bytes.Buffer}{}) }`, |
| 263 |
[]string{"*bytes.Buffer", "struct{*bytes.Buffer}"}, |
| 264 |
}, |
| 265 |
// MakeInterface is optimized away when storing to a blank. |
| 266 |
{`package M; import "bytes"; var _ interface{} = struct{*bytes.Buffer}{}`, |
| 267 |
nil, |
| 268 |
}, |
| 269 |
// MakeInterface does not create runtime type for parameterized types. |
| 270 |
{`package N; var g interface{}; func f[S any]() { var v []S; g = v }; `, |
| 271 |
nil, |
| 272 |
}, |
| 273 |
} |
| 274 |
for _, test := range tests { |
| 275 |
// Parse the file. |
| 276 |
fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().() |
| 277 |
f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, "input.go", test.input, 0) |
| 278 |
if err != nil { |
| 279 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("test %q: %s", test.input[:15], err) |
| 280 |
continue |
| 281 |
} |
| 282 |
|
| 283 |
// Create a single-file main package. |
| 284 |
// Load dependencies from gc binary export data. |
| 285 |
mode := ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.SanityCheckFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/SanityCheckFunctions. |
| 286 |
ssapkg, _, err := ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.BuildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/BuildPackage().(&typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Configscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#{Importerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Importer.: importerscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/.Defaultscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/Default().()}, fset, |
| 287 |
typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPackagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPackage().("p", ""), []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#{f}, mode) |
| 288 |
if err != nil { |
| 289 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("test %q: %s", test.input[:15], err) |
| 290 |
continue |
| 291 |
} |
| 292 |
|
| 293 |
var typstrs []string |
| 294 |
for _, T := range ssapkg.Progscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Prog..RuntimeTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#RuntimeTypes().() { |
| 295 |
if typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.IsInterfacescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/IsInterface().(T) || typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewMethodSetscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewMethodSet().(T).Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#Len().() == 0 { |
| 296 |
continue // skip interfaces and types without methods |
| 297 |
} |
| 298 |
typstrs = append(typstrs, T.Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#String().()) |
| 299 |
} |
| 300 |
sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(typstrs) |
| 301 |
|
| 302 |
if !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(typstrs, test.want) { |
| 303 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("test 'package %s': got %q, want %q", |
| 304 |
f.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name., typstrs, test.want) |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
// TestInit tests that synthesized init functions are correctly formed. |
| 310 |
// Bare init functions omit calls to dependent init functions and the use of |
| 311 |
// an init guard. They are useful in cases where the client uses a different |
| 312 |
// calling convention for init functions, or cases where it is easier for a |
| 313 |
// client to analyze bare init functions. Both of these aspects are used by |
| 314 |
// the llgo compiler for simpler integration with gccgo's runtime library, |
| 315 |
// and to simplify the analysis whereby it deduces which stores to globals |
| 316 |
// can be lowered to global initializers. |
| 317 |
func TestInitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestInit().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 318 |
tests := []struct { |
| 319 |
mode ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode# |
| 320 |
input, want string |
| 321 |
}{ |
| 322 |
{0, `package A; import _ "errors"; var i int = 42`, |
| 323 |
`# Name: A.init |
| 324 |
# Package: A |
| 325 |
# Synthetic: package initializer |
| 326 |
func init(): |
| 327 |
0: entry P:0 S:2 |
| 328 |
t0 = *init$guard bool |
| 329 |
if t0 goto 2 else 1 |
| 330 |
1: init.start P:1 S:1 idom:0 |
| 331 |
*init$guard = true:bool |
| 332 |
t1 = errors.init() () |
| 333 |
*i = 42:int |
| 334 |
jump 2 |
| 335 |
2: init.done P:2 S:0 idom:0 |
| 336 |
return |
| 337 |
|
| 338 |
`}, |
| 339 |
{ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BareInitsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BareInits., `package B; import _ "errors"; var i int = 42`, |
| 340 |
`# Name: B.init |
| 341 |
# Package: B |
| 342 |
# Synthetic: package initializer |
| 343 |
func init(): |
| 344 |
0: entry P:0 S:0 |
| 345 |
*i = 42:int |
| 346 |
return |
| 347 |
|
| 348 |
`}, |
| 349 |
} |
| 350 |
for _, test := range tests { |
| 351 |
// Create a single-file main package. |
| 352 |
mainPkg, _ := buildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/buildPackage().(t, test.input, test.mode) |
| 353 |
name := mainPkg.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Pkg..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Name().() |
| 354 |
initFunc := mainPkg.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Func().("init") |
| 355 |
if initFunc == nil { |
| 356 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("test 'package %s': no init function", name) |
| 357 |
continue |
| 358 |
} |
| 359 |
|
| 360 |
var initbuf bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer# |
| 361 |
_, err := initFunc.WriteToscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#WriteTo().(&initbuf) |
| 362 |
if err != nil { |
| 363 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("test 'package %s': WriteTo: %s", name, err) |
| 364 |
continue |
| 365 |
} |
| 366 |
|
| 367 |
if initbuf.Stringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#String().() != test.want { |
| 368 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("test 'package %s': got %s, want %s", name, initbuf.Stringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#String().(), test.want) |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
// TestSyntheticFuncs checks that the expected synthetic functions are |
| 374 |
// created, reachable, and not duplicated. |
| 375 |
func TestSyntheticFuncsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestSyntheticFuncs().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 376 |
const input = `package P |
| 377 |
type T int |
| 378 |
func (T) f() int |
| 379 |
func (*T) g() int |
| 380 |
var ( |
| 381 |
// thunks |
| 382 |
a = T.f |
| 383 |
b = T.f |
| 384 |
c = (struct{T}).f |
| 385 |
d = (struct{T}).f |
| 386 |
e = (*T).g |
| 387 |
f = (*T).g |
| 388 |
g = (struct{*T}).g |
| 389 |
h = (struct{*T}).g |
| 390 |
|
| 391 |
// bounds |
| 392 |
i = T(0).f |
| 393 |
j = T(0).f |
| 394 |
k = new(T).g |
| 395 |
l = new(T).g |
| 396 |
|
| 397 |
// wrappers |
| 398 |
m interface{} = struct{T}{} |
| 399 |
n interface{} = struct{T}{} |
| 400 |
o interface{} = struct{*T}{} |
| 401 |
p interface{} = struct{*T}{} |
| 402 |
q interface{} = new(struct{T}) |
| 403 |
r interface{} = new(struct{T}) |
| 404 |
s interface{} = new(struct{*T}) |
| 405 |
t interface{} = new(struct{*T}) |
| 406 |
) |
| 407 |
` |
| 408 |
pkg, _ := buildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/buildPackage().(t, input, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode#(0)) |
| 409 |
|
| 410 |
// Enumerate reachable synthetic functions |
| 411 |
want := map[string]string{ |
| 412 |
"(*P.T).g$bound": "bound method wrapper for func (*P.T).g() int", |
| 413 |
"(P.T).f$bound": "bound method wrapper for func (P.T).f() int", |
| 414 |
|
| 415 |
"(*P.T).g$thunk": "thunk for func (*P.T).g() int", |
| 416 |
"(P.T).f$thunk": "thunk for func (P.T).f() int", |
| 417 |
"(struct{*P.T}).g$thunk": "thunk for func (*P.T).g() int", |
| 418 |
"(struct{P.T}).f$thunk": "thunk for func (P.T).f() int", |
| 419 |
|
| 420 |
"(*P.T).f": "wrapper for func (P.T).f() int", |
| 421 |
"(*struct{*P.T}).f": "wrapper for func (P.T).f() int", |
| 422 |
"(*struct{*P.T}).g": "wrapper for func (*P.T).g() int", |
| 423 |
"(*struct{P.T}).f": "wrapper for func (P.T).f() int", |
| 424 |
"(*struct{P.T}).g": "wrapper for func (*P.T).g() int", |
| 425 |
"(struct{*P.T}).f": "wrapper for func (P.T).f() int", |
| 426 |
"(struct{*P.T}).g": "wrapper for func (*P.T).g() int", |
| 427 |
"(struct{P.T}).f": "wrapper for func (P.T).f() int", |
| 428 |
|
| 429 |
"P.init": "package initializer", |
| 430 |
} |
| 431 |
var seen []string // may contain dups |
| 432 |
for fn := range ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.AllFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/AllFunctions().(pkg.Progscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Prog.) { |
| 433 |
if fn.Syntheticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Synthetic. == "" { |
| 434 |
continue |
| 435 |
} |
| 436 |
name := fn.Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#String().() |
| 437 |
wantDescr, ok := want[name] |
| 438 |
if !ok { |
| 439 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("got unexpected/duplicate func: %q: %q", name, fn.Syntheticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Synthetic.) |
| 440 |
continue |
| 441 |
} |
| 442 |
seen = append(seen, name) |
| 443 |
|
| 444 |
if wantDescr != fn.Syntheticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Synthetic. { |
| 445 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("(%s).Synthetic = %q, want %q", name, fn.Syntheticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Synthetic., wantDescr) |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
for _, name := range seen { |
| 450 |
delete(want, name) |
| 451 |
} |
| 452 |
for fn, descr := range want { |
| 453 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("want func: %q: %q", fn, descr) |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
// TestPhiElimination ensures that dead phis, including those that |
| 458 |
// participate in a cycle, are properly eliminated. |
| 459 |
func TestPhiEliminationscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestPhiElimination().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 460 |
const input = ` |
| 461 |
package p |
| 462 |
|
| 463 |
func f() error |
| 464 |
|
| 465 |
func g(slice []int) { |
| 466 |
for { |
| 467 |
for range slice { |
| 468 |
// e should not be lifted to a dead φ-node. |
| 469 |
e := f() |
| 470 |
h(e) |
| 471 |
} |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
func h(error) |
| 476 |
` |
| 477 |
// The SSA code for this function should look something like this: |
| 478 |
// 0: |
| 479 |
// jump 1 |
| 480 |
// 1: |
| 481 |
// t0 = len(slice) |
| 482 |
// jump 2 |
| 483 |
// 2: |
| 484 |
// t1 = phi [1: -1:int, 3: t2] |
| 485 |
// t2 = t1 + 1:int |
| 486 |
// t3 = t2 < t0 |
| 487 |
// if t3 goto 3 else 1 |
| 488 |
// 3: |
| 489 |
// t4 = f() |
| 490 |
// t5 = h(t4) |
| 491 |
// jump 2 |
| 492 |
// |
| 493 |
// But earlier versions of the SSA construction algorithm would |
| 494 |
// additionally generate this cycle of dead phis: |
| 495 |
// |
| 496 |
// 1: |
| 497 |
// t7 = phi [0: nil:error, 2: t8] #e |
| 498 |
// ... |
| 499 |
// 2: |
| 500 |
// t8 = phi [1: t7, 3: t4] #e |
| 501 |
// ... |
| 502 |
|
| 503 |
p, _ := buildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/buildPackage().(t, input, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode#(0)) |
| 504 |
g := p.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Func().("g") |
| 505 |
|
| 506 |
phis := 0 |
| 507 |
for _, b := range g.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks. { |
| 508 |
for _, instr := range b.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. { |
| 509 |
if _, ok := instr.(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Phi#); ok { |
| 510 |
phis++ |
| 511 |
} |
| 512 |
} |
| 513 |
} |
| 514 |
if phis != 1 { |
| 515 |
g.WriteToscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#WriteTo().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr.) |
| 516 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("expected a single Phi (for the range index), got %d", phis) |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
// TestGenericDecls ensures that *unused* generic types, methods and functions |
| 521 |
// signatures can be built. |
| 522 |
// |
| 523 |
// TODO(taking): Add calls from non-generic functions to instantiations of generic functions. |
| 524 |
// TODO(taking): Add globals with types that are instantiations of generic functions. |
| 525 |
func TestGenericDeclsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestGenericDecls().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 526 |
const input = ` |
| 527 |
package p |
| 528 |
|
| 529 |
import "unsafe" |
| 530 |
|
| 531 |
type Pointer[T any] struct { |
| 532 |
v unsafe.Pointer |
| 533 |
} |
| 534 |
|
| 535 |
func (x *Pointer[T]) Load() *T { |
| 536 |
return (*T)(LoadPointer(&x.v)) |
| 537 |
} |
| 538 |
|
| 539 |
func Load[T any](x *Pointer[T]) *T { |
| 540 |
return x.Load() |
| 541 |
} |
| 542 |
|
| 543 |
func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer) |
| 544 |
` |
| 545 |
// The SSA members for this package should look something like this: |
| 546 |
// func LoadPointer func(addr *unsafe.Pointer) (val unsafe.Pointer) |
| 547 |
// type Pointer struct{v unsafe.Pointer} |
| 548 |
// method (*Pointer[T any]) Load() *T |
| 549 |
// func init func() |
| 550 |
// var init$guard bool |
| 551 |
|
| 552 |
p, _ := buildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/buildPackage().(t, input, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode#(0)) |
| 553 |
|
| 554 |
if load := p.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Func().("Load"); load.Signaturescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Signature..TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#TypeParams().().Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeParamList#Len().() != 1 { |
| 555 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("expected a single type param T for Load got %q", load.Signaturescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Signature.) |
| 556 |
} |
| 557 |
if ptr := p.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Type().("Pointer"); ptr.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Type#Type().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Namedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#).TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#TypeParams().().Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeParamList#Len().() != 1 { |
| 558 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("expected a single type param T for Pointer got %q", ptr.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Type#Type().()) |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
func TestGenericWrappersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestGenericWrappers().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 563 |
const input = ` |
| 564 |
package p |
| 565 |
|
| 566 |
type S[T any] struct { |
| 567 |
t *T |
| 568 |
} |
| 569 |
|
| 570 |
func (x S[T]) M() T { |
| 571 |
return *(x.t) |
| 572 |
} |
| 573 |
|
| 574 |
var thunk = S[int].M |
| 575 |
|
| 576 |
var g S[int] |
| 577 |
var bound = g.M |
| 578 |
|
| 579 |
type R[T any] struct{ S[T] } |
| 580 |
|
| 581 |
var indirect = R[int].M |
| 582 |
` |
| 583 |
// The relevant SSA members for this package should look something like this: |
| 584 |
// var bound func() int |
| 585 |
// var thunk func(S[int]) int |
| 586 |
// var wrapper func(R[int]) int |
| 587 |
|
| 588 |
for _, mode := range []ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode#{ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode#(0), ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.InstantiateGenericsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/InstantiateGenerics.} { |
| 589 |
p, _ := buildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/buildPackage().(t, input, mode) |
| 590 |
|
| 591 |
for _, entry := range []struct { |
| 592 |
name string // name of the package variable |
| 593 |
typ string // type of the package variable |
| 594 |
wrapper string // wrapper function to which the package variable is set |
| 595 |
callee string // callee within the wrapper function |
| 596 |
}{ |
| 597 |
{ |
| 598 |
"bound", |
| 599 |
"*func() int", |
| 600 |
"(p.S[int]).M$bound", |
| 601 |
"(p.S[int]).M[int]", |
| 602 |
}, |
| 603 |
{ |
| 604 |
"thunk", |
| 605 |
"*func(p.S[int]) int", |
| 606 |
"(p.S[int]).M$thunk", |
| 607 |
"(p.S[int]).M[int]", |
| 608 |
}, |
| 609 |
{ |
| 610 |
"indirect", |
| 611 |
"*func(p.R[int]) int", |
| 612 |
"(p.R[int]).M$thunk", |
| 613 |
"(p.S[int]).M[int]", |
| 614 |
}, |
| 615 |
} { |
| 616 |
t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(entry.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 617 |
v := p.Varscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Var().(entry.name) |
| 618 |
if v == nil { |
| 619 |
t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Did not find variable for %q in %s", entry.name, p.Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#String().()) |
| 620 |
} |
| 621 |
if v.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Global#Type().().Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#String().() != entry.typ { |
| 622 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected type for variable %s: %q. got %q", v, entry.typ, v.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Global#Type().()) |
| 623 |
} |
| 624 |
|
| 625 |
// Find the wrapper for v. This is stored exactly once in init. |
| 626 |
var wrapper *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function# |
| 627 |
for _, bb := range p.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Func().("init").Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks. { |
| 628 |
for _, i := range bb.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. { |
| 629 |
if store, ok := i.(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Storescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#); ok && v == store.Addrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#Addr. { |
| 630 |
switch val := store.Valscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#Val..(type) { |
| 631 |
case *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#: |
| 632 |
wrapper = val |
| 633 |
case *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.MakeClosurescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/MakeClosure#: |
| 634 |
wrapper = val.Fnscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/MakeClosure#Fn..(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#) |
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
} |
| 639 |
if wrapper == nil { |
| 640 |
t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("failed to find wrapper function for %s", entry.name) |
| 641 |
} |
| 642 |
if wrapper.Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#String().() != entry.wrapper { |
| 643 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected wrapper function %q. got %q", wrapper, entry.wrapper) |
| 644 |
} |
| 645 |
|
| 646 |
// Find the callee within the wrapper. There should be exactly one call. |
| 647 |
var callee *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function# |
| 648 |
for _, bb := range wrapper.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks. { |
| 649 |
for _, i := range bb.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. { |
| 650 |
if call, ok := i.(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Call#); ok { |
| 651 |
callee = call.Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Call#Call..StaticCalleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#StaticCallee().() |
| 652 |
} |
| 653 |
} |
| 654 |
} |
| 655 |
if callee == nil { |
| 656 |
t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("failed to find callee within wrapper %s", wrapper) |
| 657 |
} |
| 658 |
if callee.Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#String().() != entry.callee { |
| 659 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected callee in wrapper %q is %q. got %q", v, entry.callee, callee) |
| 660 |
} |
| 661 |
}) |
| 662 |
} |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
// TestTypeparamTest builds SSA over compilable examples in $GOROOT/test/typeparam/*.go. |
| 667 |
|
| 668 |
func TestTypeparamTestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestTypeparamTest().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 669 |
testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsGOROOTDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsGOROOTDir().(t, "test") |
| 670 |
|
| 671 |
// Tests use a fake goroot to stub out standard libraries with declarations in |
| 672 |
// testdata/src. Decreases runtime from ~80s to ~1s. |
| 673 |
|
| 674 |
if runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOARCHscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOARCH. == "wasm" { |
| 675 |
// Consistent flakes on wasm (#64726, #69409, #69410). |
| 676 |
// Needs more investigation, but more likely a wasm issue |
| 677 |
// Disabling for now. |
| 678 |
t.Skipscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skip().("Consistent flakes on wasm (e.g. https://go.dev/issues/64726)") |
| 679 |
} |
| 680 |
|
| 681 |
// located GOROOT based on the relative path of errors in $GOROOT/src/errors |
| 682 |
stdPkgs, err := packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.Loadscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Load().(&packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#{ |
| 683 |
Modescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Mode.: packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedFiles., |
| 684 |
}, "errors") |
| 685 |
if err != nil { |
| 686 |
t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Failed to load errors package from std: %s", err) |
| 687 |
} |
| 688 |
goroot := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Dir().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Dir().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Dir().(stdPkgs[0].GoFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#GoFiles.[0]))) |
| 689 |
dir := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(goroot, "test", "typeparam") |
| 690 |
if _, err = osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(dir); errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Isscip-go gomod github.com/golang/go/src go1.25.0 errors/Is().(err, osscip-go gomod github.com/golang/go/src go1.25.0 os/.ErrNotExistscip-go gomod github.com/golang/go/src go1.25.0 os/ErrNotExist.) { |
| 691 |
t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("test/typeparam doesn't exist under GOROOT %s", goroot) |
| 692 |
} |
| 693 |
|
| 694 |
// Collect all of the .go files in |
| 695 |
fsys := osscip-go gomod github.com/golang/go/src go1.25.0 os/.DirFSscip-go gomod github.com/golang/go/src go1.25.0 os/DirFS().(dir) |
| 696 |
entries, err := fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.ReadDirscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ReadDir().(fsys, ".") |
| 697 |
if err != nil { |
| 698 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 699 |
} |
| 700 |
|
| 701 |
// Each call to buildPackage calls package.Load, which invokes "go list", |
| 702 |
// and with over 300 subtests this can be very slow (minutes, or tens |
| 703 |
// on some platforms). So, we use an overlay to map each test file to a |
| 704 |
// distinct single-file package and load them all at once. |
| 705 |
overlay := map[string][]byte{ |
| 706 |
"go.mod": goModscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/goMod().("example.com", -1), |
| 707 |
} |
| 708 |
for _, entry := range entries { |
| 709 |
if entry.IsDirscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#IsDir().() || !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(entry.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().(), ".go") { |
| 710 |
continue // Consider standalone go files. |
| 711 |
} |
| 712 |
src, err := fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ReadFile().(fsys, entry.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().()) |
| 713 |
if err != nil { |
| 714 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 715 |
} |
| 716 |
// Only build test files that can be compiled, or compiled and run. |
| 717 |
if !bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 bytes/HasPrefix().(src, []byte("// run")) && !bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 bytes/HasPrefix().(src, []byte("// compile")) { |
| 718 |
t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("%s: not detected as a run test", entry.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().()) |
| 719 |
continue |
| 720 |
} |
| 721 |
|
| 722 |
filename := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s/main.go", entry.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().()) |
| 723 |
overlay[filename] = src |
| 724 |
} |
| 725 |
|
| 726 |
// load all packages inside the overlay so 'go list' will be triggered only once. |
| 727 |
pkgs := loadPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/loadPackages().(t, overlayFSscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/overlayFS().(overlay), "./...") |
| 728 |
for _, p := range pkgs { |
| 729 |
originFilename := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Base().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Dir().(p.GoFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#GoFiles.[0])) |
| 730 |
t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(originFilename, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 731 |
t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 732 |
prog, _ := ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.Packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/Packages().([]*packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#{p}, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.SanityCheckFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/SanityCheckFunctions.|ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.InstantiateGenericsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/InstantiateGenerics.) |
| 733 |
prog.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#Package().(p.Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Types.).Buildscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Build().() |
| 734 |
}) |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
// TestOrderOfOperations ensures order of operations are as intended. |
| 739 |
func TestOrderOfOperationsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestOrderOfOperations().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 740 |
// Testing for the order of operations within an expression is done |
| 741 |
// by collecting the sequence of direct function calls within a *Function. |
| 742 |
// Callees are all external functions so they cannot be safely re-ordered by ssa. |
| 743 |
const input = ` |
| 744 |
package p |
| 745 |
|
| 746 |
func a() int |
| 747 |
func b() int |
| 748 |
func c() int |
| 749 |
|
| 750 |
func slice(s []int) []int { return s[a():b()] } |
| 751 |
func sliceMax(s []int) []int { return s[a():b():c()] } |
| 752 |
|
| 753 |
` |
| 754 |
|
| 755 |
p, _ := buildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/buildPackage().(t, input, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode#(0)) |
| 756 |
|
| 757 |
for _, item := range []struct { |
| 758 |
fn string |
| 759 |
want string // sequence of calls within the function. |
| 760 |
}{ |
| 761 |
{"sliceMax", "[a() b() c()]"}, |
| 762 |
{"slice", "[a() b()]"}, |
| 763 |
} { |
| 764 |
fn := p.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Func().(item.fn) |
| 765 |
want := item.want |
| 766 |
t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(item.fn, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 767 |
t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 768 |
|
| 769 |
var calls []string |
| 770 |
for _, b := range fn.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks. { |
| 771 |
for _, instr := range b.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. { |
| 772 |
if call, ok := instr.(ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.CallInstructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#); ok { |
| 773 |
calls = append(calls, call.Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Instruction#String.()) |
| 774 |
} |
| 775 |
} |
| 776 |
} |
| 777 |
if got := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprint().(calls); got != want { |
| 778 |
fn.WriteToscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#WriteTo().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr.) |
| 779 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected sequence of function calls in %s was %s. got %s", fn, want, got) |
| 780 |
} |
| 781 |
}) |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
// TestGenericFunctionSelector ensures generic functions from other packages can be selected. |
| 786 |
func TestGenericFunctionSelectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestGenericFunctionSelector().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 787 |
fsys := overlayFSscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/overlayFS().(map[string][]byte{ |
| 788 |
"go.mod": goModscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/goMod().("example.com", -1), |
| 789 |
"main.go": []byte(`package main; import "example.com/a"; func main() { a.F[int](); a.G[int,string](); a.H(0) }`), |
| 790 |
"a/a.go": []byte(`package a; func F[T any](){}; func G[S, T any](){}; func H[T any](a T){} `), |
| 791 |
}) |
| 792 |
|
| 793 |
for _, mode := range []ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode#{ |
| 794 |
ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.SanityCheckFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/SanityCheckFunctions., |
| 795 |
ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.SanityCheckFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/SanityCheckFunctions. | ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.InstantiateGenericsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/InstantiateGenerics., |
| 796 |
} { |
| 797 |
|
| 798 |
pkgs := loadPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/loadPackages().(t, fsys, "example.com") // package main |
| 799 |
if len(pkgs) != 1 { |
| 800 |
t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Expected 1 root package but got %d", len(pkgs)) |
| 801 |
} |
| 802 |
prog, _ := ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.Packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/Packages().(pkgs, mode) |
| 803 |
p := prog.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#Package().(pkgs[0].Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Types.) |
| 804 |
p.Buildscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Build().() |
| 805 |
|
| 806 |
if p.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Pkg..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Name().() != "main" { |
| 807 |
t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Expected the second package is main but got %s", p.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Pkg..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Name().()) |
| 808 |
} |
| 809 |
p.Buildscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Build().() |
| 810 |
|
| 811 |
var callees []string // callees of the CallInstruction.String() in main(). |
| 812 |
for _, b := range p.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Func().("main").Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks. { |
| 813 |
for _, i := range b.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. { |
| 814 |
if call, ok := i.(ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.CallInstructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#); ok { |
| 815 |
if callee := call.Commonscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#Common.().StaticCalleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#StaticCallee().(); call != nil { |
| 816 |
callees = append(callees, callee.Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#String().()) |
| 817 |
} else { |
| 818 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("CallInstruction without StaticCallee() %q", call) |
| 819 |
} |
| 820 |
} |
| 821 |
} |
| 822 |
} |
| 823 |
sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(callees) // ignore the order in the code. |
| 824 |
|
| 825 |
want := "[example.com/a.F[int] example.com/a.G[int string] example.com/a.H[int]]" |
| 826 |
if got := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprint().(callees); got != want { |
| 827 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected main() to contain calls %v. got %v", want, got) |
| 828 |
} |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
func TestIssue58491scip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestIssue58491().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 833 |
// Test that a local type reaches type param in instantiation. |
| 834 |
src := ` |
| 835 |
package p |
| 836 |
|
| 837 |
func foo[T any](blocking func() (T, error)) error { |
| 838 |
type result struct { |
| 839 |
res T |
| 840 |
error // ensure the method set of result is non-empty |
| 841 |
} |
| 842 |
|
| 843 |
res := make(chan result, 1) |
| 844 |
go func() { |
| 845 |
var r result |
| 846 |
r.res, r.error = blocking() |
| 847 |
res <- r |
| 848 |
}() |
| 849 |
r := <-res |
| 850 |
err := r // require the rtype for result when instantiated |
| 851 |
return err |
| 852 |
} |
| 853 |
var Inst = foo[int] |
| 854 |
` |
| 855 |
fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().() |
| 856 |
f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, "p.go", src, 0) |
| 857 |
if err != nil { |
| 858 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 859 |
} |
| 860 |
files := []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#{f} |
| 861 |
|
| 862 |
pkg := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPackagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPackage().("p", "") |
| 863 |
conf := &typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Configscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#{} |
| 864 |
p, _, err := ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.BuildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/BuildPackage().(conf, fset, pkg, files, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.SanityCheckFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/SanityCheckFunctions.|ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.InstantiateGenericsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/InstantiateGenerics.) |
| 865 |
if err != nil { |
| 866 |
t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("unexpected error: %v", err) |
| 867 |
} |
| 868 |
|
| 869 |
// Find the local type result instantiated with int. |
| 870 |
var found bool |
| 871 |
for _, rt := range p.Progscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Prog..RuntimeTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#RuntimeTypes().() { |
| 872 |
if n, ok := rt.(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Namedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#); ok { |
| 873 |
if u, ok := n.Underlyingscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#Underlying().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Structscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Struct#); ok { |
| 874 |
found = true |
| 875 |
if got, want := n.Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#String().(), "p.result"; got != want { |
| 876 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected the name %s got: %s", want, got) |
| 877 |
} |
| 878 |
if got, want := u.Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Struct#String().(), "struct{res int; error}"; got != want { |
| 879 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected the underlying type of %s to be %s. got %s", n, want, got) |
| 880 |
} |
| 881 |
} |
| 882 |
} |
| 883 |
} |
| 884 |
if !found { |
| 885 |
t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("Failed to find any Named to struct types") |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
func TestIssue58491Recscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestIssue58491Rec().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 890 |
// Roughly the same as TestIssue58491 but with a recursive type. |
| 891 |
src := ` |
| 892 |
package p |
| 893 |
|
| 894 |
func foo[T any]() error { |
| 895 |
type result struct { |
| 896 |
res T |
| 897 |
next *result |
| 898 |
error // ensure the method set of result is non-empty |
| 899 |
} |
| 900 |
|
| 901 |
r := &result{} |
| 902 |
err := r // require the rtype for result when instantiated |
| 903 |
return err |
| 904 |
} |
| 905 |
var Inst = foo[int] |
| 906 |
` |
| 907 |
fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().() |
| 908 |
f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, "p.go", src, 0) |
| 909 |
if err != nil { |
| 910 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 911 |
} |
| 912 |
files := []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#{f} |
| 913 |
|
| 914 |
pkg := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPackagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPackage().("p", "") |
| 915 |
conf := &typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Configscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#{} |
| 916 |
p, _, err := ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.BuildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/BuildPackage().(conf, fset, pkg, files, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.SanityCheckFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/SanityCheckFunctions.|ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.InstantiateGenericsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/InstantiateGenerics.) |
| 917 |
if err != nil { |
| 918 |
t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("unexpected error: %v", err) |
| 919 |
} |
| 920 |
|
| 921 |
// Find the local type result instantiated with int. |
| 922 |
var found bool |
| 923 |
for _, rt := range p.Progscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Prog..RuntimeTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#RuntimeTypes().() { |
| 924 |
if n, ok := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Unaliasscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Unalias().(rt).(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Namedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#); ok { |
| 925 |
if u, ok := n.Underlyingscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#Underlying().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Structscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Struct#); ok { |
| 926 |
found = true |
| 927 |
if got, want := n.Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#String().(), "p.result"; got != want { |
| 928 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected the name %s got: %s", want, got) |
| 929 |
} |
| 930 |
if got, want := u.Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Struct#String().(), "struct{res int; next *p.result; error}"; got != want { |
| 931 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected the underlying type of %s to be %s. got %s", n, want, got) |
| 932 |
} |
| 933 |
} |
| 934 |
} |
| 935 |
} |
| 936 |
if !found { |
| 937 |
t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("Failed to find any Named to struct types") |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
// TestSyntax ensures that a function's Syntax is available. |
| 942 |
func TestSyntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestSyntax().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 943 |
const input = `package p |
| 944 |
|
| 945 |
type P int |
| 946 |
func (x *P) g() *P { return x } |
| 947 |
|
| 948 |
func F[T ~int]() *T { |
| 949 |
type S1 *T |
| 950 |
type S2 *T |
| 951 |
type S3 *T |
| 952 |
f1 := func() S1 { |
| 953 |
f2 := func() S2 { |
| 954 |
return S2(nil) |
| 955 |
} |
| 956 |
return S1(f2()) |
| 957 |
} |
| 958 |
f3 := func() S3 { |
| 959 |
return S3(f1()) |
| 960 |
} |
| 961 |
return (*T)(f3()) |
| 962 |
} |
| 963 |
var g = F[int] |
| 964 |
var _ = F[P] // unreferenced => not instantiated |
| 965 |
` |
| 966 |
|
| 967 |
p, _ := buildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/buildPackage().(t, input, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.InstantiateGenericsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/InstantiateGenerics.) |
| 968 |
prog := p.Progscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Prog. |
| 969 |
|
| 970 |
// Collect syntax information for all of the functions. |
| 971 |
got := make(map[string]string) |
| 972 |
for fn := range ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.AllFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/AllFunctions().(prog) { |
| 973 |
if fn.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Name().() == "init" { |
| 974 |
continue |
| 975 |
} |
| 976 |
syntax := fn.Syntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Syntax().() |
| 977 |
if got[fn.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Name().()] != "" { |
| 978 |
t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("dup") |
| 979 |
} |
| 980 |
got[fn.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Name().()] = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%T : %s @ %d", syntax, fn.Signaturescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Signature., prog.Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#Fset..Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(syntax.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().()).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line.) |
| 981 |
} |
| 982 |
|
| 983 |
want := map[string]string{ |
| 984 |
"g": "*ast.FuncDecl : func() *p.P @ 4", |
| 985 |
"F": "*ast.FuncDecl : func[T ~int]() *T @ 6", |
| 986 |
"F$1": "*ast.FuncLit : func() p.S1 @ 10", |
| 987 |
"F$1$1": "*ast.FuncLit : func() p.S2 @ 11", |
| 988 |
"F$2": "*ast.FuncLit : func() p.S3 @ 16", |
| 989 |
"F[int]": "*ast.FuncDecl : func() *int @ 6", |
| 990 |
"F[int]$1": "*ast.FuncLit : func() p.S1 @ 10", |
| 991 |
"F[int]$1$1": "*ast.FuncLit : func() p.S2 @ 11", |
| 992 |
"F[int]$2": "*ast.FuncLit : func() p.S3 @ 16", |
| 993 |
// ...but no F[P] etc as they are unreferenced. |
| 994 |
// (NB: GlobalDebug mode would cause them to be referenced.) |
| 995 |
} |
| 996 |
if !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(got, want) { |
| 997 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected the functions with signature to be:\n\t%#v.\n Got:\n\t%#v", want, got) |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
func TestGo117Builtinsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestGo117Builtins().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 1002 |
tests := []struct { |
| 1003 |
name string |
| 1004 |
src string |
| 1005 |
importer typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Importerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Importer# |
| 1006 |
}{ |
| 1007 |
{"slice to array pointer", "package p; var s []byte; var _ = (*[4]byte)(s)", nil}, |
| 1008 |
{"unsafe slice", `package p; import "unsafe"; var _ = unsafe.Add(nil, 0)`, importerscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/.Defaultscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/Default().()}, |
| 1009 |
{"unsafe add", `package p; import "unsafe"; var _ = unsafe.Slice((*int)(nil), 0)`, importerscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/.Defaultscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/Default().()}, |
| 1010 |
} |
| 1011 |
|
| 1012 |
for _, tc := range tests { |
| 1013 |
t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tc.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 1014 |
t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 1015 |
fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().() |
| 1016 |
f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, "p.go", tc.src, parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseCommentsscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseComments.) |
| 1017 |
if err != nil { |
| 1018 |
t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().(err) |
| 1019 |
} |
| 1020 |
files := []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#{f} |
| 1021 |
|
| 1022 |
pkg := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPackagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPackage().("p", "") |
| 1023 |
conf := &typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Configscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#{Importerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Importer.: tc.importer} |
| 1024 |
if _, _, err := ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.BuildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/BuildPackage().(conf, fset, pkg, files, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.SanityCheckFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/SanityCheckFunctions.); err != nil { |
| 1025 |
t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().(err) |
| 1026 |
} |
| 1027 |
}) |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
// TestLabels just tests that anonymous labels are handled. |
| 1032 |
func TestLabelsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestLabels().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 1033 |
tests := []string{ |
| 1034 |
`package main |
| 1035 |
func main() { _:println(1) }`, |
| 1036 |
`package main |
| 1037 |
func main() { _:println(1); _:println(2)}`, |
| 1038 |
} |
| 1039 |
for _, test := range tests { |
| 1040 |
buildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/buildPackage().(t, test, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode#(0)) |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
func TestFixedBugsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestFixedBugs().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 1045 |
for _, name := range []string{ |
| 1046 |
"issue66783a", |
| 1047 |
"issue66783b", |
| 1048 |
"issue73594", |
| 1049 |
"issue78110", |
| 1050 |
} { |
| 1051 |
t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 1052 |
base := name + ".go" |
| 1053 |
path := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/.TestDatascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/TestData.(), "fixedbugs", base) |
| 1054 |
fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().() |
| 1055 |
f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, path, nil, parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseCommentsscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseComments.) |
| 1056 |
if err != nil { |
| 1057 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 1058 |
} |
| 1059 |
files := []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#{f} |
| 1060 |
pkg := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPackagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPackage().(name, name) |
| 1061 |
mode := ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.SanityCheckFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/SanityCheckFunctions. | ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.InstantiateGenericsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/InstantiateGenerics. |
| 1062 |
// mode |= ssa.PrintFunctions // debug mode |
| 1063 |
if _, _, err := ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.BuildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/BuildPackage().(&typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Configscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#{}, fset, pkg, files, mode); err != nil { |
| 1064 |
t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().(err) |
| 1065 |
} |
| 1066 |
}) |
| 1067 |
} |
| 1068 |
} |
| 1069 |
|
| 1070 |
func TestIssue67079scip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestIssue67079().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 1071 |
// This test reproduced a race in the SSA builder nearly 100% of the time. |
| 1072 |
|
| 1073 |
// Load the package. |
| 1074 |
const src = `package p; type T int; func (T) f() {}; var _ = (*T).f` |
| 1075 |
spkg, ppkg := buildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/buildPackage().(t, src, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode#(0)) |
| 1076 |
prog := spkg.Progscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Prog. |
| 1077 |
var g errgroupscip-go gomod golang.org/x/sync v0.20.0 `golang.org/x/sync/errgroup`/.Groupscip-go gomod golang.org/x/sync v0.20.0 `golang.org/x/sync/errgroup`/Group# |
| 1078 |
|
| 1079 |
// Access bodies of all functions. |
| 1080 |
g.Goscip-go gomod golang.org/x/sync v0.20.0 `golang.org/x/sync/errgroup`/Group#Go().(func() error { |
| 1081 |
for fn := range ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.AllFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/AllFunctions().(prog) { |
| 1082 |
for _, b := range fn.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks. { |
| 1083 |
for _, instr := range b.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. { |
| 1084 |
if call, ok := instr.(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Call#); ok { |
| 1085 |
call.Commonscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Call#Common().().StaticCalleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#StaticCallee().() // access call.Value |
| 1086 |
} |
| 1087 |
} |
| 1088 |
} |
| 1089 |
} |
| 1090 |
return nil |
| 1091 |
}) |
| 1092 |
|
| 1093 |
// Force building of wrappers. |
| 1094 |
g.Goscip-go gomod golang.org/x/sync v0.20.0 `golang.org/x/sync/errgroup`/Group#Go().(func() error { |
| 1095 |
ptrT := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPointerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPointer().(ppkg.Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Types..Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Scope().().Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Lookup().("T").Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Type().()) |
| 1096 |
ptrTf := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewMethodSetscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewMethodSet().(ptrT).Atscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#At().(0) // (*T).f symbol |
| 1097 |
prog.MethodValuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#MethodValue().(ptrTf) |
| 1098 |
return nil |
| 1099 |
}) |
| 1100 |
|
| 1101 |
g.Waitscip-go gomod golang.org/x/sync v0.20.0 `golang.org/x/sync/errgroup`/Group#Wait().() // ignore error |
| 1102 |
} |
| 1103 |
|
| 1104 |
func TestGenericAliasesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestGenericAliases().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 1105 |
testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsGo1Pointscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsGo1Point().(t, 23) |
| 1106 |
|
| 1107 |
if osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 os/Getenv().("GENERICALIASTEST_CHILD") == "1" { |
| 1108 |
testGenericAliasesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/testGenericAliases().(t) |
| 1109 |
return |
| 1110 |
} |
| 1111 |
|
| 1112 |
testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsExecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsExec().(t) |
| 1113 |
testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsToolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsTool().(t, "go") |
| 1114 |
|
| 1115 |
cmd := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Argsscip-go gomod github.com/golang/go/src go1.25.0 os/Args.[0], "-test.run=TestGenericAliases") |
| 1116 |
cmd.Envscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Env. = append(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Environscip-go gomod github.com/golang/go/src go1.25.0 os/Environ().(), "GENERICALIASTEST_CHILD=1") |
| •1117 |
cmd.Envscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Env. = append(cmd.Envscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Env., |
| 1118 |
"GO111MODULE=on", |
| 1119 |
) |
| 1120 |
if testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.Go1Pointscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/Go1Point().() == 23 { |
| 1121 |
cmd.Envscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Env. = append(cmd.Envscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Env., "GOEXPERIMENT=aliastypeparams") |
| 1122 |
} |
| 1123 |
out, err := cmd.CombinedOutputscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#CombinedOutput().() |
| 1124 |
if len(out) > 0 { |
| 1125 |
t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("out=<<%s>>", out) |
| 1126 |
} |
| 1127 |
var exitcode int |
| 1128 |
if err, ok := err.(*execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.ExitErrorscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/ExitError#); ok { |
| 1129 |
exitcode = err.ExitCodescip-go gomod github.com/golang/go/src go1.25.0 os/ProcessState#ExitCode().() |
| 1130 |
} |
| 1131 |
const want = 0 |
| 1132 |
if exitcode != want { |
| 1133 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("exited %d, want %d", exitcode, want) |
| 1134 |
} |
| 1135 |
} |
| 1136 |
|
| 1137 |
func testGenericAliasesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/testGenericAliases().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 1138 |
testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsGoExperimentscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsGoExperiment().(t, "aliastypeparams") |
| 1139 |
|
| 1140 |
const source = ` |
| 1141 |
package p |
| 1142 |
|
| 1143 |
type A = uint8 |
| 1144 |
type B[T any] = [4]T |
| 1145 |
|
| 1146 |
var F = f[string] |
| 1147 |
|
| 1148 |
func f[S any]() { |
| 1149 |
// Two copies of f are made: p.f[S] and p.f[string] |
| 1150 |
|
| 1151 |
var v A // application of A that is declared outside of f without no type arguments |
| 1152 |
print("p.f", "String", "p.A", v) |
| 1153 |
print("p.f", "==", v, uint8(0)) |
| 1154 |
print("p.f[string]", "String", "p.A", v) |
| 1155 |
print("p.f[string]", "==", v, uint8(0)) |
| 1156 |
|
| 1157 |
|
| 1158 |
var u B[S] // application of B that is declared outside declared outside of f with type arguments |
| 1159 |
print("p.f", "String", "p.B[S]", u) |
| 1160 |
print("p.f", "==", u, [4]S{}) |
| 1161 |
print("p.f[string]", "String", "p.B[string]", u) |
| 1162 |
print("p.f[string]", "==", u, [4]string{}) |
| 1163 |
|
| 1164 |
type C[T any] = struct{ s S; ap *B[T]} // declaration within f with type params |
| 1165 |
var w C[int] // application of C with type arguments |
| 1166 |
print("p.f", "String", "p.C[int]", w) |
| 1167 |
print("p.f", "==", w, struct{ s S; ap *[4]int}{}) |
| 1168 |
print("p.f[string]", "String", "p.C[int]", w) |
| 1169 |
print("p.f[string]", "==", w, struct{ s string; ap *[4]int}{}) |
| 1170 |
} |
| 1171 |
` |
| 1172 |
|
| 1173 |
p, _ := buildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/buildPackage().(t, source, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.InstantiateGenericsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/InstantiateGenerics.) |
| 1174 |
|
| 1175 |
probes := callsToscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/callsTo().(ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.AllFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/AllFunctions().(p.Progscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Prog.), "print") |
| 1176 |
if got, want := len(probes), 3*4*2; got != want { |
| 1177 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Found %v probes, expected %v", got, want) |
| 1178 |
} |
| 1179 |
|
| 1180 |
const debug = false // enable to debug skips |
| 1181 |
skipped := 0 |
| 1182 |
for probe, fn := range probes { |
| 1183 |
// Each probe is of the form: |
| 1184 |
// print("within", "test", head, tail) |
| 1185 |
// The probe only matches within a function whose fn.String() is within. |
| 1186 |
// This allows for different instantiations of fn to match different probes. |
| 1187 |
// On a match, it applies the test named "test" to head::tail. |
| 1188 |
if len(probe.Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Args.) < 3 { |
| 1189 |
t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("probe %v did not have enough arguments", probe) |
| 1190 |
} |
| 1191 |
within, test, head, tail := constStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/constString().(probe.Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Args.[0]), probe.Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Args.[1], probe.Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Args.[2], probe.Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Args.[3:] |
| 1192 |
if within != fn.Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#String().() { |
| 1193 |
skipped++ |
| 1194 |
if debug { |
| 1195 |
t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("Skipping %q within %q", within, fn.Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#String().()) |
| 1196 |
} |
| 1197 |
continue // does not match function |
| 1198 |
} |
| 1199 |
|
| 1200 |
switch test := constStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/constString().(test); test { |
| 1201 |
case "==": // All of the values are types.Identical. |
| 1202 |
for _, v := range tail { |
| 1203 |
if !typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Identicalscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Identical().(head.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#Type.(), v.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#Type.()) { |
| 1204 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected %v and %v to have identical types", head, v) |
| 1205 |
} |
| 1206 |
} |
| 1207 |
case "String": // head is a string constant that all values in tail must match Type().String() |
| 1208 |
want := constStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/constString().(head) |
| 1209 |
for _, v := range tail { |
| 1210 |
if got := v.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#Type.().Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#String().(); got != want { |
| 1211 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: %v had the Type().String()=%q. expected %q", within, v, got, want) |
| 1212 |
} |
| 1213 |
} |
| 1214 |
default: |
| 1215 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%q is not a test subcommand", test) |
| 1216 |
} |
| 1217 |
} |
| 1218 |
if want := 3 * 4; skipped != want { |
| 1219 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Skipped %d probes, expected to skip %d", skipped, want) |
| 1220 |
} |
| 1221 |
} |
| 1222 |
|
| 1223 |
// constString returns the value of a string constant |
| 1224 |
// or "<not a constant string>" if the value is not a string constant. |
| 1225 |
func constStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/constString().(v ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#) string { |
| 1226 |
if c, ok := v.(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Constscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Const#); ok { |
| 1227 |
str := c.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Const#Value..Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/Value#String().() |
| 1228 |
return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Trimscip-go gomod github.com/golang/go/src go1.25.0 strings/Trim().(str, `"`) |
| 1229 |
} |
| 1230 |
return "<not a constant string>" |
| 1231 |
} |
| 1232 |
|
| 1233 |
// TestMultipleGoversions tests that globals initialized to equivalent |
| 1234 |
// function literals are compiled based on the different GoVersion in each file. |
| 1235 |
func TestMultipleGoversionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestMultipleGoversions().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 1236 |
var contents = map[string]string{ |
| 1237 |
"post.go": ` |
| 1238 |
//go:build go1.22 |
| 1239 |
package p |
| 1240 |
|
| 1241 |
var distinct = func(l []int) { |
| 1242 |
for i := range l { |
| 1243 |
print(&i) |
| 1244 |
} |
| 1245 |
} |
| 1246 |
`, |
| 1247 |
"pre.go": ` |
| 1248 |
package p |
| 1249 |
|
| 1250 |
var same = func(l []int) { |
| 1251 |
for i := range l { |
| 1252 |
print(&i) |
| 1253 |
} |
| 1254 |
} |
| 1255 |
`, |
| 1256 |
} |
| 1257 |
|
| 1258 |
fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().() |
| 1259 |
var files []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File# |
| 1260 |
for _, fname := range []string{"post.go", "pre.go"} { |
| 1261 |
file, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, fname, contents[fname], 0) |
| 1262 |
if err != nil { |
| 1263 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 1264 |
} |
| 1265 |
files = append(files, file) |
| 1266 |
} |
| 1267 |
|
| 1268 |
pkg := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPackagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPackage().("p", "") |
| 1269 |
conf := &typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Configscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#{Importerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Importer.: nil, GoVersionscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#GoVersion.: "go1.21"} |
| 1270 |
p, _, err := ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.BuildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/BuildPackage().(conf, fset, pkg, files, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.SanityCheckFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/SanityCheckFunctions.) |
| 1271 |
if err != nil { |
| 1272 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 1273 |
} |
| 1274 |
|
| 1275 |
// Test that global is initialized to a function literal that was |
| 1276 |
// compiled to have the expected for loop range variable lifetime for i. |
| 1277 |
for _, test := range []struct { |
| 1278 |
global *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Globalscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Global# |
| 1279 |
want string // basic block to []*ssa.Alloc. |
| 1280 |
}{ |
| 1281 |
{p.Varscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Var().("same"), "map[entry:[new int (i)]]"}, // i is allocated in the entry block. |
| 1282 |
{p.Varscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Var().("distinct"), "map[rangeindex.body:[new int (i)]]"}, // i is allocated in the body block. |
| 1283 |
} { |
| 1284 |
// Find the function the test.name global is initialized to. |
| 1285 |
var fn *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function# |
| 1286 |
for _, b := range p.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Func().("init").Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks. { |
| 1287 |
for _, instr := range b.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. { |
| 1288 |
if s, ok := instr.(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Storescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#); ok && s.Addrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#Addr. == test.global { |
| 1289 |
fn, _ = s.Valscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#Val..(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#) |
| 1290 |
} |
| 1291 |
} |
| 1292 |
} |
| 1293 |
if fn == nil { |
| 1294 |
t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Failed to find *ssa.Function for initial value of global %s", test.global) |
| 1295 |
} |
| 1296 |
|
| 1297 |
allocs := make(map[string][]string) // block comments -> []Alloc |
| 1298 |
for _, b := range fn.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks. { |
| 1299 |
for _, instr := range b.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. { |
| 1300 |
if a, ok := instr.(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Allocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#); ok { |
| 1301 |
allocs[b.Commentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Comment.] = append(allocs[b.Commentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Comment.], a.Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#String().()) |
| 1302 |
} |
| 1303 |
} |
| 1304 |
} |
| 1305 |
if got := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprint().(allocs); got != test.want { |
| 1306 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("[%s:=%s] expected the allocations to be in the basic blocks %q, got %q", test.global, fn, test.want, got) |
| 1307 |
} |
| 1308 |
} |
| 1309 |
} |
| 1310 |
|
| 1311 |
// TestRangeOverInt tests that, in a range-over-int (#61405), |
| 1312 |
// the type of each range var v (identified by print(v) calls) |
| 1313 |
// has the expected type. |
| 1314 |
func TestRangeOverIntscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestRangeOverInt().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 1315 |
const rangeOverIntSrc = ` |
| 1316 |
package p |
| 1317 |
|
| 1318 |
type I uint8 |
| 1319 |
|
| 1320 |
func noKey(x int) { |
| 1321 |
for range x { |
| 1322 |
// does not crash |
| 1323 |
} |
| 1324 |
} |
| 1325 |
|
| 1326 |
func untypedConstantOperand() { |
| 1327 |
for i := range 10 { |
| 1328 |
print(i) /*@ types("int")*/ |
| 1329 |
} |
| 1330 |
} |
| 1331 |
|
| 1332 |
func unsignedOperand(x uint64) { |
| 1333 |
for i := range x { |
| 1334 |
print(i) /*@ types("uint64")*/ |
| 1335 |
} |
| 1336 |
} |
| 1337 |
|
| 1338 |
func namedOperand(x I) { |
| 1339 |
for i := range x { |
| 1340 |
print(i) /*@ types("p.I")*/ |
| 1341 |
} |
| 1342 |
} |
| 1343 |
|
| 1344 |
func typeparamOperand[T int](x T) { |
| 1345 |
for i := range x { |
| 1346 |
print(i) /*@ types("T")*/ |
| 1347 |
} |
| 1348 |
} |
| 1349 |
|
| 1350 |
func assignment(x I) { |
| 1351 |
var k I |
| 1352 |
for k = range x { |
| 1353 |
print(k) /*@ types("p.I")*/ |
| 1354 |
} |
| 1355 |
} |
| 1356 |
` |
| 1357 |
|
| 1358 |
fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().() |
| 1359 |
f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, "p.go", rangeOverIntSrc, parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseCommentsscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseComments.) |
| 1360 |
if err != nil { |
| 1361 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 1362 |
} |
| 1363 |
|
| 1364 |
pkg := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPackagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPackage().("p", "") |
| 1365 |
conf := &typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Configscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#{} |
| 1366 |
p, _, err := ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.BuildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/BuildPackage().(conf, fset, pkg, []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#{f}, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.SanityCheckFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/SanityCheckFunctions.) |
| 1367 |
if err != nil { |
| 1368 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 1369 |
} |
| 1370 |
|
| 1371 |
// Collect all notes in f, i.e. comments starting with "//@ types". |
| 1372 |
notes, err := expectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/expect`/.ExtractGoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/expect`/ExtractGo().(fset.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(f.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Pos().()), f) |
| 1373 |
if err != nil { |
| 1374 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 1375 |
} |
| 1376 |
|
| 1377 |
// Collect calls to the built-in print function. |
| 1378 |
fns := make(map[*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#]bool) |
| 1379 |
for _, mem := range p.Membersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Members. { |
| 1380 |
if fn, ok := mem.(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#); ok { |
| 1381 |
fns[fn] = true |
| 1382 |
} |
| 1383 |
} |
| 1384 |
probes := callsToscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/callsTo().(fns, "print") |
| 1385 |
expectations := matchNotesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/matchNotes().(fset, notes, probes) |
| 1386 |
|
| 1387 |
for call := range probes { |
| 1388 |
if expectations[call] == nil { |
| 1389 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Unmatched call: %v @ %s", call, fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(call.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Pos().())) |
| 1390 |
} |
| 1391 |
} |
| 1392 |
|
| 1393 |
// Check each expectation. |
| 1394 |
for call, note := range expectations { |
| 1395 |
var args []string |
| 1396 |
for _, a := range call.Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Args. { |
| 1397 |
args = append(args, a.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#Type.().Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#String().()) |
| 1398 |
} |
| 1399 |
if got, want := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprint().(args), fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprint().(note.Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/expect`/Note#Args.); got != want { |
| 1400 |
at := fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(call.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Pos().()) |
| 1401 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: arguments to print had types %s, want %s", at, got, want) |
| 1402 |
logFunctionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/logFunction().(t, probes[call]) |
| 1403 |
} |
| 1404 |
} |
| 1405 |
} |
| 1406 |
|
| 1407 |
func TestBuildPackageGo120scip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestBuildPackageGo120().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 1408 |
tests := []struct { |
| 1409 |
name string |
| 1410 |
src string |
| 1411 |
importer typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Importerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Importer# |
| 1412 |
}{ |
| 1413 |
{"slice to array", "package p; var s []byte; var _ = ([4]byte)(s)", nil}, |
| 1414 |
{"slice to zero length array", "package p; var s []byte; var _ = ([0]byte)(s)", nil}, |
| 1415 |
{"slice to zero length array type parameter", "package p; var s []byte; func f[T ~[0]byte]() { tmp := (T)(s); var z T; _ = tmp == z}", nil}, |
| 1416 |
{"slice to non-zero length array type parameter", "package p; var s []byte; func h[T ~[1]byte | [4]byte]() { tmp := T(s); var z T; _ = tmp == z}", nil}, |
| 1417 |
{"slice to maybe-zero length array type parameter", "package p; var s []byte; func g[T ~[0]byte | [4]byte]() { tmp := T(s); var z T; _ = tmp == z}", nil}, |
| 1418 |
{ |
| 1419 |
"rune sequence to sequence cast patterns", ` |
| 1420 |
package p |
| 1421 |
// Each of fXX functions describes a 1.20 legal cast between sequences of runes |
| 1422 |
// as []rune, pointers to rune arrays, rune arrays, or strings. |
| 1423 |
// |
| 1424 |
// Comments listed given the current emitted instructions [approximately]. |
| 1425 |
// If multiple conversions are needed, these are separated by |. |
| 1426 |
// rune was selected as it leads to string casts (byte is similar). |
| 1427 |
// The length 2 is not significant. |
| 1428 |
// Multiple array lengths may occur in a cast in practice (including 0). |
| 1429 |
func f00[S string, D string](s S) { _ = D(s) } // ChangeType |
| 1430 |
func f01[S string, D []rune](s S) { _ = D(s) } // Convert |
| 1431 |
func f02[S string, D []rune | string](s S) { _ = D(s) } // ChangeType | Convert |
| 1432 |
func f03[S [2]rune, D [2]rune](s S) { _ = D(s) } // ChangeType |
| 1433 |
func f04[S *[2]rune, D *[2]rune](s S) { _ = D(s) } // ChangeType |
| 1434 |
func f05[S []rune, D string](s S) { _ = D(s) } // Convert |
| 1435 |
func f06[S []rune, D [2]rune](s S) { _ = D(s) } // SliceToArrayPointer; Deref |
| 1436 |
func f07[S []rune, D [2]rune | string](s S) { _ = D(s) } // SliceToArrayPointer; Deref | Convert |
| 1437 |
func f08[S []rune, D *[2]rune](s S) { _ = D(s) } // SliceToArrayPointer |
| 1438 |
func f09[S []rune, D *[2]rune | string](s S) { _ = D(s) } // SliceToArrayPointer; Deref | Convert |
| 1439 |
func f10[S []rune, D *[2]rune | [2]rune](s S) { _ = D(s) } // SliceToArrayPointer | SliceToArrayPointer; Deref |
| 1440 |
func f11[S []rune, D *[2]rune | [2]rune | string](s S) { _ = D(s) } // SliceToArrayPointer | SliceToArrayPointer; Deref | Convert |
| 1441 |
func f12[S []rune, D []rune](s S) { _ = D(s) } // ChangeType |
| 1442 |
func f13[S []rune, D []rune | string](s S) { _ = D(s) } // Convert | ChangeType |
| 1443 |
func f14[S []rune, D []rune | [2]rune](s S) { _ = D(s) } // ChangeType | SliceToArrayPointer; Deref |
| 1444 |
func f15[S []rune, D []rune | [2]rune | string](s S) { _ = D(s) } // ChangeType | SliceToArrayPointer; Deref | Convert |
| 1445 |
func f16[S []rune, D []rune | *[2]rune](s S) { _ = D(s) } // ChangeType | SliceToArrayPointer |
| 1446 |
func f17[S []rune, D []rune | *[2]rune | string](s S) { _ = D(s) } // ChangeType | SliceToArrayPointer | Convert |
| 1447 |
func f18[S []rune, D []rune | *[2]rune | [2]rune](s S) { _ = D(s) } // ChangeType | SliceToArrayPointer | SliceToArrayPointer; Deref |
| 1448 |
func f19[S []rune, D []rune | *[2]rune | [2]rune | string](s S) { _ = D(s) } // ChangeType | SliceToArrayPointer | SliceToArrayPointer; Deref | Convert |
| 1449 |
func f20[S []rune | string, D string](s S) { _ = D(s) } // Convert | ChangeType |
| 1450 |
func f21[S []rune | string, D []rune](s S) { _ = D(s) } // Convert | ChangeType |
| 1451 |
func f22[S []rune | string, D []rune | string](s S) { _ = D(s) } // ChangeType | Convert | Convert | ChangeType |
| 1452 |
func f23[S []rune | [2]rune, D [2]rune](s S) { _ = D(s) } // SliceToArrayPointer; Deref | ChangeType |
| 1453 |
func f24[S []rune | *[2]rune, D *[2]rune](s S) { _ = D(s) } // SliceToArrayPointer | ChangeType |
| 1454 |
`, nil, |
| 1455 |
}, |
| 1456 |
{ |
| 1457 |
"matching named and underlying types", ` |
| 1458 |
package p |
| 1459 |
type a string |
| 1460 |
type b string |
| 1461 |
func g0[S []rune | a | b, D []rune | a | b](s S) { _ = D(s) } |
| 1462 |
func g1[S []rune | ~string, D []rune | a | b](s S) { _ = D(s) } |
| 1463 |
func g2[S []rune | a | b, D []rune | ~string](s S) { _ = D(s) } |
| 1464 |
func g3[S []rune | ~string, D []rune |~string](s S) { _ = D(s) } |
| 1465 |
`, nil, |
| 1466 |
}, |
| 1467 |
} |
| 1468 |
|
| 1469 |
for _, tc := range tests { |
| 1470 |
t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tc.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 1471 |
t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 1472 |
fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().() |
| 1473 |
f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, "p.go", tc.src, 0) |
| 1474 |
if err != nil { |
| 1475 |
t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().(err) |
| 1476 |
} |
| 1477 |
files := []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#{f} |
| 1478 |
|
| 1479 |
pkg := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPackagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPackage().("p", "") |
| 1480 |
conf := &typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Configscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#{Importerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Importer.: tc.importer} |
| 1481 |
_, _, err = ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.BuildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/BuildPackage().(conf, fset, pkg, files, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.SanityCheckFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/SanityCheckFunctions.) |
| 1482 |
if err != nil { |
| 1483 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("unexpected error: %v", err) |
| 1484 |
} |
| 1485 |
}) |
| 1486 |
} |
| 1487 |
} |