1
0
mirror of https://github.com/golang/go synced 2024-09-30 04:24:29 -06:00

go/types, types2: replace typecheck with mustTypecheck almost everywhere (cleanup)

Replace even in places where before we have a specific error message
or different control-flow (except in TestTypeString or TestObjectString)
because failing to type-check in virtually all cases represents an error
in the test itself.

Change-Id: I9f1e6d25bddd92c168353409b281b5a3f29a747c
Reviewed-on: https://go-review.googlesource.com/c/go/+/443915
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
This commit is contained in:
Robert Griesemer 2022-10-18 20:41:14 -07:00 committed by Gopher Robot
parent 27f094b17c
commit 8190876240
13 changed files with 60 additions and 155 deletions

View File

@ -43,12 +43,12 @@ func typecheck(path, src string, info *Info) (*Package, error) {
return conf.Check(f.PkgName.Value, []*syntax.File{f}, info) return conf.Check(f.PkgName.Value, []*syntax.File{f}, info)
} }
func mustTypecheck(path, src string, info *Info) string { func mustTypecheck(path, src string, info *Info) *Package {
pkg, err := typecheck(path, src, info) pkg, err := typecheck(path, src, info)
if err != nil { if err != nil {
panic(err) // so we don't need to pass *testing.T panic(err) // so we don't need to pass *testing.T
} }
return pkg.Name() return pkg
} }
func TestValuesInfo(t *testing.T) { func TestValuesInfo(t *testing.T) {
@ -134,7 +134,7 @@ func TestValuesInfo(t *testing.T) {
info := Info{ info := Info{
Types: make(map[syntax.Expr]TypeAndValue), Types: make(map[syntax.Expr]TypeAndValue),
} }
name := mustTypecheck("ValuesInfo", test.src, &info) name := mustTypecheck("ValuesInfo", test.src, &info).Name()
// look for expression // look for expression
var expr syntax.Expr var expr syntax.Expr
@ -385,7 +385,7 @@ func TestTypesInfo(t *testing.T) {
name = pkg.Name() name = pkg.Name()
} }
} else { } else {
name = mustTypecheck("TypesInfo", test.src, &info) name = mustTypecheck("TypesInfo", test.src, &info).Name()
} }
// look for expression type // look for expression type
@ -644,7 +644,7 @@ func TestDefsInfo(t *testing.T) {
info := Info{ info := Info{
Defs: make(map[*syntax.Name]Object), Defs: make(map[*syntax.Name]Object),
} }
name := mustTypecheck("DefsInfo", test.src, &info) name := mustTypecheck("DefsInfo", test.src, &info).Name()
// find object // find object
var def Object var def Object
@ -709,7 +709,7 @@ func TestUsesInfo(t *testing.T) {
info := Info{ info := Info{
Uses: make(map[*syntax.Name]Object), Uses: make(map[*syntax.Name]Object),
} }
name := mustTypecheck("UsesInfo", test.src, &info) name := mustTypecheck("UsesInfo", test.src, &info).Name()
// find object // find object
var use Object var use Object
@ -849,7 +849,7 @@ func TestImplicitsInfo(t *testing.T) {
info := Info{ info := Info{
Implicits: make(map[syntax.Node]Object), Implicits: make(map[syntax.Node]Object),
} }
name := mustTypecheck("ImplicitsInfo", test.src, &info) name := mustTypecheck("ImplicitsInfo", test.src, &info).Name()
// the test cases expect at most one Implicits entry // the test cases expect at most one Implicits entry
if len(info.Implicits) > 1 { if len(info.Implicits) > 1 {
@ -977,7 +977,7 @@ func TestPredicatesInfo(t *testing.T) {
for _, test := range tests { for _, test := range tests {
info := Info{Types: make(map[syntax.Expr]TypeAndValue)} info := Info{Types: make(map[syntax.Expr]TypeAndValue)}
name := mustTypecheck("PredicatesInfo", test.src, &info) name := mustTypecheck("PredicatesInfo", test.src, &info).Name()
// look for expression predicates // look for expression predicates
got := "<missing>" got := "<missing>"
@ -1069,7 +1069,7 @@ func TestScopesInfo(t *testing.T) {
for _, test := range tests { for _, test := range tests {
info := Info{Scopes: make(map[syntax.Node]*Scope)} info := Info{Scopes: make(map[syntax.Node]*Scope)}
name := mustTypecheck("ScopesInfo", test.src, &info) name := mustTypecheck("ScopesInfo", test.src, &info).Name()
// number of scopes must match // number of scopes must match
if len(info.Scopes) != len(test.scopes) { if len(info.Scopes) != len(test.scopes) {
@ -1257,7 +1257,7 @@ func TestInitOrderInfo(t *testing.T) {
for _, test := range tests { for _, test := range tests {
info := Info{} info := Info{}
name := mustTypecheck("InitOrderInfo", test.src, &info) name := mustTypecheck("InitOrderInfo", test.src, &info).Name()
// number of initializers must match // number of initializers must match
if len(info.InitOrder) != len(test.inits) { if len(info.InitOrder) != len(test.inits) {
@ -1626,11 +1626,7 @@ func TestLookupFieldOrMethod(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
pkg, err := typecheck("test", "package p;"+test.src, nil) pkg := mustTypecheck("test", "package p;"+test.src, nil)
if err != nil {
t.Errorf("%s: incorrect test case: %s", test.src, err)
continue
}
obj := pkg.Scope().Lookup("a") obj := pkg.Scope().Lookup("a")
if obj == nil { if obj == nil {
@ -1912,11 +1908,7 @@ func TestIdentical(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
pkg, err := typecheck("test", "package p;"+test.src, nil) pkg := mustTypecheck("test", "package p;"+test.src, nil)
if err != nil {
t.Errorf("%s: incorrect test case: %s", test.src, err)
continue
}
X := pkg.Scope().Lookup("X") X := pkg.Scope().Lookup("X")
Y := pkg.Scope().Lookup("Y") Y := pkg.Scope().Lookup("Y")
if X == nil || Y == nil { if X == nil || Y == nil {
@ -2191,10 +2183,7 @@ func f(x T) T { return foo.F(x) }
func TestInstantiate(t *testing.T) { func TestInstantiate(t *testing.T) {
// eventually we like more tests but this is a start // eventually we like more tests but this is a start
const src = "package p; type T[P any] *T[P]" const src = "package p; type T[P any] *T[P]"
pkg, err := typecheck(".", src, nil) pkg := mustTypecheck(".", src, nil)
if err != nil {
t.Fatal(err)
}
// type T should have one type parameter // type T should have one type parameter
T := pkg.Scope().Lookup("T").Type().(*Named) T := pkg.Scope().Lookup("T").Type().(*Named)
@ -2229,14 +2218,11 @@ func TestInstantiateErrors(t *testing.T) {
for _, test := range tests { for _, test := range tests {
src := "package p; " + test.src src := "package p; " + test.src
pkg, err := typecheck(".", src, nil) pkg := mustTypecheck(".", src, nil)
if err != nil {
t.Fatal(err)
}
T := pkg.Scope().Lookup("T").Type().(*Named) T := pkg.Scope().Lookup("T").Type().(*Named)
_, err = Instantiate(nil, T, test.targs, true) _, err := Instantiate(nil, T, test.targs, true)
if err == nil { if err == nil {
t.Fatalf("Instantiate(%v, %v) returned nil error, want non-nil", T, test.targs) t.Fatalf("Instantiate(%v, %v) returned nil error, want non-nil", T, test.targs)
} }
@ -2552,10 +2538,7 @@ type V4 struct{}
func (V4) M() func (V4) M()
` `
pkg, err := typecheck("p.go", src, nil) pkg := mustTypecheck("p.go", src, nil)
if err != nil {
t.Fatal(err)
}
T := pkg.Scope().Lookup("T").Type().Underlying().(*Interface) T := pkg.Scope().Lookup("T").Type().Underlying().(*Interface)
lookup := func(name string) (*Func, bool) { lookup := func(name string) (*Func, bool) {

View File

@ -107,10 +107,7 @@ func TestInstantiateEquality(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
pkg, err := typecheck(".", test.src, nil) pkg := mustTypecheck(".", test.src, nil)
if err != nil {
t.Fatal(err)
}
t.Run(pkg.Name(), func(t *testing.T) { t.Run(pkg.Name(), func(t *testing.T) {
ctxt := NewContext() ctxt := NewContext()
@ -136,14 +133,8 @@ func TestInstantiateEquality(t *testing.T) {
func TestInstantiateNonEquality(t *testing.T) { func TestInstantiateNonEquality(t *testing.T) {
const src = "package p; type T[P any] int" const src = "package p; type T[P any] int"
pkg1, err := typecheck(".", src, nil) pkg1 := mustTypecheck(".", src, nil)
if err != nil { pkg2 := mustTypecheck(".", src, nil)
t.Fatal(err)
}
pkg2, err := typecheck(".", src, nil)
if err != nil {
t.Fatal(err)
}
// We consider T1 and T2 to be distinct types, so their instances should not // We consider T1 and T2 to be distinct types, so their instances should not
// be deduplicated by the context. // be deduplicated by the context.
T1 := pkg1.Scope().Lookup("T").Type().(*Named) T1 := pkg1.Scope().Lookup("T").Type().(*Named)
@ -188,10 +179,7 @@ var X T[int]
for _, test := range tests { for _, test := range tests {
src := prefix + test.decl src := prefix + test.decl
pkg, err := typecheck(".", src, nil) pkg := mustTypecheck(".", src, nil)
if err != nil {
t.Fatal(err)
}
typ := NewPointer(pkg.Scope().Lookup("X").Type()) typ := NewPointer(pkg.Scope().Lookup("X").Type())
obj, _, _ := LookupFieldOrMethod(typ, false, pkg, "m") obj, _, _ := LookupFieldOrMethod(typ, false, pkg, "m")
m, _ := obj.(*Func) m, _ := obj.(*Func)
@ -213,10 +201,7 @@ func (T[P]) m() {}
var _ T[int] var _ T[int]
` `
pkg, err := typecheck(".", src, nil) pkg := mustTypecheck(".", src, nil)
if err != nil {
t.Fatal(err)
}
typ := pkg.Scope().Lookup("T").Type().(*Named) typ := pkg.Scope().Lookup("T").Type().(*Named)
obj, _, _ := LookupFieldOrMethod(typ, false, pkg, "m") obj, _, _ := LookupFieldOrMethod(typ, false, pkg, "m")
if obj == nil { if obj == nil {

View File

@ -449,10 +449,7 @@ func TestIssue34151(t *testing.T) {
const asrc = `package a; type I interface{ M() }; type T struct { F interface { I } }` 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{})` const bsrc = `package b; import "a"; type T struct { F interface { a.I } }; var _ = a.T(T{})`
a, err := typecheck("a", asrc, nil) a := mustTypecheck("a", asrc, nil)
if err != nil {
t.Fatalf("package %s failed to typecheck: %v", a.Name(), err)
}
bast := mustParse("", bsrc) bast := mustParse("", bsrc)
conf := Config{Importer: importHelper{pkg: a}} conf := Config{Importer: importHelper{pkg: a}}
@ -564,16 +561,13 @@ func TestIssue43124(t *testing.T) {
csrc = `package c; import ("a"; "html/template"); func _() { a.G(template.Template{}) }` csrc = `package c; import ("a"; "html/template"); func _() { a.G(template.Template{}) }`
) )
a, err := typecheck("a", asrc, nil) a := mustTypecheck("a", asrc, nil)
if err != nil {
t.Fatalf("package a failed to typecheck: %v", err)
}
conf := Config{Importer: importHelper{pkg: a, fallback: defaultImporter()}} conf := Config{Importer: importHelper{pkg: a, fallback: defaultImporter()}}
// Packages should be fully qualified when there is ambiguity within the // Packages should be fully qualified when there is ambiguity within the
// error string itself. // error string itself.
bast := mustParse("", bsrc) bast := mustParse("", bsrc)
_, err = conf.Check(bast.PkgName.Value, []*syntax.File{bast}, nil) _, err := conf.Check(bast.PkgName.Value, []*syntax.File{bast}, nil)
if err == nil { if err == nil {
t.Fatal("package b had no errors") t.Fatal("package b had no errors")
} }

View File

@ -31,10 +31,7 @@ func (G[P]) N() (p P) { return }
type Inst = G[int] type Inst = G[int]
` `
pkg, err := typecheck("p", src, nil) pkg := mustTypecheck("p", src, nil)
if err != nil {
b.Fatal(err)
}
var ( var (
T = pkg.Scope().Lookup("T").Type() T = pkg.Scope().Lookup("T").Type()

View File

@ -18,13 +18,9 @@ func findStructType(t *testing.T, src string) *types2.Struct {
} }
func findStructTypeConfig(t *testing.T, src string, conf *types2.Config) *types2.Struct { func findStructTypeConfig(t *testing.T, src string, conf *types2.Config) *types2.Struct {
f := mustParse("x.go", src) types := make(map[syntax.Expr]types2.TypeAndValue)
info := types2.Info{Types: make(map[syntax.Expr]types2.TypeAndValue)} mustTypecheck("x", src, &types2.Info{Types: types})
_, err := conf.Check("x", []*syntax.File{f}, &info) for _, tv := range types {
if err != nil {
t.Fatal(err)
}
for _, tv := range info.Types {
if ts, ok := tv.Type.(*types2.Struct); ok { if ts, ok := tv.Type.(*types2.Struct); ok {
return ts return ts
} }

View File

@ -135,8 +135,8 @@ func TestTypeString(t *testing.T) {
} }
func TestQualifiedTypeString(t *testing.T) { func TestQualifiedTypeString(t *testing.T) {
p, _ := typecheck("p.go", "package p; type T int", nil) p := mustTypecheck("p.go", "package p; type T int", nil)
q, _ := typecheck("q.go", "package q", nil) q := mustTypecheck("q.go", "package q", nil)
pT := p.Scope().Lookup("T").Type() pT := p.Scope().Lookup("T").Type()
for _, test := range []struct { for _, test := range []struct {

View File

@ -46,12 +46,12 @@ func typecheck(path, src string, info *Info) (*Package, error) {
return conf.Check(f.Name.Name, fset, []*ast.File{f}, info) return conf.Check(f.Name.Name, fset, []*ast.File{f}, info)
} }
func mustTypecheck(path, src string, info *Info) string { func mustTypecheck(path, src string, info *Info) *Package {
pkg, err := typecheck(path, src, info) pkg, err := typecheck(path, src, info)
if err != nil { if err != nil {
panic(err) // so we don't need to pass *testing.T panic(err) // so we don't need to pass *testing.T
} }
return pkg.Name() return pkg
} }
func TestValuesInfo(t *testing.T) { func TestValuesInfo(t *testing.T) {
@ -137,7 +137,7 @@ func TestValuesInfo(t *testing.T) {
info := Info{ info := Info{
Types: make(map[ast.Expr]TypeAndValue), Types: make(map[ast.Expr]TypeAndValue),
} }
name := mustTypecheck("ValuesInfo", test.src, &info) name := mustTypecheck("ValuesInfo", test.src, &info).Name()
// look for expression // look for expression
var expr ast.Expr var expr ast.Expr
@ -384,7 +384,7 @@ func TestTypesInfo(t *testing.T) {
name = pkg.Name() name = pkg.Name()
} }
} else { } else {
name = mustTypecheck("TypesInfo", test.src, &info) name = mustTypecheck("TypesInfo", test.src, &info).Name()
} }
// look for expression type // look for expression type
@ -642,7 +642,7 @@ func TestDefsInfo(t *testing.T) {
info := Info{ info := Info{
Defs: make(map[*ast.Ident]Object), Defs: make(map[*ast.Ident]Object),
} }
name := mustTypecheck("DefsInfo", test.src, &info) name := mustTypecheck("DefsInfo", test.src, &info).Name()
// find object // find object
var def Object var def Object
@ -709,7 +709,7 @@ func TestUsesInfo(t *testing.T) {
info := Info{ info := Info{
Uses: make(map[*ast.Ident]Object), Uses: make(map[*ast.Ident]Object),
} }
name := mustTypecheck("UsesInfo", test.src, &info) name := mustTypecheck("UsesInfo", test.src, &info).Name()
// find object // find object
var use Object var use Object
@ -850,7 +850,7 @@ func TestImplicitsInfo(t *testing.T) {
info := Info{ info := Info{
Implicits: make(map[ast.Node]Object), Implicits: make(map[ast.Node]Object),
} }
name := mustTypecheck("ImplicitsInfo", test.src, &info) name := mustTypecheck("ImplicitsInfo", test.src, &info).Name()
// the test cases expect at most one Implicits entry // the test cases expect at most one Implicits entry
if len(info.Implicits) > 1 { if len(info.Implicits) > 1 {
@ -978,7 +978,7 @@ func TestPredicatesInfo(t *testing.T) {
for _, test := range tests { for _, test := range tests {
info := Info{Types: make(map[ast.Expr]TypeAndValue)} info := Info{Types: make(map[ast.Expr]TypeAndValue)}
name := mustTypecheck("PredicatesInfo", test.src, &info) name := mustTypecheck("PredicatesInfo", test.src, &info).Name()
// look for expression predicates // look for expression predicates
got := "<missing>" got := "<missing>"
@ -1070,7 +1070,7 @@ func TestScopesInfo(t *testing.T) {
for _, test := range tests { for _, test := range tests {
info := Info{Scopes: make(map[ast.Node]*Scope)} info := Info{Scopes: make(map[ast.Node]*Scope)}
name := mustTypecheck("ScopesInfo", test.src, &info) name := mustTypecheck("ScopesInfo", test.src, &info).Name()
// number of scopes must match // number of scopes must match
if len(info.Scopes) != len(test.scopes) { if len(info.Scopes) != len(test.scopes) {
@ -1258,7 +1258,7 @@ func TestInitOrderInfo(t *testing.T) {
for _, test := range tests { for _, test := range tests {
info := Info{} info := Info{}
name := mustTypecheck("InitOrderInfo", test.src, &info) name := mustTypecheck("InitOrderInfo", test.src, &info).Name()
// number of initializers must match // number of initializers must match
if len(info.InitOrder) != len(test.inits) { if len(info.InitOrder) != len(test.inits) {
@ -1620,11 +1620,7 @@ func TestLookupFieldOrMethod(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
pkg, err := typecheck("test", "package p;"+test.src, nil) pkg := mustTypecheck("test", "package p;"+test.src, nil)
if err != nil {
t.Errorf("%s: incorrect test case: %s", test.src, err)
continue
}
obj := pkg.Scope().Lookup("a") obj := pkg.Scope().Lookup("a")
if obj == nil { if obj == nil {
@ -1903,11 +1899,7 @@ func TestIdentical(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
pkg, err := typecheck("test", "package p;"+test.src, nil) pkg := mustTypecheck("test", "package p;"+test.src, nil)
if err != nil {
t.Errorf("%s: incorrect test case: %s", test.src, err)
continue
}
X := pkg.Scope().Lookup("X") X := pkg.Scope().Lookup("X")
Y := pkg.Scope().Lookup("Y") Y := pkg.Scope().Lookup("Y")
if X == nil || Y == nil { if X == nil || Y == nil {
@ -2186,10 +2178,7 @@ func f(x T) T { return foo.F(x) }
func TestInstantiate(t *testing.T) { func TestInstantiate(t *testing.T) {
// eventually we like more tests but this is a start // eventually we like more tests but this is a start
const src = "package p; type T[P any] *T[P]" const src = "package p; type T[P any] *T[P]"
pkg, err := typecheck(".", src, nil) pkg := mustTypecheck(".", src, nil)
if err != nil {
t.Fatal(err)
}
// type T should have one type parameter // type T should have one type parameter
T := pkg.Scope().Lookup("T").Type().(*Named) T := pkg.Scope().Lookup("T").Type().(*Named)
@ -2224,14 +2213,11 @@ func TestInstantiateErrors(t *testing.T) {
for _, test := range tests { for _, test := range tests {
src := "package p; " + test.src src := "package p; " + test.src
pkg, err := typecheck(".", src, nil) pkg := mustTypecheck(".", src, nil)
if err != nil {
t.Fatal(err)
}
T := pkg.Scope().Lookup("T").Type().(*Named) T := pkg.Scope().Lookup("T").Type().(*Named)
_, err = Instantiate(nil, T, test.targs, true) _, err := Instantiate(nil, T, test.targs, true)
if err == nil { if err == nil {
t.Fatalf("Instantiate(%v, %v) returned nil error, want non-nil", T, test.targs) t.Fatalf("Instantiate(%v, %v) returned nil error, want non-nil", T, test.targs)
} }
@ -2550,10 +2536,7 @@ type V4 struct{}
func (V4) M() func (V4) M()
` `
pkg, err := typecheck("p.go", src, nil) pkg := mustTypecheck("p.go", src, nil)
if err != nil {
t.Fatal(err)
}
T := pkg.Scope().Lookup("T").Type().Underlying().(*Interface) T := pkg.Scope().Lookup("T").Type().Underlying().(*Interface)
lookup := func(name string) (*Func, bool) { lookup := func(name string) (*Func, bool) {

View File

@ -109,10 +109,7 @@ func TestInstantiateEquality(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
pkg, err := typecheck(".", test.src, nil) pkg := mustTypecheck(".", test.src, nil)
if err != nil {
t.Fatal(err)
}
t.Run(pkg.Name(), func(t *testing.T) { t.Run(pkg.Name(), func(t *testing.T) {
ctxt := NewContext() ctxt := NewContext()
@ -139,14 +136,8 @@ func TestInstantiateEquality(t *testing.T) {
func TestInstantiateNonEquality(t *testing.T) { func TestInstantiateNonEquality(t *testing.T) {
const src = "package p; type T[P any] int" const src = "package p; type T[P any] int"
pkg1, err := typecheck(".", src, nil) pkg1 := mustTypecheck(".", src, nil)
if err != nil { pkg2 := mustTypecheck(".", src, nil)
t.Fatal(err)
}
pkg2, err := typecheck(".", src, nil)
if err != nil {
t.Fatal(err)
}
// We consider T1 and T2 to be distinct types, so their instances should not // We consider T1 and T2 to be distinct types, so their instances should not
// be deduplicated by the context. // be deduplicated by the context.
@ -194,10 +185,7 @@ var X T[int]
for _, test := range tests { for _, test := range tests {
src := prefix + test.decl src := prefix + test.decl
pkg, err := typecheck(".", src, nil) pkg := mustTypecheck(".", src, nil)
if err != nil {
t.Fatal(err)
}
typ := NewPointer(pkg.Scope().Lookup("X").Type()) typ := NewPointer(pkg.Scope().Lookup("X").Type())
obj, _, _ := LookupFieldOrMethod(typ, false, pkg, "m") obj, _, _ := LookupFieldOrMethod(typ, false, pkg, "m")
m, _ := obj.(*Func) m, _ := obj.(*Func)
@ -219,10 +207,7 @@ func (T[P]) m() {}
var _ T[int] var _ T[int]
` `
pkg, err := typecheck(".", src, nil) pkg := mustTypecheck(".", src, nil)
if err != nil {
t.Fatal(err)
}
typ := pkg.Scope().Lookup("T").Type().(*Named) typ := pkg.Scope().Lookup("T").Type().(*Named)
obj, _, _ := LookupFieldOrMethod(typ, false, pkg, "m") obj, _, _ := LookupFieldOrMethod(typ, false, pkg, "m")
if obj == nil { if obj == nil {

View File

@ -450,10 +450,7 @@ func TestIssue34151(t *testing.T) {
const asrc = `package a; type I interface{ M() }; type T struct { F interface { I } }` 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{})` const bsrc = `package b; import "a"; type T struct { F interface { a.I } }; var _ = a.T(T{})`
a, err := typecheck("a", asrc, nil) a := mustTypecheck("a", asrc, nil)
if err != nil {
t.Fatalf("package %s failed to typecheck: %v", a.Name(), err)
}
bast := mustParse(fset, "", bsrc) bast := mustParse(fset, "", bsrc)
conf := Config{Importer: importHelper{pkg: a}} conf := Config{Importer: importHelper{pkg: a}}
@ -609,10 +606,7 @@ var _ T = template /* ERROR cannot use.*text/template.* as T value */.Template{}
` `
) )
a, err := typecheck("a", asrc, nil) a := mustTypecheck("a", asrc, nil)
if err != nil {
t.Fatalf("package a failed to typecheck: %v", err)
}
imp := importHelper{pkg: a, fallback: importer.Default()} imp := importHelper{pkg: a, fallback: importer.Default()}
testFiles(t, nil, []string{"b.go"}, [][]byte{[]byte(bsrc)}, false, imp) testFiles(t, nil, []string{"b.go"}, [][]byte{[]byte(bsrc)}, false, imp)

View File

@ -84,11 +84,7 @@ func TestNewMethodSet(t *testing.T) {
} }
check := func(src string, methods []method, generic bool) { check := func(src string, methods []method, generic bool) {
pkg, err := typecheck("test", "package p;"+src, nil) pkg := mustTypecheck("test", "package p;"+src, nil)
if err != nil {
t.Errorf("%s: incorrect test case: %s", src, err)
return
}
scope := pkg.Scope() scope := pkg.Scope()
if generic { if generic {

View File

@ -32,10 +32,7 @@ func (G[P]) N() (p P) { return }
type Inst = G[int] type Inst = G[int]
` `
pkg, err := typecheck("p", src, nil) pkg := mustTypecheck("p", src, nil)
if err != nil {
b.Fatal(err)
}
var ( var (
T = pkg.Scope().Lookup("T").Type() T = pkg.Scope().Lookup("T").Type()

View File

@ -21,14 +21,9 @@ func findStructType(t *testing.T, src string) *types.Struct {
} }
func findStructTypeConfig(t *testing.T, src string, conf *types.Config) *types.Struct { func findStructTypeConfig(t *testing.T, src string, conf *types.Config) *types.Struct {
fset := token.NewFileSet() types_ := make(map[ast.Expr]types.TypeAndValue)
f := mustParse(fset, "x.go", src) mustTypecheck("x", src, &types.Info{Types: types_})
info := types.Info{Types: make(map[ast.Expr]types.TypeAndValue)} for _, tv := range types_ {
_, err := conf.Check("x", fset, []*ast.File{f}, &info)
if err != nil {
t.Fatal(err)
}
for _, tv := range info.Types {
if ts, ok := tv.Type.(*types.Struct); ok { if ts, ok := tv.Type.(*types.Struct); ok {
return ts return ts
} }

View File

@ -137,8 +137,8 @@ func TestTypeString(t *testing.T) {
} }
func TestQualifiedTypeString(t *testing.T) { func TestQualifiedTypeString(t *testing.T) {
p, _ := typecheck("p.go", "package p; type T int", nil) p := mustTypecheck("p.go", "package p; type T int", nil)
q, _ := typecheck("q.go", "package q", nil) q := mustTypecheck("q.go", "package q", nil)
pT := p.Scope().Lookup("T").Type() pT := p.Scope().Lookup("T").Type()
for _, test := range []struct { for _, test := range []struct {