Debian Codegraph @

internal/gcimporter/gcimporter_test.go

raw symbols SCIP
1 // Copyright 2011 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 // This file is a copy of $GOROOT/src/go/internal/gcimporter/gcimporter_test.go,
6 // adjusted to make it build with code from (std lib) internal/testenv copied.
7
8 package gcimporter_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/
9
10 import (
11 "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/"
12 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
13 "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/"
14 "go/constantscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/"
15 goimporterscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/ "go/importerscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/"
16 goparserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ "go/parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/"
17 "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/"
18 "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/"
19 "osscip-go gomod github.com/golang/go/src go1.25.0 os/"
20 "os/execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/"
21 "pathscip-go gomod github.com/golang/go/src go1.25.0 path/"
22 "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/"
23 "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/"
24 "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/"
25 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
26 "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/"
27 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
28 "timescip-go gomod github.com/golang/go/src go1.25.0 time/"
29
30 "golang.org/x/tools/internal/gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/"
31 "golang.org/x/tools/internal/gorootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/goroot`/"
32 "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/"
33 )
34
35 func TestMainscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestMain().(m *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Mscip-go gomod github.com/golang/go/src go1.25.0 testing/M#) {
36 testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.ExitIfSmallMachinescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/ExitIfSmallMachine().()
37 osscip-go gomod github.com/golang/go/src go1.25.0 os/.Exitscip-go gomod github.com/golang/go/src go1.25.0 os/Exit().(m.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/M#Run().())
38 }
39
40 // ----------------------------------------------------------------------------
41
42 func needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(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#, compiler string) {
43 if runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.Compilerscip-go gomod github.com/golang/go/src go1.25.0 runtime/Compiler. == compiler {
44 return
45 }
46 switch compiler {
47 case "gc":
48 t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("gc-built packages not available (compiler = %s)", runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.Compilerscip-go gomod github.com/golang/go/src go1.25.0 runtime/Compiler.)
49 }
50 }
51
52 // compile runs the compiler on filename, with dirname as the working directory,
53 // and writes the output file to outdirname.
54 // compile gives the resulting package a packagepath of p.
55 func compilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compile().(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#, dirname, filename, outdirname string, packagefiles map[string]string) string {
56 return compilePkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compilePkg().(t, dirname, filename, outdirname, packagefiles, "p")
57 }
58
59 func compilePkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compilePkg().(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#, dirname, filename, outdirname string, packagefiles map[string]string, pkg string) string {
60 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)
61
62 // filename must end with ".go"
63 basename := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSuffix().(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().(filename), ".go")
64 ok := filename != basename
65 if !ok {
66 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("filename doesn't end in .go: %s", filename)
67 }
68 objname := basename + ".o"
69 outname := 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().(outdirname, objname)
70
71 importcfgfile := osscip-go gomod github.com/golang/go/src go1.25.0 os/.DevNullscip-go gomod github.com/golang/go/src go1.25.0 os/DevNull.
72 if len(packagefiles) > 0 {
73 importcfgfile = 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().(outdirname, basename) + ".importcfg"
74 importcfg := new(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#)
75 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(importcfg, "# import config")
76 for k, v := range packagefiles {
77 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(importcfg, "\npackagefile %s=%s\n", k, v)
78 }
79 if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().(importcfgfile, importcfg.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().(), 0655); err != nil {
80 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
81 }
82 }
83
84 importreldir := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.0 strings/ReplaceAll().(outdirname, string(osscip-go gomod github.com/golang/go/src go1.25.0 os/.PathSeparatorscip-go gomod github.com/golang/go/src go1.25.0 os/PathSeparator.), "/")
85 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().("go", "tool", "compile", "-p", pkg, "-D", importreldir, "-importcfg", importcfgfile, "-o", outname, filename)
86 cmd.Dirscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Dir. = dirname
87 if out, err := cmd.CombinedOutputscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#CombinedOutput().(); err != nil {
88 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("%s", out)
89 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("go tool compile %s failed: %s", filename, err)
90 }
91 return outname
92 }
93
94 func testPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testPath().(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#, path, srcDir string) *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package# {
95 t0 := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().()
96 pkg, err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.Importscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/Import().(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().(), make(map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#), path, srcDir, nil)
97 if err != nil {
98 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("testPath(%s): %s", path, err)
99 return nil
100 }
101 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("testPath(%s): %v", path, timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sincescip-go gomod github.com/golang/go/src go1.25.0 time/Since().(t0))
102 return pkg
103 }
104
105 func mktmpdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/mktmpdir().(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#) string {
106 tmpdir, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.MkdirTempscip-go gomod github.com/golang/go/src go1.25.0 os/MkdirTemp().("", "gcimporter_test")
107 if err != nil {
108 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("mktmpdir:", err)
109 }
110 if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Mkdirscip-go gomod github.com/golang/go/src go1.25.0 os/Mkdir().(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().(tmpdir, "testdata"), 0700); err != nil {
111 osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(tmpdir)
112 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("mktmpdir:", err)
113 }
114 return tmpdir
115 }
116
117 const testfilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testfile. = "exports.go"
118
119 func TestImportTestdatascip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestImportTestdata().(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#) {
120 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
121 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) // to find stdlib export data in the build cache
122
123 tmpdir := mktmpdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/mktmpdir().(t)
124 defer osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(tmpdir)
125
126 packageFiles := map[string]string{}
127 for _, pkg := range []string{"go/ast", "go/token"} {
128 export, _, err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.FindPkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/FindPkg().(pkg, "testdata")
129 if export == "" {
130 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("no export data found for %s: %s", pkg, err)
131 }
132 packageFiles[pkg] = export
133 }
134
135 compilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compile().(t, "testdata", testfilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testfile., 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().(tmpdir, "testdata"), packageFiles)
136
137 // filename should end with ".go"
138 filename := testfilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testfile.[:len(testfilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testfile.)-3]
139 if pkg := testPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testPath().(t, "./testdata/"+filename, tmpdir); pkg != nil {
140 // The package's Imports list must include all packages
141 // explicitly imported by testfile, plus all packages
142 // referenced indirectly via exported objects in testfile.
143 // With the textual export format (when run against Go1.6),
144 // the list may also include additional packages that are
145 // not strictly required for import processing alone (they
146 // are exported to err "on the safe side").
147 // For now, we just test the presence of a few packages
148 // that we know are there for sure.
149 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().(pkg.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Imports().())
150 wants := []string{"go/ast", "go/token", "go/ast"}
151 for _, want := range wants {
152 if !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().(got, want) {
153 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().(`Package("exports").Imports() = %s, does not contain %s`, got, want)
154 }
155 }
156 }
157 }
158
159 func TestImportTypeparamTestsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestImportTypeparamTests().(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#) {
160 if testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Shortscip-go gomod github.com/golang/go/src go1.25.0 testing/Short().() {
161 t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("in short mode, skipping test that requires export data for all of std")
162 }
163
164 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) // to find stdlib export data in the build cache
165 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")
166
167 // This package only handles gc export data.
168 if runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.Compilerscip-go gomod github.com/golang/go/src go1.25.0 runtime/Compiler. != "gc" {
169 t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("gc-built packages not available (compiler = %s)", runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.Compilerscip-go gomod github.com/golang/go/src go1.25.0 runtime/Compiler.)
170 }
171
172 tmpdir := mktmpdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/mktmpdir().(t)
173 defer osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(tmpdir)
174
175 // Check go files in test/typeparam, except those that fail for a known
176 // reason.
177 rootDir := 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().(runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOROOTscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOROOT().(), "test", "typeparam")
178 list, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadDirscip-go gomod github.com/golang/go/src go1.25.0 os/ReadDir().(rootDir)
179 if err != nil {
180 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
181 }
182
183 for _, entry := range list {
184 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") {
185 // For now, only consider standalone go files.
186 continue
187 }
188
189 t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(entry.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#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#) {
190 filename := 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().(rootDir, entry.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().())
191 src, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 os/ReadFile().(filename)
192 if err != nil {
193 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
194 }
195 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")) {
196 // We're bypassing the logic of run.go here, so be conservative about
197 // the files we consider in an attempt to make this test more robust to
198 // changes in test/typeparams.
199 t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("not detected as a run test")
200 }
201
202 // Compile and import, and compare the resulting package with the package
203 // that was type-checked directly.
204 pkgFiles, err := gorootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/goroot`/.PkgfileMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/goroot`/PkgfileMap().()
205 if err != nil {
206 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
207 }
208 compilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compile().(t, rootDir, entry.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().(), 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().(tmpdir, "testdata"), pkgFiles)
209 pkgName := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSuffix().(entry.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().(), ".go")
210 imported := importPkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importPkg().(t, "./testdata/"+pkgName, tmpdir)
211 checked := checkFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/checkFile().(t, filename, src)
212
213 seen := make(map[string]bool)
214 for _, name := range imported.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Scope().().Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Names().() {
215 if !tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.IsExportedscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/IsExported().(name) {
216 continue // ignore synthetic names like .inittask and .dict.*
217 }
218 seen[name] = true
219
220 importedObj := imported.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().(name)
221 got := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.ObjectStringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/ObjectString().(importedObj, typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.RelativeToscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/RelativeTo().(imported))
222
223 checkedObj := checked.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().(name)
224 if checkedObj == nil {
225 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("imported object %q was not type-checked", name)
226 }
227 want := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.ObjectStringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/ObjectString().(checkedObj, typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.RelativeToscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/RelativeTo().(checked))
228
229 if got != want {
230 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("imported %q as %q, want %q", name, got, want)
231 }
232 }
233
234 for _, name := range checked.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Scope().().Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Names().() {
235 if !tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.IsExportedscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/IsExported().(name) || seen[name] {
236 continue
237 }
238 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("did not import object %q", name)
239 }
240 })
241 }
242 }
243
244 func checkFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/checkFile().(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#, filename string, src []byte) *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package# {
245 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().()
246 f, err := goparserscip-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, filename, src, 0)
247 if err != nil {
248 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
249 }
250 config := 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#{
251 Importerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Importer.: goimporterscip-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().(),
252 }
253 pkg, err := config.Checkscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Check().("", fset, []*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}, nil)
254 if err != nil {
255 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
256 }
257 return pkg
258 }
259
260 func TestVersionHandlingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestVersionHandling().(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#) {
261 // This package only handles gc export data.
262 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
263
264 const dir = "./testdata/versions"
265 list, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadDirscip-go gomod github.com/golang/go/src go1.25.0 os/ReadDir().(dir)
266 if err != nil {
267 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
268 }
269
270 tmpdir := mktmpdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/mktmpdir().(t)
271 defer osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(tmpdir)
272 corruptdir := 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().(tmpdir, "testdata", "versions")
273 if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Mkdirscip-go gomod github.com/golang/go/src go1.25.0 os/Mkdir().(corruptdir, 0700); err != nil {
274 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
275 }
276
277 for _, f := range list {
278 name := f.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().()
279 if !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().(name, ".a") {
280 continue // not a package file
281 }
282 if 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().(name, "corrupted") {
283 continue // don't process a leftover corrupted file
284 }
285 pkgpath := "./" + name[:len(name)-2]
286
287 if testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Verbosescip-go gomod github.com/golang/go/src go1.25.0 testing/Verbose().() {
288 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("importing %s", name)
289 }
290
291 // test that export data can be imported
292 _, err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.Importscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/Import().(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().(), make(map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#), pkgpath, dir, nil)
293 if err != nil {
294 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("import %q failed: %v", pkgpath, err)
295 continue
296 }
297
298 // create file with corrupted export data
299 // 1) read file
300 data, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 os/ReadFile().(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().(dir, name))
301 if err != nil {
302 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
303 }
304 // 2) find export data
305 // Index is an incorrect but 'good enough for tests' way to find the end of the export data.
306 i := bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Indexscip-go gomod github.com/golang/go/src go1.25.0 bytes/Index().(data, []byte("\n$$B\n")) + 5
307 j := bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Indexscip-go gomod github.com/golang/go/src go1.25.0 bytes/Index().(data[i:], []byte("\n$$\n")) + i
308 if i < 0 || j < 0 || i > j {
309 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("export data section not found (i = %d, j = %d)", i, j)
310 }
311 // 3) corrupt the data (increment every 7th byte)
312 for k := j - 13; k >= i; k -= 7 {
313 data[k]++
314 }
315 // 4) write the file
316 pkgpath += "_corrupted"
317 filename := 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().(corruptdir, pkgpath) + ".a"
318 osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().(filename, data, 0666)
319
320 // test that importing the corrupted file results in an error
321 _, err = gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.Importscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/Import().(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().(), make(map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#), pkgpath, corruptdir, nil)
322 if err == nil {
323 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("import corrupted %q succeeded", pkgpath)
324 } else if msg := err.Error(); !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().(msg, "internal error") {
325 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("import %q error incorrect (%s)", pkgpath, msg)
326 }
327 }
328 }
329
330 func TestImportStdLibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestImportStdLib().(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#) {
331 if testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Shortscip-go gomod github.com/golang/go/src go1.25.0 testing/Short().() {
332 t.Skipscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skip().("the imports can be expensive, and this test is especially slow when the build cache is empty")
333 }
334 // This package only handles gc export data.
335 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
336 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) // to find stdlib export data in the build cache
337
338 // Get list of packages in stdlib. Filter out test-only packages with {{if .GoFiles}} check.
339 var stderr 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#
340 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().("go", "list", "-f", "{{if .GoFiles}}{{.ImportPath}}{{end}}", "std")
341 cmd.Stderrscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stderr. = &stderr
342 out, err := cmd.Outputscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Output().()
343 if err != nil {
344 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("failed to run go list to determine stdlib packages: %v\nstderr:\n%v", err, stderr.Stringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#String().())
345 }
346 pkgs := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Fieldsscip-go gomod github.com/golang/go/src go1.25.0 strings/Fields().(string(out))
347
348 var nimports int
349 for _, pkg := range pkgs {
350 t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(pkg, 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#) {
351 if testPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testPath().(t, pkg, 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().(testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.GOROOTscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/GOROOT().(t), "src", pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Dirscip-go gomod github.com/golang/go/src go1.25.0 path/Dir().(pkg))) != nil {
352 nimports++
353 }
354 })
355 }
356 const minPkgs = 225 // 'GOOS=plan9 go1.18 list std | wc -l' reports 228; most other platforms have more.
357 if len(pkgs) < minPkgs {
358 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("too few packages (%d) were imported", nimports)
359 }
360
361 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("tested %d imports", nimports)
362 }
363
364 var importedObjectTestsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importedObjectTests. = []struct {
365 namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importedObjectTests:name. string
366 wantscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importedObjectTests:want. string
367 }{
368 // non-interfaces
369 {"crypto.Hash", "type Hash uint"},
370 {"go/ast.ObjKind", "type ObjKind int"},
371 {"go/types.Qualifier", "type Qualifier func(*Package) string"},
372 {"go/types.Comparable", "func Comparable(T Type) bool"},
373 {"math.Pi", "const Pi untyped float"},
374 {"math.Sin", "func Sin(x float64) float64"},
375 {"go/ast.NotNilFilter", "func NotNilFilter(_ string, v reflect.Value) bool"},
376
377 // interfaces
378 {"context.Context", "type Context interface{Deadline() (deadline time.Time, ok bool); Done() <-chan struct{}; Err() error; Value(key any) any}"},
379 {"crypto.Decrypter", "type Decrypter interface{Decrypt(rand io.Reader, msg []byte, opts DecrypterOpts) (plaintext []byte, err error); Public() PublicKey}"},
380 {"encoding.BinaryMarshaler", "type BinaryMarshaler interface{MarshalBinary() (data []byte, err error)}"},
381 {"io.Reader", "type Reader interface{Read(p []byte) (n int, err error)}"},
382 {"io.ReadWriter", "type ReadWriter interface{Reader; Writer}"},
383 {"go/ast.Node", "type Node interface{End() go/token.Pos; Pos() go/token.Pos}"},
384 {"go/types.Type", "type Type interface{String() string; Underlying() Type}"},
385 }
386
387 func TestImportedTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestImportedTypes().(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#) {
388 // This package only handles gc export data.
389 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
390 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) // to find stdlib export data in the build cache
391
392 for _, test := range importedObjectTestsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importedObjectTests. {
393 obj := importObjectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importObject().(t, test.namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importedObjectTests:name.)
394 if obj == nil {
395 continue // error reported elsewhere
396 }
397 got := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.ObjectStringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/ObjectString().(obj, typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.RelativeToscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/RelativeTo().(obj.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pkg().()))
398
399 // TODO(rsc): Delete this block once go.dev/cl/368254 lands.
400 if got != test.wantscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importedObjectTests:want. && test.wantscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importedObjectTests:want. == stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.0 strings/ReplaceAll().(got, "interface{}", "any") {
401 got = test.wantscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importedObjectTests:want.
402 }
403
404 if got != test.wantscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importedObjectTests:want. {
405 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: got %q; want %q", test.namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importedObjectTests:name., got, test.wantscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importedObjectTests:want.)
406 }
407
408 if named, _ := 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().(obj.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#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#); named != nil {
409 verifyInterfaceMethodRecvsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/verifyInterfaceMethodRecvs().(t, named, 0)
410 }
411 }
412 }
413
414 func TestImportedConstsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestImportedConsts().(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#) {
415 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) // to find stdlib export data in the build cache
416
417 tests := []struct {
418 name string
419 want constantscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/.Kindscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/Kind#
420 }{
421 {"math.Pi", constantscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/.Floatscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/Float.},
422 {"math.MaxFloat64", constantscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/.Floatscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/Float.},
423 {"math.MaxInt64", constantscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/.Intscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/Int.},
424 }
425
426 for _, test := range tests {
427 obj := importObjectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importObject().(t, test.name)
428 if got := obj.(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Constscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Const#).Valscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Const#Val().().Kindscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/Value#Kind().(); got != test.want {
429 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: imported as constant.Kind(%v), want constant.Kind(%v)", test.name, got, test.want)
430 }
431 }
432 }
433
434 // importObject imports the object specified by a name of the form
435 // <import path>.<object name>, e.g. go/types.Type.
436 //
437 // If any errors occur they are reported via t and the resulting object will
438 // be nil.
439 func importObjectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importObject().(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#, name string) typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object# {
440 s := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.25.0 strings/Split().(name, ".")
441 if len(s) != 2 {
442 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("inconsistent test data")
443 }
444 importPath := s[0]
445 objName := s[1]
446
447 pkg, err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.Importscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/Import().(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().(), make(map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#), importPath, ".", nil)
448 if err != nil {
449 t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().(err)
450 return nil
451 }
452
453 obj := pkg.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().(objName)
454 if obj == nil {
455 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: object not found", name)
456 return nil
457 }
458 return obj
459 }
460
461 // verifyInterfaceMethodRecvs verifies that method receiver types
462 // are named if the methods belong to a named interface type.
463 func verifyInterfaceMethodRecvsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/verifyInterfaceMethodRecvs().(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#, named *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#, level int) {
464 // avoid endless recursion in case of an embedding bug that lead to a cycle
465 if level > 10 {
466 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: embeds itself", named)
467 return
468 }
469
470 iface, _ := named.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`/.Interfacescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Interface#)
471 if iface == nil {
472 return // not an interface
473 }
474
475 // check explicitly declared methods
476 for m := range iface.ExplicitMethodsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Interface#ExplicitMethods().() {
477 recv := m.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Signaturescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#).Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Recv().()
478 if recv == nil {
479 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: missing receiver type", m)
480 continue
481 }
482 if recv.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().() != named {
483 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: got recv type %s; want %s", m, recv.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().(), named)
484 }
485 }
486
487 // check embedded interfaces (if they are named, too)
488 for etyp := range iface.EmbeddedTypesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Interface#EmbeddedTypes().() {
489 // embedding of interfaces cannot have cycles; recursion will terminate
490 if etype, _ := 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().(etyp).(*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#); etype != nil {
491 verifyInterfaceMethodRecvsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/verifyInterfaceMethodRecvs().(t, etype, level+1)
492 }
493 }
494 }
495
496 func TestIssue5815scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestIssue5815().(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#) {
497 // This package only handles gc export data.
498 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
499 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) // to find stdlib export data in the build cache
500
501 pkg := importPkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importPkg().(t, "strings", ".")
502
503 scope := pkg.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Scope().()
504 for _, name := range scope.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Names().() {
505 obj := scope.Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Lookup().(name)
506 if obj.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pkg().() == nil {
507 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("no pkg for %s", obj)
508 }
509 if tname, _ := obj.(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.TypeNamescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeName#); tname != nil {
510 named := 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().(tname.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#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#)
511 for m := range named.Methodsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#Methods().() {
512 if m.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#Pkg().() == nil {
513 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("no pkg for %s", m)
514 }
515 }
516 }
517 }
518 }
519
520 // Smoke test to ensure that imported methods get the correct package.
521 func TestCorrectMethodPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestCorrectMethodPackage().(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#) {
522 // This package only handles gc export data.
523 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
524 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) // to find stdlib export data in the build cache
525
526 imports := make(map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#)
527 _, err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.Importscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/Import().(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().(), imports, "net/http", ".", nil)
528 if err != nil {
529 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
530 }
531
532 mutex := imports["sync"].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().("Mutex").(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.TypeNamescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeName#).Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().()
533 mset := 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().(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().(mutex)) // methods of *sync.Mutex
534 sel := mset.Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#Lookup().(nil, "Lock")
535 lock := sel.Objscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Selection#Obj().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Funcscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#)
536 if got, want := lock.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#Pkg().().Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Path().(), "sync"; got != want {
537 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("got package path %q; want %q", got, want)
538 }
539 }
540
541 func TestIssue13566scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestIssue13566().(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#) {
542 // This package only handles gc export data.
543 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
544 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) // to find stdlib export data in the build cache
545 skipWindowsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/skipWindows().(t)
546
547 tmpdir := mktmpdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/mktmpdir().(t)
548 defer osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(tmpdir)
549 testoutdir := 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().(tmpdir, "testdata")
550
551 // b.go needs to be compiled from the output directory so that the compiler can
552 // find the compiled package a. We pass the full path to compile() so that we
553 // don't have to copy the file to that directory.
554 bpath, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Absscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Abs().(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().("testdata", "b.go"))
555 if err != nil {
556 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
557 }
558
559 jsonExport, _, err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.FindPkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/FindPkg().("encoding/json", "testdata")
560 if jsonExport == "" {
561 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("no export data found for encoding/json: %s", err)
562 }
563
564 compilePkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compilePkg().(t, "testdata", "a.go", testoutdir, map[string]string{"encoding/json": jsonExport}, apkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/apkg().(testoutdir))
565 compilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compile().(t, testoutdir, bpath, testoutdir, map[string]string{apkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/apkg().(testoutdir): 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().(testoutdir, "a.o")})
566
567 // import must succeed (test for issue at hand)
568 pkg := importPkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importPkg().(t, "./testdata/b", tmpdir)
569
570 // make sure all indirectly imported packages have names
571 for _, imp := range pkg.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Imports().() {
572 if imp.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Name().() == "" {
573 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("no name for %s package", imp.Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Path().())
574 }
575 }
576 }
577
578 func TestIssue13898scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestIssue13898().(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#) {
579 // This package only handles gc export data.
580 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
581 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) // to find stdlib export data in the build cache
582
583 // import go/internal/gcimporter which imports go/types partially
584 imports := make(map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#)
585 _, err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.Importscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/Import().(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().(), imports, "go/internal/gcimporter", ".", nil)
586 if err != nil {
587 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
588 }
589
590 // look for go/types package
591 var goTypesPkg *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#
592 for path, pkg := range imports {
593 if path == "go/types" {
594 goTypesPkg = pkg
595 break
596 }
597 }
598 if goTypesPkg == nil {
599 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("go/types not found")
600 }
601
602 // look for go/types.Object type
603 obj := lookupObjscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/lookupObj().(t, goTypesPkg.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Scope().(), "Object")
604 typ, 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().(obj.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#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#)
605 if !ok {
606 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("go/types.Object type is %v; wanted named type", typ)
607 }
608
609 // lookup go/types.Object.Pkg method
610 m, index, indirect := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.LookupFieldOrMethodscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/LookupFieldOrMethod().(typ, false, nil, "Pkg")
611 if m == nil {
612 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("go/types.Object.Pkg not found (index = %v, indirect = %v)", index, indirect)
613 }
614
615 // the method must belong to go/types
616 if m.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pkg().().Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Path().() != "go/types" {
617 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("found %v; want go/types", m.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pkg().())
618 }
619 }
620
621 func TestIssue15517scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestIssue15517().(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#) {
622 // This package only handles gc export data.
623 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
624 skipWindowsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/skipWindows().(t)
625
626 tmpdir := mktmpdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/mktmpdir().(t)
627 defer osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(tmpdir)
628
629 compilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compile().(t, "testdata", "p.go", 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().(tmpdir, "testdata"), nil)
630
631 // Multiple imports of p must succeed without redeclaration errors.
632 // We use an import path that's not cleaned up so that the eventual
633 // file path for the package is different from the package path; this
634 // will expose the error if it is present.
635 //
636 // (Issue: Both the textual and the binary importer used the file path
637 // of the package to be imported as key into the shared packages map.
638 // However, the binary importer then used the package path to identify
639 // the imported package to mark it as complete; effectively marking the
640 // wrong package as complete. By using an "unclean" package path, the
641 // file and package path are different, exposing the problem if present.
642 // The same issue occurs with vendoring.)
643 imports := make(map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#)
644 for range 3 {
645 if _, err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.Importscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/Import().(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().(), imports, "./././testdata/p", tmpdir, nil); err != nil {
646 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
647 }
648 }
649 }
650
651 func TestIssue15920scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestIssue15920().(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#) {
652 // This package only handles gc export data.
653 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
654 skipWindowsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/skipWindows().(t)
655
656 compileAndImportPkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compileAndImportPkg().(t, "issue15920")
657 }
658
659 func TestIssue20046scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestIssue20046().(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#) {
660 // This package only handles gc export data.
661 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
662 skipWindowsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/skipWindows().(t)
663
664 // "./issue20046".V.M must exist
665 pkg := compileAndImportPkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compileAndImportPkg().(t, "issue20046")
666 obj := lookupObjscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/lookupObj().(t, pkg.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Scope().(), "V")
667 if m, index, indirect := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.LookupFieldOrMethodscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/LookupFieldOrMethod().(obj.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Type().(), false, nil, "M"); m == nil {
668 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("V.M not found (index = %v, indirect = %v)", index, indirect)
669 }
670 }
671
672 func TestIssue25301scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestIssue25301().(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#) {
673 // This package only handles gc export data.
674 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
675 skipWindowsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/skipWindows().(t)
676
677 compileAndImportPkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compileAndImportPkg().(t, "issue25301")
678 }
679
680 func TestIssue51836scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestIssue51836().(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#) {
681 // This package only handles gc export data.
682 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
683 skipWindowsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/skipWindows().(t)
684
685 tmpdir := mktmpdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/mktmpdir().(t)
686 defer osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(tmpdir)
687 testoutdir := 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().(tmpdir, "testdata")
688
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().("testdata", "issue51836")
690 // Following the pattern of TestIssue13898, aa.go needs to be compiled from
691 // the output directory. We pass the full path to compile() so that we don't
692 // have to copy the file to that directory.
693 aapath, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Absscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Abs().(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().(dir, "aa.go"))
694 if err != nil {
695 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
696 }
697 compilePkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compilePkg().(t, dir, "a.go", testoutdir, nil, apkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/apkg().(testoutdir))
698 compilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compile().(t, testoutdir, aapath, testoutdir, map[string]string{apkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/apkg().(testoutdir): 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().(testoutdir, "a.o")})
699
700 // import must succeed (test for issue at hand)
701 _ = importPkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importPkg().(t, "./testdata/aa", tmpdir)
702 }
703
704 func TestIssue61561scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestIssue61561().(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#) {
705 const src = `package p
706
707 type I[P any] interface {
708 m(P)
709 n() P
710 }
711
712 type J = I[int]
713
714 type StillBad[P any] *interface{b(P)}
715
716 type K = StillBad[string]
717 `
718 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().()
719 f, err := goparserscip-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)
720 if f == nil {
721 // Some test cases may have parse errors, but we must always have a
722 // file.
723 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("ParseFile returned nil file. Err: %v", err)
724 }
725
726 config := &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#{}
727 pkg1, err := config.Checkscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Check().("p", fset, []*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}, nil)
728 if err != nil {
729 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
730 }
731
732 // Export it. (Shallowness isn't important here.)
733 data, err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.IExportShallowscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/IExportShallow().(fset, pkg1, nil)
734 if err != nil {
735 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("export: %v", err) // any failure to export is a bug
736 }
737
738 // Re-import it.
739 imports := make(map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#)
740 pkg2, err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.IImportShallowscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/IImportShallow().(fset, gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.GetPackagesFromMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/GetPackagesFromMap().(imports), data, "p", nil)
741 if err != nil {
742 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("import: %v", err) // any failure of IExport+IImport is a bug.
743 }
744
745 insts := []typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#{
746 pkg2.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().("J").Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Type().(),
747 // This test is still racy, because the incomplete interface is contained
748 // within a nested type expression.
749 //
750 // Uncomment this once golang/go#61561 is fixed.
751 // pkg2.Scope().Lookup("K").Type().Underlying().(*types.Pointer).Elem(),
752 }
753
754 // Use the interface instances concurrently.
755 for _, inst := range insts {
756 var wg syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.WaitGroupscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#
757 for range 2 {
758 wg.Goscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#Go().(func() {
759 _ = 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().(inst)
760 })
761 }
762 wg.Waitscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#Wait().()
763 }
764 }
765
766 func TestIssue57015scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestIssue57015().(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 // This package only handles gc export data.
768 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
769 skipWindowsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/skipWindows().(t)
770
771 compileAndImportPkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compileAndImportPkg().(t, "issue57015")
772 }
773
774 // This is a regression test for a failure to export a package
775 // containing type errors.
776 //
777 // Though the issues and tests are specific, they may be representatives of a
778 // class of exporter bugs on ill-typed code that we have yet to flush out.
779 //
780 // TODO(adonovan): systematize our search for similar problems using
781 // fuzz testing.
782 func TestExportInvalidscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestExportInvalid().(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#) {
783
784 tests := []struct {
785 name string
786 src string
787 objName string
788 }{
789 // The lack of a receiver causes Recv.Type=Invalid.
790 // (The type checker then treats Foo as a package-level
791 // function, inserting it into the package scope.)
792 // The exporter needs to apply the same treatment.
793 {"issue 57729", `package p; func () Foo() {}`, "Foo"},
794
795 // It must be possible to export a constant with unknown kind, even if its
796 // type is known.
797 {"issue 60605", `package p; const EPSILON float64 = 1e-`, "EPSILON"},
798
799 // We must not crash when exporting a struct with unknown package.
800 {"issue 60891", `package p; type I[P any] int; const C I[struct{}] = 42`, "C"},
801 }
802
803 for _, test := range tests {
804 t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(test.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#) {
805 // Parse the ill-typed input.
806 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().()
807
808 f, err := goparserscip-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", test.src, 0)
809 if f == nil {
810 // Some test cases may have parse errors, but we must always have a
811 // file.
812 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("ParseFile returned nil file. Err: %v", err)
813 }
814
815 // Type check it, expecting errors.
816 config := &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#{
817 Errorscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Error.: func(err error) { t.Logscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Log().(err) }, // don't abort at first error
818 }
819 pkg1, _ := config.Checkscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Check().("p", fset, []*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}, nil)
820
821 // Export it.
822 // (Shallowness isn't important here.)
823 data, err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.IExportShallowscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/IExportShallow().(fset, pkg1, nil)
824 if err != nil {
825 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("export: %v", err) // any failure to export is a bug
826 }
827
828 // Re-import it.
829 imports := make(map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#)
830 pkg2, err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.IImportShallowscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/IImportShallow().(fset, gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.GetPackagesFromMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/GetPackagesFromMap().(imports), data, "p", nil)
831 if err != nil {
832 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("import: %v", err) // any failure of IExport+IImport is a bug.
833 }
834
835 // Check that the expected object is present in both packages.
836 // We can't assert the type hasn't changed: it may have, in some cases.
837 hasObj1 := pkg1.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().(test.objName) != nil
838 hasObj2 := pkg2.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().(test.objName) != nil
839 if hasObj1 != hasObj2 {
840 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("export+import changed Lookup(%q)!=nil: was %t, became %t", test.objName, hasObj1, hasObj2)
841 }
842 })
843 }
844 }
845
846 func TestIssue58296scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestIssue58296().(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#) {
847 // Compiles packages c, b, and a where c imports b and b imports a,
848 // then imports c with stub *types.Packages for b and a, and checks that
849 // both a and b are in the Imports() of c.
850 //
851 // This is how go/packages can read the exportdata when NeedDeps is off.
852
853 // This package only handles gc export data.
854 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
855 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) // to find stdlib export data in the build cache
856 skipWindowsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/skipWindows().(t)
857
858 tmpdir := mktmpdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/mktmpdir().(t)
859 defer osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(tmpdir)
860 testoutdir := 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().(tmpdir, "testdata")
861
862 apkg := 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().(testoutdir, "a")
863 bpkg := 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().(testoutdir, "b")
864 cpkg := 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().(testoutdir, "c")
865
866 srcdir := 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().("testdata", "issue58296")
867 compilePkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compilePkg().(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().(srcdir, "a"), "a.go", testoutdir, nil, apkg)
868 compilePkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compilePkg().(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().(srcdir, "b"), "b.go", testoutdir, map[string]string{apkg: 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().(testoutdir, "a.o")}, bpkg)
869 compilePkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compilePkg().(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().(srcdir, "c"), "c.go", testoutdir, map[string]string{bpkg: 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().(testoutdir, "b.o")}, cpkg)
870
871 // The export data reader for c cannot rely on Package.Imports
872 // being populated for a or b. (For the imports {a,b} it is unset.)
873 imports := map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#{
874 apkg: 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().(apkg, "a"),
875 bpkg: 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().(bpkg, "b"),
876 }
877
878 // make sure a and b are both imported by c.
879 pkg, err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.Importscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/Import().(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().(), imports, "./c", testoutdir, nil)
880 if err != nil {
881 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
882 }
883
884 var names []string
885 for _, imp := range pkg.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Imports().() {
886 names = append(names, imp.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Name().())
887 }
888 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().(names)
889
890 if got, want := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(names, ","), "a,b"; got != want {
891 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("got imports %v for package c. wanted %v", names, want)
892 }
893 }
894
895 func TestIssueAliasesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestIssueAliases().(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#) {
896 // This package only handles gc export data.
897 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, 24)
898 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
899 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) // to find stdlib export data in the build cache
900 skipWindowsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/skipWindows().(t)
901
902 tmpdir := mktmpdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/mktmpdir().(t)
903 defer osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(tmpdir)
904 testoutdir := 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().(tmpdir, "testdata")
905
906 apkg := 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().(testoutdir, "a")
907 bpkg := 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().(testoutdir, "b")
908 cpkg := 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().(testoutdir, "c")
909
910 // compile a, b and c into gc export data.
911 srcdir := 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().("testdata", "aliases")
912 compilePkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compilePkg().(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().(srcdir, "a"), "a.go", testoutdir, nil, apkg)
913 compilePkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compilePkg().(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().(srcdir, "b"), "b.go", testoutdir, map[string]string{apkg: 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().(testoutdir, "a.o")}, bpkg)
914 compilePkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compilePkg().(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().(srcdir, "c"), "c.go", testoutdir,
915 map[string]string{apkg: 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().(testoutdir, "a.o"), bpkg: 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().(testoutdir, "b.o")},
916 cpkg,
917 )
918
919 // import c from gc export data using a and b.
920 pkg, err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.Importscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/Import().(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().(), map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#{
921 apkg: 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().(apkg, "a"),
922 bpkg: 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().(bpkg, "b"),
923 }, "./c", testoutdir, nil)
924 if err != nil {
925 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
926 }
927
928 // Check c's objects and types.
929 var objs []string
930 for _, imp := range pkg.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Scope().().Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Names().() {
931 obj := pkg.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().(imp)
932 s := 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 : %s", obj.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Name().(), obj.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Type().())
933 s = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.0 strings/ReplaceAll().(s, testoutdir, "testdata")
934 objs = append(objs, s)
935 }
936 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().(objs)
937
938 want := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().([]string{
939 "S : struct{F int}",
940 "T : struct{F int}",
941 "U : testdata/a.A[string]",
942 "V : testdata/a.A[int]",
943 "W : testdata/b.B[string]",
944 "X : testdata/b.B[int]",
945 "Y : testdata/c.c[string]",
946 "Z : testdata/c.c[int]",
947 "c : testdata/c.c[V any]",
948 }, ",")
949 if got := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(objs, ","); got != want {
950 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("got imports %v for package c. wanted %v", objs, want)
951 }
952 }
953
954 // apkg returns the package "a" prefixed by (as a package) testoutdir
955 func apkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/apkg().(testoutdir string) string {
956 apkg := testoutdir + "/a"
957 if osscip-go gomod github.com/golang/go/src go1.25.0 os/.PathSeparatorscip-go gomod github.com/golang/go/src go1.25.0 os/PathSeparator. != '/' {
958 apkg = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.0 strings/ReplaceAll().(apkg, string(osscip-go gomod github.com/golang/go/src go1.25.0 os/.PathSeparatorscip-go gomod github.com/golang/go/src go1.25.0 os/PathSeparator.), "/")
959 }
960 return apkg
961 }
962
963 func importPkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importPkg().(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#, path, srcDir string) *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package# {
964 pkg, err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.Importscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/Import().(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().(), make(map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#), path, srcDir, nil)
965 if err != nil {
966 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
967 }
968 return pkg
969 }
970
971 func compileAndImportPkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compileAndImportPkg().(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#, name string) *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package# {
972 tmpdir := mktmpdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/mktmpdir().(t)
973 defer osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(tmpdir)
974 compilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compile().(t, "testdata", name+".go", 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().(tmpdir, "testdata"), nil)
975 return importPkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importPkg().(t, "./testdata/"+name, tmpdir)
976 }
977
978 func lookupObjscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/lookupObj().(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#, scope *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#, name string) typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object# {
979 if obj := scope.Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Lookup().(name); obj != nil {
980 return obj
981 }
982 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("%s not found", name)
983 return nil
984 }
985
986 // skipWindows skips the test on windows.
987 //
988 // On windows, we have to set the -D option for the compiler to avoid having a drive
989 // letter and an illegal ':' in the import path - just skip it (see also issue #3483).
990 func skipWindowsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/skipWindows().(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#) {
991 if runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOOS. == "windows" {
992 t.Skipscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skip().("avoid dealing with relative paths/drive letters on windows")
993 }
994 }
995
996 type importMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importMap# map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#
997
998 func (m importMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importMap#) Importscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importMap#Import().(path string) (*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#, error) { return m[path], nil }
999
1000 func TestIssue69912scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestIssue69912().(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#) {
1001 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().()
1002
1003 check := func(pkgname, src string, imports importMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importMap#) (*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#, error) {
1004 f, err := goparserscip-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, "a.go", src, 0)
1005 if err != nil {
1006 return nil, err
1007 }
1008 config := &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#{
1009 Importerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Importer.: imports,
1010 }
1011 return config.Checkscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Check().(pkgname, fset, []*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}, nil)
1012 }
1013
1014 const libSrc = `package lib
1015
1016 type T int
1017 `
1018
1019 lib, err := check("lib", libSrc, nil)
1020 if err != nil {
1021 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Checking lib: %v", err)
1022 }
1023
1024 // Export it.
1025 var out 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#
1026 if err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.IExportDatascip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/IExportData().(&out, fset, lib); err != nil {
1027 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("export: %v", err) // any failure to export is a bug
1028 }
1029
1030 // Re-import it.
1031 imports := make(map[string]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#)
1032 _, lib2, err := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.IImportDatascip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/IImportData().(fset, imports, out.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().(), "lib")
1033 if err != nil {
1034 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("import: %v", err) // any failure of export+import is a bug.
1035 }
1036
1037 // Use the resulting package concurrently, via dot-imports.
1038
1039 const pSrc = `package p
1040
1041 import . "lib"
1042
1043 type S struct {
1044 f T
1045 }
1046 `
1047 importer := importMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/importMap#{
1048 "lib": lib2,
1049 }
1050 var wg syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.WaitGroupscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#
1051 for range 10 {
1052 wg.Goscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#Go().(func() {
1053 _, err := check("p", pSrc, importer)
1054 if err != nil {
1055 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("check failed: %v", err)
1056 }
1057 })
1058 }
1059 wg.Waitscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#Wait().()
1060 }
1061
1062 func TestGenericMethodsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestGenericMethods().(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#) {
1063 t.SkipNowscip-go gomod github.com/golang/go/src go1.25.0 testing/common#SkipNow().() // TODO(mark): Turn on with UIR V4.
1064 // This package only handles gc export data.
1065 needsCompilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/needsCompiler().(t, "gc")
1066 skipWindowsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/skipWindows().(t)
1067
1068 tmpdir := mktmpdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/mktmpdir().(t)
1069 defer osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(tmpdir)
1070 testoutdir := 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().(tmpdir, "testdata")
1071
1072 // Following the pattern of TestIssue13898, b.go needs to be compiled from
1073 // the output directory. We pass the full path to compile() so that we don't
1074 // have to copy the file to that directory.
1075 srcdir := 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().("testdata", "genmeth")
1076 bpath, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Absscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Abs().(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().(srcdir, "b.go"))
1077 if err != nil {
1078 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
1079 }
1080
1081 compilePkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compilePkg().(t, srcdir, "a.go", testoutdir, nil, apkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/apkg().(testoutdir))
1082 compilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/compile().(t, testoutdir, bpath, testoutdir, map[string]string{apkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/apkg().(testoutdir): 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().(testoutdir, "a.o")})
1083 }