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