| 1 | // Copyright 2021 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package gcimporter_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/ |
| 6 | |
| 7 | // This file defines test of generics features introduce in go1.18. |
| 8 | |
| 9 | import ( |
| 10 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 11 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 12 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 13 | "go/importerscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/" |
| 14 | "go/parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/" |
| 15 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 16 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 17 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 18 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 19 | "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/" |
| 20 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 21 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 22 | |
| 23 | "golang.org/x/tools/internal/gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/" |
| 24 | "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/" |
| 25 | ) |
| 26 | |
| 27 | // TODO(rfindley): migrate this to testdata, as has been done in the standard library. |
| 28 | func TestGenericExportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestGenericExport().(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#) { |
| 29 | const src = ` |
| 30 | package generic |
| 31 | |
| 32 | type Any any |
| 33 | |
| 34 | type T[A, B any] struct { Left A; Right B } |
| 35 | |
| 36 | func (T[P, Q]) m() {} |
| 37 | |
| 38 | var X T[int, string] = T[int, string]{1, "hi"} |
| 39 | |
| 40 | func ToInt[P interface{ ~int }](p P) int { return int(p) } |
| 41 | |
| 42 | var IntID = ToInt[int] |
| 43 | |
| 44 | type G[C comparable] int |
| 45 | |
| 46 | func ImplicitFunc[T ~int]() {} |
| 47 | |
| 48 | type ImplicitType[T ~int] int |
| 49 | |
| 50 | // Exercise constant import/export |
| 51 | const C1 = 42 |
| 52 | const C2 int = 42 |
| 53 | const C3 float64 = 42 |
| 54 | |
| 55 | type Constraint[T any] interface { |
| 56 | m(T) |
| 57 | } |
| 58 | |
| 59 | // TODO(rfindley): revert to multiple blanks once the restriction on multiple |
| 60 | // blanks is removed from the type checker. |
| 61 | // type Blanks[_ any, _ Constraint[int]] int |
| 62 | // func (Blanks[_, _]) m() {} |
| 63 | type Blanks[_ any] int |
| 64 | func (Blanks[_]) m() {} |
| 65 | ` |
| 66 | testExportSrcscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testExportSrc().(t, []byte(src)) |
| 67 | } |
| 68 | |
| 69 | func testExportSrcscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testExportSrc().(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#, src []byte) { |
| 70 | // This package only handles gc export data. |
| 71 | 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" { |
| 72 | 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.) |
| 73 | } |
| 74 | 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) |
| 75 | |
| 76 | 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().() |
| 77 | f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, "g.go", src, 0) |
| 78 | if err != nil { |
| 79 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 80 | } |
| 81 | conf := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Configscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#{ |
| 82 | Importerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Importer.: importerscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/.Defaultscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/Default().(), |
| 83 | } |
| 84 | pkg, err := conf.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) |
| 85 | if err != nil { |
| 86 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 87 | } |
| 88 | |
| 89 | // export |
| 90 | version := gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.IExportVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/IExportVersion. |
| 91 | data, err := iexportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/iexport().(fset, version, pkg) |
| 92 | if err != nil { |
| 93 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 94 | } |
| 95 | |
| 96 | testPkgDatascip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testPkgData().(t, fset, version, pkg, data) |
| 97 | } |
| 98 | |
| 99 | func TestIndexedImportTypeparamTestsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestIndexedImportTypeparamTests().(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#) { |
| 100 | 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 |
| 101 | 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") |
| 102 | |
| 103 | // Check go files in test/typeparam. |
| 104 | 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") |
| 105 | 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) |
| 106 | if err != nil { |
| 107 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 108 | } |
| 109 | |
| 110 | for _, entry := range list { |
| 111 | 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") { |
| 112 | // For now, only consider standalone go files. |
| 113 | continue |
| 114 | } |
| 115 | |
| 116 | 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#) { |
| 117 | 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().()) |
| 118 | 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) |
| 119 | if err != nil { |
| 120 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 121 | } |
| 122 | |
| 123 | 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")) { |
| 124 | // We're bypassing the logic of run.go here, so be conservative about |
| 125 | // the files we consider in an attempt to make this test more robust to |
| 126 | // changes in test/typeparams. |
| 127 | t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("not detected as a run test") |
| 128 | } |
| 129 | |
| 130 | testExportSrcscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testExportSrc().(t, src) |
| 131 | }) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func TestRecursiveExport_Issue51219scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/TestRecursiveExport_Issue51219().(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#) { |
| 136 | const srca = ` |
| 137 | package a |
| 138 | |
| 139 | type Interaction[DataT InteractionDataConstraint] struct { |
| 140 | } |
| 141 | |
| 142 | type InteractionDataConstraint interface { |
| 143 | []byte | |
| 144 | UserCommandInteractionData |
| 145 | } |
| 146 | |
| 147 | type UserCommandInteractionData struct { |
| 148 | resolvedInteractionWithOptions |
| 149 | } |
| 150 | |
| 151 | type resolvedInteractionWithOptions struct { |
| 152 | Resolved Resolved |
| 153 | } |
| 154 | |
| 155 | type Resolved struct { |
| 156 | Users ResolvedData[User] |
| 157 | } |
| 158 | |
| 159 | type ResolvedData[T ResolvedDataConstraint] map[uint64]T |
| 160 | |
| 161 | type ResolvedDataConstraint interface { |
| 162 | User | Message |
| 163 | } |
| 164 | |
| 165 | type User struct{} |
| 166 | |
| 167 | type Message struct { |
| 168 | Interaction *Interaction[[]byte] |
| 169 | } |
| 170 | ` |
| 171 | |
| 172 | const srcb = ` |
| 173 | package b |
| 174 | |
| 175 | import ( |
| 176 | "a" |
| 177 | ) |
| 178 | |
| 179 | // InteractionRequest is an incoming request Interaction |
| 180 | type InteractionRequest[T a.InteractionDataConstraint] struct { |
| 181 | a.Interaction[T] |
| 182 | } |
| 183 | ` |
| 184 | |
| 185 | const srcp = ` |
| 186 | package p |
| 187 | |
| 188 | import ( |
| 189 | "b" |
| 190 | ) |
| 191 | |
| 192 | // ResponseWriterMock mocks corde's ResponseWriter interface |
| 193 | type ResponseWriterMock struct { |
| 194 | x b.InteractionRequest[[]byte] |
| 195 | } |
| 196 | ` |
| 197 | |
| 198 | importer := &testImporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testImporter#{ |
| 199 | srcscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testImporter#src.: map[string][]byte{ |
| 200 | "a": []byte(srca), |
| 201 | "b": []byte(srcb), |
| 202 | "p": []byte(srcp), |
| 203 | }, |
| 204 | pkgsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testImporter#pkgs.: 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#), |
| 205 | } |
| 206 | _, err := importer.Importscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testImporter#Import().("p") |
| 207 | if err != nil { |
| 208 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // testImporter is a helper to test chains of imports using export data. |
| 213 | type testImporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testImporter# struct { |
| 214 | srcscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testImporter#src. map[string][]byte // original source |
| 215 | pkgsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testImporter#pkgs. 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# // memoized imported packages |
| 216 | } |
| 217 | |
| 218 | func (t *testImporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testImporter#) Importscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testImporter#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) { |
| 219 | if pkg, ok := t.pkgsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testImporter#pkgs.[path]; ok { |
| 220 | return pkg, nil |
| 221 | } |
| 222 | src, ok := t.srcscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testImporter#src.[path] |
| 223 | if !ok { |
| 224 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("unknown path %v", path) |
| 225 | } |
| 226 | |
| 227 | // Type-check, but don't return this package directly. |
| 228 | 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().() |
| 229 | f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, path+".go", src, 0) |
| 230 | if err != nil { |
| 231 | return nil, err |
| 232 | } |
| 233 | conf := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Configscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#{ |
| 234 | Importerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Importer.: t, |
| 235 | } |
| 236 | pkg, err := conf.Checkscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Check().(path, 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) |
| 237 | if err != nil { |
| 238 | return nil, err |
| 239 | } |
| 240 | |
| 241 | // Export and import to get the package imported from export data. |
| 242 | exportdata, err := iexportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/iexport().(fset, gcimporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/.IExportVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter`/IExportVersion., pkg) |
| 243 | if err != nil { |
| 244 | return nil, err |
| 245 | } |
| 246 | 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#) |
| 247 | fset2 := 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().() |
| 248 | _, pkg2, 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().(fset2, imports, exportdata, pkg.Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Path().()) |
| 249 | if err != nil { |
| 250 | return nil, err |
| 251 | } |
| 252 | t.pkgsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gcimporter_test`/testImporter#pkgs.[path] = pkg2 |
| 253 | return pkg2, nil |
| 254 | } |