summaryrefslogtreecommitdiff
path: root/src/go/types
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2023-04-19 15:34:49 -0700
committerGopher Robot <gobot@golang.org>2023-04-28 17:58:07 +0000
commitc9b0d8b61ef222b151206546b5d56c1cbe0d3449 (patch)
treeb1bb8e189bbcc351ab357d6f7e86d20430d895af /src/go/types
parent08357012247db5c84002b7a4c1693411a1a9b295 (diff)
downloadgo-git-c9b0d8b61ef222b151206546b5d56c1cbe0d3449.tar.gz
go/types, types2: extract package name from test sources automatically
This simplifies explicit tests and ensures that the error messages contain the package name instead of a generic file name like "p.go". Fixes #59736. Change-Id: I1b42e30f53ba88456e92f990d80ca68ffc987e20 Reviewed-on: https://go-review.googlesource.com/c/go/+/486617 TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com> Run-TryBot: Robert Griesemer <gri@google.com>
Diffstat (limited to 'src/go/types')
-rw-r--r--src/go/types/api_test.go105
-rw-r--r--src/go/types/builtins_test.go2
-rw-r--r--src/go/types/example_test.go30
-rw-r--r--src/go/types/hilbert_test.go2
-rw-r--r--src/go/types/instantiate_test.go10
-rw-r--r--src/go/types/issues_test.go51
-rw-r--r--src/go/types/methodset_test.go2
-rw-r--r--src/go/types/mono_test.go2
-rw-r--r--src/go/types/named_test.go4
-rw-r--r--src/go/types/object_test.go4
-rw-r--r--src/go/types/resolver_test.go4
-rw-r--r--src/go/types/sizes_test.go4
-rw-r--r--src/go/types/typestring_test.go6
13 files changed, 113 insertions, 113 deletions
diff --git a/src/go/types/api_test.go b/src/go/types/api_test.go
index 2d0df43263..825f30585b 100644
--- a/src/go/types/api_test.go
+++ b/src/go/types/api_test.go
@@ -24,21 +24,21 @@ 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 parse(fset *token.FileSet, src string) (*ast.File, error) {
+ return parser.ParseFile(fset, pkgName(src), 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 := parse(fset, src)
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)
+ f, err := parse(fset, src)
if f == nil { // ignore errors unless f is nil
return nil, err
}
@@ -51,9 +51,9 @@ 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 {
+func mustTypecheck(src string, conf *Config, info *Info) *Package {
fset := token.NewFileSet()
- f := mustParse(fset, path, src)
+ f := mustParse(fset, src)
if conf == nil {
conf = &Config{
Importer: importer.Default(),
@@ -66,6 +66,20 @@ func mustTypecheck(path, src string, conf *Config, info *Info) *Package {
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
@@ -149,7 +163,7 @@ func TestValuesInfo(t *testing.T) {
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
@@ -387,7 +401,7 @@ func TestTypesInfo(t *testing.T) {
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
@@ -396,7 +410,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
@@ -561,7 +575,7 @@ type T[P any] []P
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, _ := typecheck(src, &conf, &Info{Instances: instMap, Uses: useMap})
imports[pkg.Name()] = pkg
return pkg
}
@@ -656,7 +670,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
@@ -723,7 +737,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
@@ -756,7 +770,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),
@@ -864,7 +878,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 {
@@ -992,7 +1006,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>"
@@ -1084,7 +1098,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) {
@@ -1272,7 +1286,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) {
@@ -1293,8 +1307,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
@@ -1330,10 +1344,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)
}
}
@@ -1368,8 +1380,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)
}
@@ -1546,9 +1557,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 = `
@@ -1577,9 +1586,7 @@ func TestIssue59603(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 = `
@@ -1664,7 +1671,7 @@ func TestLookupFieldOrMethod(t *testing.T) {
}
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 {
@@ -1710,7 +1717,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)
@@ -1747,7 +1754,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.
@@ -1791,7 +1798,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:]*)\*/$`)
@@ -1943,7 +1950,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 {
@@ -2017,7 +2024,7 @@ func TestIdenticalUnions(t *testing.T) {
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
}
@@ -2040,7 +2047,7 @@ 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`},
@@ -2053,7 +2060,7 @@ func TestCompositeLitTypes(t *testing.T) {
{`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)
@@ -2108,7 +2115,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),
@@ -2167,7 +2174,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
@@ -2222,7 +2229,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)
@@ -2257,7 +2264,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)
@@ -2296,7 +2303,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 {
@@ -2353,7 +2360,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 {
@@ -2485,7 +2492,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)
@@ -2580,7 +2587,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) {
diff --git a/src/go/types/builtins_test.go b/src/go/types/builtins_test.go
index 5591fecf02..6238464f58 100644
--- a/src/go/types/builtins_test.go
+++ b/src/go/types/builtins_test.go
@@ -174,7 +174,7 @@ func testBuiltinSignature(t *testing.T, name, src0, want string) {
uses := make(map[*ast.Ident]Object)
types := make(map[ast.Expr]TypeAndValue)
- mustTypecheck("p", src, nil, &Info{Uses: uses, Types: types})
+ mustTypecheck(src, nil, &Info{Uses: uses, Types: types})
// find called function
n := 0
diff --git a/src/go/types/example_test.go b/src/go/types/example_test.go
index 37b5ea4511..1ee47bc123 100644
--- a/src/go/types/example_test.go
+++ b/src/go/types/example_test.go
@@ -35,25 +35,23 @@ func ExampleScope() {
// Parse the source files for a package.
fset := token.NewFileSet()
var files []*ast.File
- for _, file := range []struct{ name, input string }{
- {"main.go", `
-package main
+ for _, src := range []string{
+ `package main
import "fmt"
func main() {
freezing := FToC(-18)
fmt.Println(freezing, Boiling) }
-`},
- {"celsius.go", `
-package main
+`,
+ `package main
import "fmt"
type Celsius float64
func (c Celsius) String() string { return fmt.Sprintf("%g°C", c) }
func FToC(f float64) Celsius { return Celsius(f - 32 / 9 * 5) }
const Boiling Celsius = 100
func Unused() { {}; {{ var x int; _ = x }} } // make sure empty block scopes get printed
-`},
+`,
} {
- files = append(files, mustParse(fset, file.name, file.input))
+ files = append(files, mustParse(fset, src))
}
// Type-check a package consisting of these files.
@@ -79,13 +77,13 @@ func Unused() { {}; {{ var x int; _ = x }} } // make sure empty block scopes get
// . func temperature.FToC(f float64) temperature.Celsius
// . func temperature.Unused()
// . func temperature.main()
- // . main.go scope {
+ // . main scope {
// . . package fmt
// . . function scope {
// . . . var freezing temperature.Celsius
// . . }
// . }
- // . celsius.go scope {
+ // . main scope {
// . . package fmt
// . . function scope {
// . . . var c temperature.Celsius
@@ -183,7 +181,7 @@ func fib(x int) int {
// We need a specific fileset in this test below for positions.
// Cannot use typecheck helper.
fset := token.NewFileSet()
- f := mustParse(fset, "fib.go", input)
+ f := mustParse(fset, input)
// Type-check the package.
// We create an empty map for each kind of input
@@ -250,10 +248,10 @@ func fib(x int) int {
// defined at -
// used at 6:15
// func fib(x int) int:
- // defined at fib.go:8:6
+ // defined at fib:8:6
// used at 12:20, 12:9
// type S string:
- // defined at fib.go:4:6
+ // defined at fib:4:6
// used at 6:23
// type int:
// defined at -
@@ -262,13 +260,13 @@ func fib(x int) int {
// defined at -
// used at 4:8
// var b S:
- // defined at fib.go:6:8
+ // defined at fib:6:8
// used at 6:19
// var c string:
- // defined at fib.go:6:11
+ // defined at fib:6:11
// used at 6:25
// var x int:
- // defined at fib.go:8:10
+ // defined at fib:8:10
// used at 10:10, 12:13, 12:24, 9:5
//
// Types and Values of each expression:
diff --git a/src/go/types/hilbert_test.go b/src/go/types/hilbert_test.go
index 7da2a7ded1..f530422492 100644
--- a/src/go/types/hilbert_test.go
+++ b/src/go/types/hilbert_test.go
@@ -27,7 +27,7 @@ func TestHilbert(t *testing.T) {
return
}
- mustTypecheck("hilbert.go", string(src), nil, nil)
+ mustTypecheck(string(src), nil, nil)
}
func program(n int, out string) []byte {
diff --git a/src/go/types/instantiate_test.go b/src/go/types/instantiate_test.go
index 574f3aeb86..58dfa70131 100644
--- a/src/go/types/instantiate_test.go
+++ b/src/go/types/instantiate_test.go
@@ -109,7 +109,7 @@ func TestInstantiateEquality(t *testing.T) {
}
for _, test := range tests {
- pkg := mustTypecheck(".", test.src, nil, nil)
+ pkg := mustTypecheck(test.src, nil, nil)
t.Run(pkg.Name(), func(t *testing.T) {
ctxt := NewContext()
@@ -135,8 +135,8 @@ func TestInstantiateEquality(t *testing.T) {
func TestInstantiateNonEquality(t *testing.T) {
const src = "package p; type T[P any] int"
- pkg1 := mustTypecheck(".", src, nil, nil)
- pkg2 := mustTypecheck(".", src, nil, nil)
+ pkg1 := mustTypecheck(src, nil, nil)
+ pkg2 := mustTypecheck(src, nil, nil)
// We consider T1 and T2 to be distinct types, so their instances should not
// be deduplicated by the context.
T1 := pkg1.Scope().Lookup("T").Type().(*Named)
@@ -181,7 +181,7 @@ var X T[int]
for _, test := range tests {
src := prefix + test.decl
- pkg := mustTypecheck(".", src, nil, nil)
+ pkg := mustTypecheck(src, nil, nil)
typ := NewPointer(pkg.Scope().Lookup("X").Type())
obj, _, _ := LookupFieldOrMethod(typ, false, pkg, "m")
m, _ := obj.(*Func)
@@ -203,7 +203,7 @@ func (T[P]) m() {}
var _ T[int]
`
- pkg := mustTypecheck(".", src, nil, nil)
+ pkg := mustTypecheck(src, nil, nil)
typ := pkg.Scope().Lookup("T").Type().(*Named)
obj, _, _ := LookupFieldOrMethod(typ, false, pkg, "m")
if obj == nil {
diff --git a/src/go/types/issues_test.go b/src/go/types/issues_test.go
index 888ca3cc70..a464659aaf 100644
--- a/src/go/types/issues_test.go
+++ b/src/go/types/issues_test.go
@@ -21,7 +21,7 @@ import (
)
func TestIssue5770(t *testing.T) {
- _, err := typecheck("p", `package p; type S struct{T}`, nil, nil)
+ _, err := typecheck(`package p; type S struct{T}`, nil, nil)
const want = "undefined: T"
if err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("got: %v; want: %s", err, want)
@@ -41,7 +41,7 @@ var (
_ = (interface{})(nil)
)`
types := make(map[ast.Expr]TypeAndValue)
- mustTypecheck("p", src, nil, &Info{Types: types})
+ mustTypecheck(src, nil, &Info{Types: types})
for x, tv := range types {
var want Type
@@ -80,7 +80,7 @@ func f() int {
}
`
types := make(map[ast.Expr]TypeAndValue)
- mustTypecheck("p", src, nil, &Info{Types: types})
+ mustTypecheck(src, nil, &Info{Types: types})
want := Typ[Int]
n := 0
@@ -104,7 +104,7 @@ package p
func (T) m() (res bool) { return }
type T struct{} // receiver type after method declaration
`
- f := mustParse(fset, "", src)
+ f := mustParse(fset, src)
var conf Config
defs := make(map[*ast.Ident]Object)
@@ -138,7 +138,7 @@ func _() {
// We need a specific fileset in this test below for positions.
// Cannot use typecheck helper.
fset := token.NewFileSet()
- f := mustParse(fset, "", src)
+ f := mustParse(fset, src)
const want = `L3 defs func p._()
L4 defs const w untyped int
@@ -235,7 +235,7 @@ func main() {
`
f := func(test, src string) {
info := &Info{Uses: make(map[*ast.Ident]Object)}
- mustTypecheck("main", src, nil, info)
+ mustTypecheck(src, nil, info)
var pkg *Package
count := 0
@@ -263,13 +263,13 @@ func TestIssue22525(t *testing.T) {
got := "\n"
conf := Config{Error: func(err error) { got += err.Error() + "\n" }}
- typecheck("", src, &conf, nil) // do not crash
+ typecheck(src, &conf, nil) // do not crash
want := `
-1:27: a declared and not used
-1:30: b declared and not used
-1:33: c declared and not used
-1:36: d declared and not used
-1:39: e declared and not used
+p:1:27: a declared and not used
+p:1:30: b declared and not used
+p:1:33: c declared and not used
+p:1:36: d declared and not used
+p:1:39: e declared and not used
`
if got != want {
t.Errorf("got: %swant: %s", got, want)
@@ -289,7 +289,7 @@ func TestIssue25627(t *testing.T) {
`struct { *I }`,
`struct { a int; b Missing; *Missing }`,
} {
- f := mustParse(fset, "", prefix+src)
+ f := mustParse(fset, prefix+src)
cfg := Config{Importer: importer.Default(), Error: func(err error) {}}
info := &Info{Types: make(map[ast.Expr]TypeAndValue)}
@@ -326,7 +326,7 @@ func TestIssue28005(t *testing.T) {
// compute original file ASTs
var orig [len(sources)]*ast.File
for i, src := range sources {
- orig[i] = mustParse(fset, "", src)
+ orig[i] = mustParse(fset, src)
}
// run the test for all order permutations of the incoming files
@@ -400,8 +400,8 @@ func TestIssue28282(t *testing.T) {
}
func TestIssue29029(t *testing.T) {
- f1 := mustParse(fset, "", `package p; type A interface { M() }`)
- f2 := mustParse(fset, "", `package p; var B interface { A }`)
+ f1 := mustParse(fset, `package p; type A interface { M() }`)
+ f2 := mustParse(fset, `package p; var B interface { A }`)
// printInfo prints the *Func definitions recorded in info, one *Func per line.
printInfo := func(info *Info) string {
@@ -447,10 +447,10 @@ func TestIssue34151(t *testing.T) {
const asrc = `package a; type I interface{ M() }; type T struct { F interface { I } }`
const bsrc = `package b; import "a"; type T struct { F interface { a.I } }; var _ = a.T(T{})`
- a := mustTypecheck("a", asrc, nil, nil)
+ a := mustTypecheck(asrc, nil, nil)
conf := Config{Importer: importHelper{pkg: a}}
- mustTypecheck("b", bsrc, &conf, nil)
+ mustTypecheck(bsrc, &conf, nil)
}
type importHelper struct {
@@ -488,13 +488,8 @@ func TestIssue34921(t *testing.T) {
var pkg *Package
for _, src := range sources {
- f := mustParse(fset, "", src)
conf := Config{Importer: importHelper{pkg: pkg}}
- res, err := conf.Check(f.Name.Name, fset, []*ast.File{f}, nil)
- if err != nil {
- t.Errorf("%q failed to typecheck: %v", src, err)
- }
- pkg = res // res is imported by the next package in this test
+ pkg = mustTypecheck(src, &conf, nil) // pkg imported by the next package in this test
}
}
@@ -599,7 +594,7 @@ var _ T = template /* ERRORx "cannot use.*text/template.* as T value" */.Templat
`
)
- a := mustTypecheck("a", asrc, nil, nil)
+ a := mustTypecheck(asrc, nil, nil)
imp := importHelper{pkg: a, fallback: importer.Default()}
testFiles(t, nil, []string{"b.go"}, [][]byte{[]byte(bsrc)}, false, imp)
@@ -696,7 +691,7 @@ func TestIssue51093(t *testing.T) {
for _, test := range tests {
src := fmt.Sprintf("package p; func _[P %s]() { _ = P(%s) }", test.typ, test.val)
types := make(map[ast.Expr]TypeAndValue)
- mustTypecheck("p", src, nil, &Info{Types: types})
+ mustTypecheck(src, nil, &Info{Types: types})
var n int
for x, tv := range types {
@@ -828,8 +823,8 @@ func (S) M5(struct {S;t}) {}
fset := token.NewFileSet()
test := func(main, b, want string) {
re := regexp.MustCompile(want)
- bpkg := mustTypecheck("b", b, nil, nil)
- mast := mustParse(fset, "main.go", main)
+ bpkg := mustTypecheck(b, nil, nil)
+ mast := mustParse(fset, main)
conf := Config{Importer: importHelper{pkg: bpkg}}
_, err := conf.Check(mast.Name.Name, fset, []*ast.File{mast}, nil)
if err == nil {
diff --git a/src/go/types/methodset_test.go b/src/go/types/methodset_test.go
index 3f8a0b1a10..918b51d93b 100644
--- a/src/go/types/methodset_test.go
+++ b/src/go/types/methodset_test.go
@@ -84,7 +84,7 @@ func TestNewMethodSet(t *testing.T) {
}
check := func(src string, methods []method, generic bool) {
- pkg := mustTypecheck("test", "package p;"+src, nil, nil)
+ pkg := mustTypecheck("package p;"+src, nil, nil)
scope := pkg.Scope()
if generic {
diff --git a/src/go/types/mono_test.go b/src/go/types/mono_test.go
index a8d2acfd66..ccab846c6d 100644
--- a/src/go/types/mono_test.go
+++ b/src/go/types/mono_test.go
@@ -21,7 +21,7 @@ func checkMono(t *testing.T, body string) error {
Error: func(err error) { fmt.Fprintln(&buf, err) },
Importer: importer.Default(),
}
- typecheck("x", src, &conf, nil)
+ typecheck(src, &conf, nil)
if buf.Len() == 0 {
return nil
}
diff --git a/src/go/types/named_test.go b/src/go/types/named_test.go
index 55c0021398..8e00f6e0f9 100644
--- a/src/go/types/named_test.go
+++ b/src/go/types/named_test.go
@@ -32,7 +32,7 @@ func (G[P]) N() (p P) { return }
type Inst = G[int]
`
- pkg := mustTypecheck("p", src, nil, nil)
+ pkg := mustTypecheck(src, nil, nil)
var (
T = pkg.Scope().Lookup("T").Type()
@@ -107,7 +107,7 @@ type Inst = *Tree[int]
`
fset := token.NewFileSet()
- f := mustParse(fset, "foo.go", src)
+ f := mustParse(fset, src)
pkg := NewPackage("p", f.Name.Name)
if err := NewChecker(nil, fset, pkg, nil).Files([]*ast.File{f}); err != nil {
t.Fatal(err)
diff --git a/src/go/types/object_test.go b/src/go/types/object_test.go
index bed8de3637..74acdaeeeb 100644
--- a/src/go/types/object_test.go
+++ b/src/go/types/object_test.go
@@ -58,7 +58,7 @@ func TestIsAlias(t *testing.T) {
// the same Func Object as the original method. See also go.dev/issue/34421.
func TestEmbeddedMethod(t *testing.T) {
const src = `package p; type I interface { error }`
- pkg := mustTypecheck("p", src, nil, nil)
+ pkg := mustTypecheck(src, nil, nil)
// get original error.Error method
eface := Universe.Lookup("error")
@@ -112,7 +112,7 @@ func TestObjectString(t *testing.T) {
for _, test := range testObjects {
src := "package p; " + test.src
- pkg, err := typecheck(filename, src, nil, nil)
+ pkg, err := typecheck(src, nil, nil)
if err != nil {
t.Errorf("%s: %s", src, err)
continue
diff --git a/src/go/types/resolver_test.go b/src/go/types/resolver_test.go
index 284ad8e998..e95af80585 100644
--- a/src/go/types/resolver_test.go
+++ b/src/go/types/resolver_test.go
@@ -119,8 +119,8 @@ func TestResolveIdents(t *testing.T) {
// parse package files
fset := token.NewFileSet()
var files []*ast.File
- for i, src := range sources {
- files = append(files, mustParse(fset, fmt.Sprintf("sources[%d]", i), src))
+ for _, src := range sources {
+ files = append(files, mustParse(fset, src))
}
// resolve and type-check package AST
diff --git a/src/go/types/sizes_test.go b/src/go/types/sizes_test.go
index 4964bf2cf9..f2e7e8ab2e 100644
--- a/src/go/types/sizes_test.go
+++ b/src/go/types/sizes_test.go
@@ -21,7 +21,7 @@ func findStructType(t *testing.T, src string) *types.Struct {
func findStructTypeConfig(t *testing.T, src string, conf *types.Config) *types.Struct {
types_ := make(map[ast.Expr]types.TypeAndValue)
- mustTypecheck("x", src, nil, &types.Info{Types: types_})
+ mustTypecheck(src, nil, &types.Info{Types: types_})
for _, tv := range types_ {
if ts, ok := tv.Type.(*types.Struct); ok {
return ts
@@ -90,7 +90,7 @@ const _ = unsafe.Offsetof(struct{ x int64 }{}.x)
Importer: importer.Default(),
Sizes: &types.StdSizes{WordSize: 8, MaxAlign: 8},
}
- mustTypecheck("x", src, &conf, &info)
+ mustTypecheck(src, &conf, &info)
for _, tv := range info.Types {
_ = conf.Sizes.Sizeof(tv.Type)
_ = conf.Sizes.Alignof(tv.Type)
diff --git a/src/go/types/typestring_test.go b/src/go/types/typestring_test.go
index d3172d6bb9..45670b7e15 100644
--- a/src/go/types/typestring_test.go
+++ b/src/go/types/typestring_test.go
@@ -119,7 +119,7 @@ func TestTypeString(t *testing.T) {
for _, test := range tests {
src := `package p; import "io"; type _ io.Writer; type T ` + test.src
- pkg, err := typecheck(filename, src, nil, nil)
+ pkg, err := typecheck(src, nil, nil)
if err != nil {
t.Errorf("%s: %s", src, err)
continue
@@ -137,8 +137,8 @@ func TestTypeString(t *testing.T) {
}
func TestQualifiedTypeString(t *testing.T) {
- p := mustTypecheck("p.go", "package p; type T int", nil, nil)
- q := mustTypecheck("q.go", "package q", nil, nil)
+ p := mustTypecheck("package p; type T int", nil, nil)
+ q := mustTypecheck("package q", nil, nil)
pT := p.Scope().Lookup("T").Type()
for _, test := range []struct {