]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/go/types/api_test.go
go/types, types2: remove local version processing in favor of go/version
[gostls13.git] / src / go / types / api_test.go
index b154ad58524911ceed436598347864159d5ee2e0..594b92bb237f9217c42bd85b2561baddab6af017 100644 (file)
@@ -11,11 +11,13 @@ import (
        "go/importer"
        "go/parser"
        "go/token"
+       "internal/goversion"
        "internal/testenv"
        "reflect"
        "regexp"
        "sort"
        "strings"
+       "sync"
        "testing"
 
        . "go/types"
@@ -24,24 +26,17 @@ import (
 // nopos indicates an unknown position
 var nopos token.Pos
 
-func parse(fset *token.FileSet, filename, src string) (*ast.File, error) {
-       return parser.ParseFile(fset, filename, src, 0)
-}
-
-func mustParse(fset *token.FileSet, filename, src string) *ast.File {
-       f, err := parse(fset, filename, src)
+func mustParse(fset *token.FileSet, src string) *ast.File {
+       f, err := parser.ParseFile(fset, pkgName(src), src, 0)
        if err != nil {
                panic(err) // so we don't need to pass *testing.T
        }
        return f
 }
 
-func typecheck(path, src string, conf *Config, info *Info) (*Package, error) {
+func typecheck(src string, conf *Config, info *Info) (*Package, error) {
        fset := token.NewFileSet()
-       f, err := parse(fset, path, src)
-       if f == nil { // ignore errors unless f is nil
-               return nil, err
-       }
+       f := mustParse(fset, src)
        if conf == nil {
                conf = &Config{
                        Error:    func(err error) {}, // collect all errors
@@ -51,14 +46,28 @@ func typecheck(path, src string, conf *Config, info *Info) (*Package, error) {
        return conf.Check(f.Name.Name, fset, []*ast.File{f}, info)
 }
 
-func mustTypecheck(path, src string, conf *Config, info *Info) *Package {
-       pkg, err := typecheck(path, src, conf, info)
+func mustTypecheck(src string, conf *Config, info *Info) *Package {
+       pkg, err := typecheck(src, conf, info)
        if err != nil {
                panic(err) // so we don't need to pass *testing.T
        }
        return pkg
 }
 
+// pkgName extracts the package name from src, which must contain a package header.
+func pkgName(src string) string {
+       const kw = "package "
+       if i := strings.Index(src, kw); i >= 0 {
+               after := src[i+len(kw):]
+               n := len(after)
+               if i := strings.IndexAny(after, "\n\t ;/"); i >= 0 {
+                       n = i
+               }
+               return after[:n]
+       }
+       panic("missing package header: " + src)
+}
+
 func TestValuesInfo(t *testing.T) {
        var tests = []struct {
                src  string
@@ -134,15 +143,15 @@ func TestValuesInfo(t *testing.T) {
                {`package f6b; var _            =  1e-2000i`, `1e-2000i`, `complex128`, `(0 + 0i)`},
                {`package f7b; var _            = -1e-2000i`, `-1e-2000i`, `complex128`, `(0 + 0i)`},
 
-               {`package g0; const (a = len([iota]int{}); b; c); const _ = c`, `c`, `int`, `2`}, // issue #22341
-               {`package g1; var(j int32; s int; n = 1.0<<s == j)`, `1.0`, `int32`, `1`},        // issue #48422
+               {`package g0; const (a = len([iota]int{}); b; c); const _ = c`, `c`, `int`, `2`}, // go.dev/issue/22341
+               {`package g1; var(j int32; s int; n = 1.0<<s == j)`, `1.0`, `int32`, `1`},        // go.dev/issue/48422
        }
 
        for _, test := range tests {
                info := Info{
                        Types: make(map[ast.Expr]TypeAndValue),
                }
-               name := mustTypecheck("ValuesInfo", test.src, nil, &info).Name()
+               name := mustTypecheck(test.src, nil, &info).Name()
 
                // look for expression
                var expr ast.Expr
@@ -248,7 +257,7 @@ func TestTypesInfo(t *testing.T) {
                        `(string, bool)`,
                },
 
-               // issue 6796
+               // go.dev/issue/6796
                {`package issue6796_a; var x interface{}; var _, _ = (x.(int))`,
                        `x.(int)`,
                        `(int, bool)`,
@@ -270,7 +279,7 @@ func TestTypesInfo(t *testing.T) {
                        `(string, bool)`,
                },
 
-               // issue 7060
+               // go.dev/issue/7060
                {`package issue7060_a; var ( m map[int]string; x, ok = m[0] )`,
                        `m[0]`,
                        `(string, bool)`,
@@ -296,7 +305,7 @@ func TestTypesInfo(t *testing.T) {
                        `(string, bool)`,
                },
 
-               // issue 28277
+               // go.dev/issue/28277
                {`package issue28277_a; func f(...int)`,
                        `...int`,
                        `[]int`,
@@ -306,7 +315,7 @@ func TestTypesInfo(t *testing.T) {
                        `[][]struct{}`,
                },
 
-               // issue 47243
+               // go.dev/issue/47243
                {`package issue47243_a; var x int32; var _ = x << 3`, `3`, `untyped int`},
                {`package issue47243_b; var x int32; var _ = x << 3.`, `3.`, `untyped float`},
                {`package issue47243_c; var x int32; var _ = 1 << x`, `1 << x`, `int`},
@@ -318,10 +327,10 @@ func TestTypesInfo(t *testing.T) {
                {`package issue47243_i; var x int32; var _ = 1 << (2 << x)`, `(2 << x)`, `untyped int`},
                {`package issue47243_j; var x int32; var _ = 1 << (2 << x)`, `2`, `untyped int`},
 
-               // tests for broken code that doesn't parse or type-check
+               // tests for broken code that doesn't type-check
                {broken + `x0; func _() { var x struct {f string}; x.f := 0 }`, `x.f`, `string`},
                {broken + `x1; func _() { var z string; type x struct {f string}; y := &x{q: z}}`, `z`, `string`},
-               {broken + `x2; func _() { var a, b string; type x struct {f string}; z := &x{f: a; f: b;}}`, `b`, `string`},
+               {broken + `x2; func _() { var a, b string; type x struct {f string}; z := &x{f: a, f: b,}}`, `b`, `string`},
                {broken + `x3; var x = panic("");`, `panic`, `func(interface{})`},
                {`package x4; func _() { panic("") }`, `panic`, `func(interface{})`},
                {broken + `x5; func _() { var x map[string][...]int; x = map[string][...]int{"": {1,2,3}} }`, `x`, `map[string]invalid type`},
@@ -344,13 +353,13 @@ func TestTypesInfo(t *testing.T) {
                // instantiated types must be sanitized
                {`package g0; type t[P any] int; var x struct{ f t[int] }; var _ = x.f`, `x.f`, `g0.t[int]`},
 
-               // issue 45096
+               // go.dev/issue/45096
                {`package issue45096; func _[T interface{ ~int8 | ~int16 | ~int32  }](x T) { _ = x < 0 }`, `0`, `T`},
 
-               // issue 47895
+               // go.dev/issue/47895
                {`package p; import "unsafe"; type S struct { f int }; var s S; var _ = unsafe.Offsetof(s.f)`, `s.f`, `int`},
 
-               // issue 50093
+               // go.dev/issue/50093
                {`package u0a; func _[_ interface{int}]() {}`, `int`, `int`},
                {`package u1a; func _[_ interface{~int}]() {}`, `~int`, `~int`},
                {`package u2a; func _[_ interface{int | string}]() {}`, `int | string`, `int | string`},
@@ -374,13 +383,33 @@ func TestTypesInfo(t *testing.T) {
                {`package u3c; type _ interface{int | string | ~bool}`, `int | string`, `int | string`},
                {`package u3c; type _ interface{int | string | ~bool}`, `~bool`, `~bool`},
                {`package u3c; type _ interface{int | string | ~float64|~bool}`, `int | string | ~float64`, `int | string | ~float64`},
+
+               // reverse type inference
+               {`package r1; var _ func(int) = g; func g[P any](P) {}`, `g`, `func(int)`},
+               {`package r2; var _ func(int) = g[int]; func g[P any](P) {}`, `g`, `func[P any](P)`}, // go.dev/issues/60212
+               {`package r3; var _ func(int) = g[int]; func g[P any](P) {}`, `g[int]`, `func(int)`},
+               {`package r4; var _ func(int, string) = g; func g[P, Q any](P, Q) {}`, `g`, `func(int, string)`},
+               {`package r5; var _ func(int, string) = g[int]; func g[P, Q any](P, Q) {}`, `g`, `func[P, Q any](P, Q)`}, // go.dev/issues/60212
+               {`package r6; var _ func(int, string) = g[int]; func g[P, Q any](P, Q) {}`, `g[int]`, `func(int, string)`},
+
+               {`package s1; func _() { f(g) }; func f(func(int)) {}; func g[P any](P) {}`, `g`, `func(int)`},
+               {`package s2; func _() { f(g[int]) }; func f(func(int)) {}; func g[P any](P) {}`, `g`, `func[P any](P)`}, // go.dev/issues/60212
+               {`package s3; func _() { f(g[int]) }; func f(func(int)) {}; func g[P any](P) {}`, `g[int]`, `func(int)`},
+               {`package s4; func _() { f(g) }; func f(func(int, string)) {}; func g[P, Q any](P, Q) {}`, `g`, `func(int, string)`},
+               {`package s5; func _() { f(g[int]) }; func f(func(int, string)) {}; func g[P, Q any](P, Q) {}`, `g`, `func[P, Q any](P, Q)`}, // go.dev/issues/60212
+               {`package s6; func _() { f(g[int]) }; func f(func(int, string)) {}; func g[P, Q any](P, Q) {}`, `g[int]`, `func(int, string)`},
+
+               {`package s7; func _() { f(g, h) }; func f[P any](func(int, P), func(P, string)) {}; func g[P any](P, P) {}; func h[P, Q any](P, Q) {}`, `g`, `func(int, int)`},
+               {`package s8; func _() { f(g, h) }; func f[P any](func(int, P), func(P, string)) {}; func g[P any](P, P) {}; func h[P, Q any](P, Q) {}`, `h`, `func(int, string)`},
+               {`package s9; func _() { f(g, h[int]) }; func f[P any](func(int, P), func(P, string)) {}; func g[P any](P, P) {}; func h[P, Q any](P, Q) {}`, `h`, `func[P, Q any](P, Q)`}, // go.dev/issues/60212
+               {`package s10; func _() { f(g, h[int]) }; func f[P any](func(int, P), func(P, string)) {}; func g[P any](P, P) {}; func h[P, Q any](P, Q) {}`, `h[int]`, `func(int, string)`},
        }
 
        for _, test := range tests {
                info := Info{Types: make(map[ast.Expr]TypeAndValue)}
                var name string
                if strings.HasPrefix(test.src, broken) {
-                       pkg, err := typecheck("TypesInfo", test.src, nil, &info)
+                       pkg, err := typecheck(test.src, nil, &info)
                        if err == nil {
                                t.Errorf("package %s: expected to fail but passed", pkg.Name())
                                continue
@@ -389,7 +418,7 @@ func TestTypesInfo(t *testing.T) {
                                name = pkg.Name()
                        }
                } else {
-                       name = mustTypecheck("TypesInfo", test.src, nil, &info).Name()
+                       name = mustTypecheck(test.src, nil, &info).Name()
                }
 
                // look for expression type
@@ -407,7 +436,7 @@ func TestTypesInfo(t *testing.T) {
 
                // check that type is correct
                if got := typ.String(); got != test.typ {
-                       t.Errorf("package %s: got %s; want %s", name, got, test.typ)
+                       t.Errorf("package %s: expr = %s: got %s; want %s", name, test.expr, got, test.typ)
                }
        }
 }
@@ -543,18 +572,62 @@ type T[P any] []P
                {`package issue51803; func foo[T any](T) {}; func _() { foo[int]( /* leave arg away on purpose */ ) }`,
                        []testInst{{`foo`, []string{`int`}, `func(int)`}},
                },
+
+               // reverse type inference
+               {`package reverse1a; var f func(int) = g; func g[P any](P) {}`,
+                       []testInst{{`g`, []string{`int`}, `func(int)`}},
+               },
+               {`package reverse1b; func f(func(int)) {}; func g[P any](P) {}; func _() { f(g) }`,
+                       []testInst{{`g`, []string{`int`}, `func(int)`}},
+               },
+               {`package reverse2a; var f func(int, string) = g; func g[P, Q any](P, Q) {}`,
+                       []testInst{{`g`, []string{`int`, `string`}, `func(int, string)`}},
+               },
+               {`package reverse2b; func f(func(int, string)) {}; func g[P, Q any](P, Q) {}; func _() { f(g) }`,
+                       []testInst{{`g`, []string{`int`, `string`}, `func(int, string)`}},
+               },
+               {`package reverse2c; func f(func(int, string)) {}; func g[P, Q any](P, Q) {}; func _() { f(g[int]) }`,
+                       []testInst{{`g`, []string{`int`, `string`}, `func(int, string)`}},
+               },
+               // reverse3a not possible (cannot assign to generic function outside of argument passing)
+               {`package reverse3b; func f[R any](func(int) R) {}; func g[P any](P) string { return "" }; func _() { f(g) }`,
+                       []testInst{
+                               {`f`, []string{`string`}, `func(func(int) string)`},
+                               {`g`, []string{`int`}, `func(int) string`},
+                       },
+               },
+               {`package reverse4a; var _, _ func([]int, *float32) = g, h; func g[P, Q any]([]P, *Q) {}; func h[R any]([]R, *float32) {}`,
+                       []testInst{
+                               {`g`, []string{`int`, `float32`}, `func([]int, *float32)`},
+                               {`h`, []string{`int`}, `func([]int, *float32)`},
+                       },
+               },
+               {`package reverse4b; func f(_, _ func([]int, *float32)) {}; func g[P, Q any]([]P, *Q) {}; func h[R any]([]R, *float32) {}; func _() { f(g, h) }`,
+                       []testInst{
+                               {`g`, []string{`int`, `float32`}, `func([]int, *float32)`},
+                               {`h`, []string{`int`}, `func([]int, *float32)`},
+                       },
+               },
+               {`package issue59956; func f(func(int), func(string), func(bool)) {}; func g[P any](P) {}; func _() { f(g, g, g) }`,
+                       []testInst{
+                               {`g`, []string{`int`}, `func(int)`},
+                               {`g`, []string{`string`}, `func(string)`},
+                               {`g`, []string{`bool`}, `func(bool)`},
+                       },
+               },
        }
 
        for _, test := range tests {
                imports := make(testImporter)
-               conf := Config{
-                       Importer: imports,
-                       Error:    func(error) {}, // ignore errors
-               }
+               conf := Config{Importer: imports}
                instMap := make(map[*ast.Ident]Instance)
                useMap := make(map[*ast.Ident]Object)
                makePkg := func(src string) *Package {
-                       pkg, _ := typecheck("p.go", src, &conf, &Info{Instances: instMap, Uses: useMap})
+                       pkg, err := typecheck(src, &conf, &Info{Instances: instMap, Uses: useMap})
+                       // allow error for issue51803
+                       if err != nil && (pkg == nil || pkg.Name() != "issue51803") {
+                               t.Fatal(err)
+                       }
                        imports[pkg.Name()] = pkg
                        return pkg
                }
@@ -649,7 +722,7 @@ func TestDefsInfo(t *testing.T) {
                info := Info{
                        Defs: make(map[*ast.Ident]Object),
                }
-               name := mustTypecheck("DefsInfo", test.src, nil, &info).Name()
+               name := mustTypecheck(test.src, nil, &info).Name()
 
                // find object
                var def Object
@@ -716,7 +789,7 @@ func TestUsesInfo(t *testing.T) {
                info := Info{
                        Uses: make(map[*ast.Ident]Object),
                }
-               name := mustTypecheck("UsesInfo", test.src, nil, &info).Name()
+               name := mustTypecheck(test.src, nil, &info).Name()
 
                // find object
                var use Object
@@ -749,7 +822,7 @@ func (r N[B]) m() { r.m(); r.n() }
 func (r *N[C]) n() {  }
 `
        fset := token.NewFileSet()
-       f := mustParse(fset, "p.go", src)
+       f := mustParse(fset, src)
        info := Info{
                Defs:       make(map[*ast.Ident]Object),
                Uses:       make(map[*ast.Ident]Object),
@@ -857,7 +930,7 @@ func TestImplicitsInfo(t *testing.T) {
                info := Info{
                        Implicits: make(map[ast.Node]Object),
                }
-               name := mustTypecheck("ImplicitsInfo", test.src, nil, &info).Name()
+               name := mustTypecheck(test.src, nil, &info).Name()
 
                // the test cases expect at most one Implicits entry
                if len(info.Implicits) > 1 {
@@ -888,6 +961,81 @@ func TestImplicitsInfo(t *testing.T) {
        }
 }
 
+func TestPkgNameOf(t *testing.T) {
+       testenv.MustHaveGoBuild(t)
+
+       const src = `
+package p
+
+import (
+       . "os"
+       _ "io"
+       "math"
+       "path/filepath"
+       snort "sort"
+)
+
+// avoid imported and not used errors
+var (
+       _ = Open // os.Open
+       _ = math.Sin
+       _ = filepath.Abs
+       _ = snort.Ints
+)
+`
+
+       var tests = []struct {
+               path string // path string enclosed in "'s
+               want string
+       }{
+               {`"os"`, "."},
+               {`"io"`, "_"},
+               {`"math"`, "math"},
+               {`"path/filepath"`, "filepath"},
+               {`"sort"`, "snort"},
+       }
+
+       fset := token.NewFileSet()
+       f := mustParse(fset, src)
+       info := Info{
+               Defs:      make(map[*ast.Ident]Object),
+               Implicits: make(map[ast.Node]Object),
+       }
+       var conf Config
+       conf.Importer = importer.Default()
+       _, err := conf.Check("p", fset, []*ast.File{f}, &info)
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       // map import paths to importDecl
+       imports := make(map[string]*ast.ImportSpec)
+       for _, s := range f.Decls[0].(*ast.GenDecl).Specs {
+               if imp, _ := s.(*ast.ImportSpec); imp != nil {
+                       imports[imp.Path.Value] = imp
+               }
+       }
+
+       for _, test := range tests {
+               imp := imports[test.path]
+               if imp == nil {
+                       t.Fatalf("invalid test case: import path %s not found", test.path)
+               }
+               got := info.PkgNameOf(imp)
+               if got == nil {
+                       t.Fatalf("import %s: package name not found", test.path)
+               }
+               if got.Name() != test.want {
+                       t.Errorf("import %s: got %s; want %s", test.path, got.Name(), test.want)
+               }
+       }
+
+       // test non-existing importDecl
+       if got := info.PkgNameOf(new(ast.ImportSpec)); got != nil {
+               t.Errorf("got %s for non-existing import declaration", got.Name())
+       }
+}
+
 func predString(tv TypeAndValue) string {
        var buf strings.Builder
        pred := func(b bool, s string) {
@@ -985,7 +1133,7 @@ func TestPredicatesInfo(t *testing.T) {
 
        for _, test := range tests {
                info := Info{Types: make(map[ast.Expr]TypeAndValue)}
-               name := mustTypecheck("PredicatesInfo", test.src, nil, &info).Name()
+               name := mustTypecheck(test.src, nil, &info).Name()
 
                // look for expression predicates
                got := "<missing>"
@@ -1077,7 +1225,7 @@ func TestScopesInfo(t *testing.T) {
 
        for _, test := range tests {
                info := Info{Scopes: make(map[ast.Node]*Scope)}
-               name := mustTypecheck("ScopesInfo", test.src, nil, &info).Name()
+               name := mustTypecheck(test.src, nil, &info).Name()
 
                // number of scopes must match
                if len(info.Scopes) != len(test.scopes) {
@@ -1188,7 +1336,7 @@ func TestInitOrderInfo(t *testing.T) {
                }`, []string{
                        "d = 3", "b = f()", "c = f()", "a = c + b",
                }},
-               // test case for issue 7131
+               // test case for go.dev/issue/7131
                {`package main
 
                var counter int
@@ -1203,7 +1351,7 @@ func TestInitOrderInfo(t *testing.T) {
                `, []string{
                        "a = next()", "b = next()", "c = next()", "d = next()", "e = next()", "f = next()", "_ = makeOrder()",
                }},
-               // test case for issue 10709
+               // test case for go.dev/issue/10709
                {`package p13
 
                var (
@@ -1223,7 +1371,7 @@ func TestInitOrderInfo(t *testing.T) {
                }`, []string{
                        "t = makeT(0)", "v = t.m()",
                }},
-               // test case for issue 10709: same as test before, but variable decls swapped
+               // test case for go.dev/issue/10709: same as test before, but variable decls swapped
                {`package p14
 
                var (
@@ -1243,7 +1391,7 @@ func TestInitOrderInfo(t *testing.T) {
                }`, []string{
                        "t = makeT(0)", "v = t.m()",
                }},
-               // another candidate possibly causing problems with issue 10709
+               // another candidate possibly causing problems with go.dev/issue/10709
                {`package p15
 
                var y1 = f1()
@@ -1265,7 +1413,7 @@ func TestInitOrderInfo(t *testing.T) {
 
        for _, test := range tests {
                info := Info{}
-               name := mustTypecheck("InitOrderInfo", test.src, nil, &info).Name()
+               name := mustTypecheck(test.src, nil, &info).Name()
 
                // number of initializers must match
                if len(info.InitOrder) != len(test.inits) {
@@ -1286,8 +1434,8 @@ func TestInitOrderInfo(t *testing.T) {
 
 func TestMultiFileInitOrder(t *testing.T) {
        fset := token.NewFileSet()
-       fileA := mustParse(fset, "", `package main; var a = 1`)
-       fileB := mustParse(fset, "", `package main; var b = 2`)
+       fileA := mustParse(fset, `package main; var a = 1`)
+       fileB := mustParse(fset, `package main; var b = 2`)
 
        // The initialization order must not depend on the parse
        // order of the files, only on the presentation order to
@@ -1323,10 +1471,8 @@ func TestFiles(t *testing.T) {
        var info Info
        check := NewChecker(&conf, fset, pkg, &info)
 
-       for i, src := range sources {
-               filename := fmt.Sprintf("sources%d", i)
-               f := mustParse(fset, filename, src)
-               if err := check.Files([]*ast.File{f}); err != nil {
+       for _, src := range sources {
+               if err := check.Files([]*ast.File{mustParse(fset, src)}); err != nil {
                        t.Error(err)
                }
        }
@@ -1361,8 +1507,7 @@ func TestSelection(t *testing.T) {
        imports := make(testImporter)
        conf := Config{Importer: imports}
        makePkg := func(path, src string) {
-               f := mustParse(fset, path+".go", src)
-               pkg, err := conf.Check(path, fset, []*ast.File{f}, &Info{Selections: selections})
+               pkg, err := conf.Check(path, fset, []*ast.File{mustParse(fset, src)}, &Info{Selections: selections})
                if err != nil {
                        t.Fatal(err)
                }
@@ -1539,9 +1684,7 @@ func TestIssue8518(t *testing.T) {
                Importer: imports,
        }
        makePkg := func(path, src string) {
-               f := mustParse(fset, path, src)
-               pkg, _ := conf.Check(path, fset, []*ast.File{f}, nil) // errors logged via conf.Error
-               imports[path] = pkg
+               imports[path], _ = conf.Check(path, fset, []*ast.File{mustParse(fset, src)}, nil) // errors logged via conf.Error
        }
 
        const libSrc = `
@@ -1562,6 +1705,32 @@ var _ = a.C2
        makePkg("main", mainSrc) // don't crash when type-checking this package
 }
 
+func TestIssue59603(t *testing.T) {
+       fset := token.NewFileSet()
+       imports := make(testImporter)
+       conf := Config{
+               Error:    func(err error) { t.Log(err) }, // don't exit after first error
+               Importer: imports,
+       }
+       makePkg := func(path, src string) {
+               imports[path], _ = conf.Check(path, fset, []*ast.File{mustParse(fset, src)}, nil) // errors logged via conf.Error
+       }
+
+       const libSrc = `
+package a
+const C = foo
+`
+
+       const mainSrc = `
+package main
+import "a"
+const _ = a.C
+`
+
+       makePkg("a", libSrc)
+       makePkg("main", mainSrc) // don't crash when type-checking this package
+}
+
 func TestLookupFieldOrMethodOnNil(t *testing.T) {
        // LookupFieldOrMethod on a nil type is expected to produce a run-time panic.
        defer func() {
@@ -1623,13 +1792,13 @@ func TestLookupFieldOrMethod(t *testing.T) {
                // outside method set of a generic type
                {"var x T[int]; type T[P any] struct{}; func (*T[P]) f() {}", false, nil, true},
 
-               // recursive generic types; see golang/go#52715
+               // recursive generic types; see go.dev/issue/52715
                {"var a T[int]; type ( T[P any] struct { *N[P] }; N[P any] struct { *T[P] } ); func (N[P]) f() {}", true, []int{0, 0}, true},
                {"var a T[int]; type ( T[P any] struct { *N[P] }; N[P any] struct { *T[P] } ); func (T[P]) f() {}", true, []int{0}, false},
        }
 
        for _, test := range tests {
-               pkg := mustTypecheck("test", "package p;"+test.src, nil, nil)
+               pkg := mustTypecheck("package p;"+test.src, nil, nil)
 
                obj := pkg.Scope().Lookup("a")
                if obj == nil {
@@ -1656,7 +1825,7 @@ func TestLookupFieldOrMethod(t *testing.T) {
        }
 }
 
-// Test for golang/go#52715
+// Test for go.dev/issue/52715
 func TestLookupFieldOrMethod_RecursiveGeneric(t *testing.T) {
        const src = `
 package pkg
@@ -1675,7 +1844,7 @@ type Instance = *Tree[int]
 `
 
        fset := token.NewFileSet()
-       f := mustParse(fset, "foo.go", src)
+       f := mustParse(fset, src)
        pkg := NewPackage("pkg", f.Name.Name)
        if err := NewChecker(nil, fset, pkg, nil).Files([]*ast.File{f}); err != nil {
                panic(err)
@@ -1712,7 +1881,7 @@ func TestScopeLookupParent(t *testing.T) {
                }
        }
 
-       makePkg("lib", mustParse(fset, "", "package lib; var X int"))
+       makePkg("lib", mustParse(fset, "package lib; var X int"))
        // Each /*name=kind:line*/ comment makes the test look up the
        // name at that point and checks that it resolves to a decl of
        // the specified kind and line number.  "undef" means undefined.
@@ -1756,7 +1925,7 @@ func F(){
 `
 
        info.Uses = make(map[*ast.Ident]Object)
-       f := mustParse(fset, "", mainSrc)
+       f := mustParse(fset, mainSrc)
        makePkg("main", f)
        mainScope := imports["main"].Scope()
        rx := regexp.MustCompile(`^/\*(\w*)=([\w:]*)\*/$`)
@@ -1898,7 +2067,7 @@ func TestIdentical(t *testing.T) {
                {`func X(int) string { return "" }; func Y(int) {}`, false},
 
                // Generic functions. Type parameters should be considered identical modulo
-               // renaming. See also issue #49722.
+               // renaming. See also go.dev/issue/49722.
                {`func X[P ~int](){}; func Y[Q ~int]() {}`, true},
                {`func X[P1 any, P2 ~*P1](){}; func Y[Q1 any, Q2 ~*Q1]() {}`, true},
                {`func X[P1 any, P2 ~[]P1](){}; func Y[Q1 any, Q2 ~*Q1]() {}`, false},
@@ -1908,7 +2077,7 @@ func TestIdentical(t *testing.T) {
        }
 
        for _, test := range tests {
-               pkg := mustTypecheck("test", "package p;"+test.src, nil, nil)
+               pkg := mustTypecheck("package p;"+test.src, nil, nil)
                X := pkg.Scope().Lookup("X")
                Y := pkg.Scope().Lookup("Y")
                if X == nil || Y == nil {
@@ -1979,10 +2148,33 @@ func TestIdenticalUnions(t *testing.T) {
        }
 }
 
+func TestIssue61737(t *testing.T) {
+       // This test verifies that it is possible to construct invalid interfaces
+       // containing duplicate methods using the go/types API.
+       //
+       // It must be possible for importers to construct such invalid interfaces.
+       // Previously, this panicked.
+
+       sig1 := NewSignatureType(nil, nil, nil, NewTuple(NewParam(nopos, nil, "", Typ[Int])), nil, false)
+       sig2 := NewSignatureType(nil, nil, nil, NewTuple(NewParam(nopos, nil, "", Typ[String])), nil, false)
+
+       methods := []*Func{
+               NewFunc(nopos, nil, "M", sig1),
+               NewFunc(nopos, nil, "M", sig2),
+       }
+
+       embeddedMethods := []*Func{
+               NewFunc(nopos, nil, "M", sig2),
+       }
+       embedded := NewInterfaceType(embeddedMethods, nil)
+       iface := NewInterfaceType(methods, []Type{embedded})
+       iface.Complete()
+}
+
 func TestIssue15305(t *testing.T) {
        const src = "package p; func f() int16; var _ = f(undef)"
        fset := token.NewFileSet()
-       f := mustParse(fset, "issue15305.go", src)
+       f := mustParse(fset, src)
        conf := Config{
                Error: func(err error) {}, // allow errors
        }
@@ -2005,20 +2197,20 @@ func TestIssue15305(t *testing.T) {
 // types for composite literal expressions and composite literal type
 // expressions.
 func TestCompositeLitTypes(t *testing.T) {
-       for _, test := range []struct {
+       for i, test := range []struct {
                lit, typ string
        }{
                {`[16]byte{}`, `[16]byte`},
-               {`[...]byte{}`, `[0]byte`},                // test for issue #14092
-               {`[...]int{1, 2, 3}`, `[3]int`},           // test for issue #14092
-               {`[...]int{90: 0, 98: 1, 2}`, `[100]int`}, // test for issue #14092
+               {`[...]byte{}`, `[0]byte`},                // test for go.dev/issue/14092
+               {`[...]int{1, 2, 3}`, `[3]int`},           // test for go.dev/issue/14092
+               {`[...]int{90: 0, 98: 1, 2}`, `[100]int`}, // test for go.dev/issue/14092
                {`[]int{}`, `[]int`},
                {`map[string]bool{"foo": true}`, `map[string]bool`},
                {`struct{}{}`, `struct{}`},
                {`struct{x, y int; z complex128}{}`, `struct{x int; y int; z complex128}`},
        } {
                fset := token.NewFileSet()
-               f := mustParse(fset, test.lit, "package p; var _ = "+test.lit)
+               f := mustParse(fset, fmt.Sprintf("package p%d; var _ = %s", i, test.lit))
                types := make(map[ast.Expr]TypeAndValue)
                if _, err := new(Config).Check("p", fset, []*ast.File{f}, &Info{Types: types}); err != nil {
                        t.Fatalf("%s: %v", test.lit, err)
@@ -2073,7 +2265,7 @@ func f(x int) { y := x; print(y) }
 `
 
        fset := token.NewFileSet()
-       f := mustParse(fset, "src", src)
+       f := mustParse(fset, src)
 
        info := &Info{
                Defs: make(map[*ast.Ident]Object),
@@ -2132,7 +2324,7 @@ var v T = c
 func f(x T) T { return foo.F(x) }
 `
        fset := token.NewFileSet()
-       f := mustParse(fset, "src", src)
+       f := mustParse(fset, src)
        files := []*ast.File{f}
 
        // type-check using all possible importers
@@ -2187,7 +2379,7 @@ func f(x T) T { return foo.F(x) }
 func TestInstantiate(t *testing.T) {
        // eventually we like more tests but this is a start
        const src = "package p; type T[P any] *T[P]"
-       pkg := mustTypecheck(".", src, nil, nil)
+       pkg := mustTypecheck(src, nil, nil)
 
        // type T should have one type parameter
        T := pkg.Scope().Lookup("T").Type().(*Named)
@@ -2208,6 +2400,60 @@ func TestInstantiate(t *testing.T) {
        }
 }
 
+func TestInstantiateConcurrent(t *testing.T) {
+       const src = `package p
+
+type I[P any] interface {
+       m(P)
+       n() P
+}
+
+type J = I[int]
+
+type Nested[P any] *interface{b(P)}
+
+type K = Nested[string]
+`
+       pkg := mustTypecheck(src, nil, nil)
+
+       insts := []*Interface{
+               pkg.Scope().Lookup("J").Type().Underlying().(*Interface),
+               pkg.Scope().Lookup("K").Type().Underlying().(*Pointer).Elem().(*Interface),
+       }
+
+       // Use the interface instances concurrently.
+       for _, inst := range insts {
+               var (
+                       counts  [2]int      // method counts
+                       methods [2][]string // method strings
+               )
+               var wg sync.WaitGroup
+               for i := 0; i < 2; i++ {
+                       i := i
+                       wg.Add(1)
+                       go func() {
+                               defer wg.Done()
+
+                               counts[i] = inst.NumMethods()
+                               for mi := 0; mi < counts[i]; mi++ {
+                                       methods[i] = append(methods[i], inst.Method(mi).String())
+                               }
+                       }()
+               }
+               wg.Wait()
+
+               if counts[0] != counts[1] {
+                       t.Errorf("mismatching method counts for %s: %d vs %d", inst, counts[0], counts[1])
+                       continue
+               }
+               for i := 0; i < counts[0]; i++ {
+                       if m0, m1 := methods[0][i], methods[1][i]; m0 != m1 {
+                               t.Errorf("mismatching methods for %s: %s vs %s", inst, m0, m1)
+                       }
+               }
+       }
+}
+
 func TestInstantiateErrors(t *testing.T) {
        tests := []struct {
                src    string // by convention, T must be the type being instantiated
@@ -2222,7 +2468,7 @@ func TestInstantiateErrors(t *testing.T) {
 
        for _, test := range tests {
                src := "package p; " + test.src
-               pkg := mustTypecheck(".", src, nil, nil)
+               pkg := mustTypecheck(src, nil, nil)
 
                T := pkg.Scope().Lookup("T").Type().(*Named)
 
@@ -2261,7 +2507,7 @@ func TestInstanceIdentity(t *testing.T) {
        conf := Config{Importer: imports}
        makePkg := func(src string) {
                fset := token.NewFileSet()
-               f := mustParse(fset, "", src)
+               f := mustParse(fset, src)
                name := f.Name.Name
                pkg, err := conf.Check(name, fset, []*ast.File{f}, nil)
                if err != nil {
@@ -2318,7 +2564,7 @@ func fn() {
                Defs: make(map[*ast.Ident]Object),
        }
        fset := token.NewFileSet()
-       f := mustParse(fset, "p.go", src)
+       f := mustParse(fset, src)
        conf := Config{}
        pkg, err := conf.Check(f.Name.Name, fset, []*ast.File{f}, info)
        if err != nil {
@@ -2450,7 +2696,7 @@ type Bad Bad // invalid type
 `
 
        fset := token.NewFileSet()
-       f := mustParse(fset, "p.go", src)
+       f := mustParse(fset, src)
        conf := Config{Error: func(error) {}}
        pkg, _ := conf.Check(f.Name.Name, fset, []*ast.File{f}, nil)
 
@@ -2545,7 +2791,7 @@ type V4 struct{}
 func (V4) M()
 `
 
-       pkg := mustTypecheck("p.go", src, nil, nil)
+       pkg := mustTypecheck(src, nil, nil)
 
        T := pkg.Scope().Lookup("T").Type().Underlying().(*Interface)
        lookup := func(name string) (*Func, bool) {
@@ -2577,3 +2823,95 @@ func (V4) M()
        // V4 has no method m but has M. Should not report wrongType.
        checkMissingMethod("V4", false)
 }
+
+func TestErrorURL(t *testing.T) {
+       var conf Config
+       *stringFieldAddr(&conf, "_ErrorURL") = " [go.dev/e/%s]"
+
+       // test case for a one-line error
+       const src1 = `
+package p
+var _ T
+`
+       _, err := typecheck(src1, &conf, nil)
+       if err == nil || !strings.HasSuffix(err.Error(), " [go.dev/e/UndeclaredName]") {
+               t.Errorf("src1: unexpected error: got %v", err)
+       }
+
+       // test case for a multi-line error
+       const src2 = `
+package p
+func f() int { return 0 }
+var _ = f(1, 2)
+`
+       _, err = typecheck(src2, &conf, nil)
+       if err == nil || !strings.Contains(err.Error(), " [go.dev/e/WrongArgCount]\n") {
+               t.Errorf("src1: unexpected error: got %v", err)
+       }
+}
+
+func TestModuleVersion(t *testing.T) {
+       // version go1.dd must be able to typecheck go1.dd.0, go1.dd.1, etc.
+       goversion := fmt.Sprintf("go1.%d", goversion.Version)
+       for _, v := range []string{
+               goversion,
+               goversion + ".0",
+               goversion + ".1",
+               goversion + ".rc",
+       } {
+               conf := Config{GoVersion: v}
+               pkg := mustTypecheck("package p", &conf, nil)
+               if pkg.GoVersion() != conf.GoVersion {
+                       t.Errorf("got %s; want %s", pkg.GoVersion(), conf.GoVersion)
+               }
+       }
+}
+
+func TestFileVersions(t *testing.T) {
+       for _, test := range []struct {
+               goVersion   string
+               fileVersion string
+               wantVersion string
+       }{
+               {"", "", ""},                   // no versions specified
+               {"go1.19", "", "go1.19"},       // module version specified
+               {"", "go1.20", ""},             // file upgrade ignored
+               {"go1.19", "go1.20", "go1.20"}, // file upgrade permitted
+               {"go1.20", "go1.19", "go1.20"}, // file downgrade not permitted
+               {"go1.21", "go1.19", "go1.19"}, // file downgrade permitted (module version is >= go1.21)
+
+               // versions containing release numbers
+               // (file versions containing release numbers are considered invalid)
+               {"go1.19.0", "", "go1.19.0"},         // no file version specified
+               {"go1.20", "go1.20.1", "go1.20"},     // file upgrade ignored
+               {"go1.20.1", "go1.20", "go1.20.1"},   // file upgrade ignored
+               {"go1.20.1", "go1.21", "go1.21"},     // file upgrade permitted
+               {"go1.20.1", "go1.19", "go1.20.1"},   // file downgrade not permitted
+               {"go1.21.1", "go1.19.1", "go1.21.1"}, // file downgrade not permitted (invalid file version)
+               {"go1.21.1", "go1.19", "go1.19"},     // file downgrade permitted (module version is >= go1.21)
+       } {
+               var src string
+               if test.fileVersion != "" {
+                       src = "//go:build " + test.fileVersion + "\n"
+               }
+               src += "package p"
+
+               conf := Config{GoVersion: test.goVersion}
+               versions := make(map[*ast.File]string)
+               var info Info
+               info.FileVersions = versions
+               mustTypecheck(src, &conf, &info)
+
+               n := 0
+               for _, v := range versions {
+                       want := test.wantVersion
+                       if v != want {
+                               t.Errorf("%q: unexpected file version: got %q, want %q", src, v, want)
+                       }
+                       n++
+               }
+               if n != 1 {
+                       t.Errorf("%q: incorrect number of map entries: got %d", src, n)
+               }
+       }
+}