...

Source file src/cmd/compile/internal/importer/gcimporter_test.go

Documentation: cmd/compile/internal/importer

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package importer
     6  
     7  import (
     8  	"bytes"
     9  	"cmd/compile/internal/types2"
    10  	"fmt"
    11  	"go/build"
    12  	"internal/testenv"
    13  	"os"
    14  	"os/exec"
    15  	"path"
    16  	"path/filepath"
    17  	"runtime"
    18  	"strings"
    19  	"testing"
    20  	"time"
    21  )
    22  
    23  func TestMain(m *testing.M) {
    24  	build.Default.GOROOT = testenv.GOROOT(nil)
    25  	os.Exit(m.Run())
    26  }
    27  
    28  // compile runs the compiler on filename, with dirname as the working directory,
    29  // and writes the output file to outdirname.
    30  // compile gives the resulting package a packagepath of testdata/<filebasename>.
    31  func compile(t *testing.T, dirname, filename, outdirname string, packagefiles map[string]string) string {
    32  	// filename must end with ".go"
    33  	basename, ok := strings.CutSuffix(filepath.Base(filename), ".go")
    34  	if !ok {
    35  		t.Helper()
    36  		t.Fatalf("filename doesn't end in .go: %s", filename)
    37  	}
    38  	objname := basename + ".o"
    39  	outname := filepath.Join(outdirname, objname)
    40  	pkgpath := path.Join("testdata", basename)
    41  
    42  	importcfgfile := os.DevNull
    43  	if len(packagefiles) > 0 {
    44  		importcfgfile = filepath.Join(outdirname, basename) + ".importcfg"
    45  		importcfg := new(bytes.Buffer)
    46  		for k, v := range packagefiles {
    47  			fmt.Fprintf(importcfg, "packagefile %s=%s\n", k, v)
    48  		}
    49  		if err := os.WriteFile(importcfgfile, importcfg.Bytes(), 0655); err != nil {
    50  			t.Fatal(err)
    51  		}
    52  	}
    53  
    54  	cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-p", pkgpath, "-D", "testdata", "-importcfg", importcfgfile, "-o", outname, filename)
    55  	cmd.Dir = dirname
    56  	out, err := cmd.CombinedOutput()
    57  	if err != nil {
    58  		t.Helper()
    59  		t.Logf("%s", out)
    60  		t.Fatalf("go tool compile %s failed: %s", filename, err)
    61  	}
    62  	return outname
    63  }
    64  
    65  func testPath(t *testing.T, path, srcDir string) *types2.Package {
    66  	t0 := time.Now()
    67  	pkg, err := Import(make(map[string]*types2.Package), path, srcDir, nil)
    68  	if err != nil {
    69  		t.Errorf("testPath(%s): %s", path, err)
    70  		return nil
    71  	}
    72  	t.Logf("testPath(%s): %v", path, time.Since(t0))
    73  	return pkg
    74  }
    75  
    76  func mktmpdir(t *testing.T) string {
    77  	tmpdir := t.TempDir()
    78  	if err := os.Mkdir(filepath.Join(tmpdir, "testdata"), 0700); err != nil {
    79  		t.Fatal("mktmpdir:", err)
    80  	}
    81  	return tmpdir
    82  }
    83  
    84  func TestImportTestdata(t *testing.T) {
    85  	// This package only handles gc export data.
    86  	if runtime.Compiler != "gc" {
    87  		t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
    88  	}
    89  
    90  	testenv.MustHaveGoBuild(t)
    91  
    92  	testfiles := map[string][]string{
    93  		"exports.go":  {"go/ast", "go/token"},
    94  		"generics.go": nil,
    95  	}
    96  	if true /* was goexperiment.Unified */ {
    97  		// TODO(mdempsky): Fix test below to flatten the transitive
    98  		// Package.Imports graph. Unified IR is more precise about
    99  		// recreating the package import graph.
   100  		testfiles["exports.go"] = []string{"go/ast"}
   101  	}
   102  
   103  	for testfile, wantImports := range testfiles {
   104  		tmpdir := mktmpdir(t)
   105  
   106  		importMap := map[string]string{}
   107  		for _, pkg := range wantImports {
   108  			export, _, err := FindPkg(pkg, "testdata")
   109  			if export == "" {
   110  				t.Fatalf("no export data found for %s: %v", pkg, err)
   111  			}
   112  			importMap[pkg] = export
   113  		}
   114  
   115  		compile(t, "testdata", testfile, filepath.Join(tmpdir, "testdata"), importMap)
   116  		path := "./testdata/" + strings.TrimSuffix(testfile, ".go")
   117  
   118  		if pkg := testPath(t, path, tmpdir); pkg != nil {
   119  			// The package's Imports list must include all packages
   120  			// explicitly imported by testfile, plus all packages
   121  			// referenced indirectly via exported objects in testfile.
   122  			got := fmt.Sprint(pkg.Imports())
   123  			for _, want := range wantImports {
   124  				if !strings.Contains(got, want) {
   125  					t.Errorf(`Package("exports").Imports() = %s, does not contain %s`, got, want)
   126  				}
   127  			}
   128  		}
   129  	}
   130  }
   131  
   132  func TestVersionHandling(t *testing.T) {
   133  	testenv.MustHaveGoBuild(t)
   134  
   135  	// This package only handles gc export data.
   136  	if runtime.Compiler != "gc" {
   137  		t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
   138  	}
   139  
   140  	const dir = "./testdata/versions"
   141  	list, err := os.ReadDir(dir)
   142  	if err != nil {
   143  		t.Fatal(err)
   144  	}
   145  
   146  	tmpdir := mktmpdir(t)
   147  	corruptdir := filepath.Join(tmpdir, "testdata", "versions")
   148  	if err := os.Mkdir(corruptdir, 0700); err != nil {
   149  		t.Fatal(err)
   150  	}
   151  
   152  	for _, f := range list {
   153  		name := f.Name()
   154  		if !strings.HasSuffix(name, ".a") {
   155  			continue // not a package file
   156  		}
   157  		if strings.Contains(name, "corrupted") {
   158  			continue // don't process a leftover corrupted file
   159  		}
   160  		pkgpath := "./" + name[:len(name)-2]
   161  
   162  		if testing.Verbose() {
   163  			t.Logf("importing %s", name)
   164  		}
   165  
   166  		// test that export data can be imported
   167  		_, err := Import(make(map[string]*types2.Package), pkgpath, dir, nil)
   168  		if err != nil {
   169  			// ok to fail if it fails with a no longer supported error for select files
   170  			if strings.Contains(err.Error(), "no longer supported") {
   171  				switch name {
   172  				case "test_go1.7_0.a", "test_go1.7_1.a",
   173  					"test_go1.8_4.a", "test_go1.8_5.a",
   174  					"test_go1.11_6b.a", "test_go1.11_999b.a":
   175  					continue
   176  				}
   177  				// fall through
   178  			}
   179  			// ok to fail if it fails with a newer version error for select files
   180  			if strings.Contains(err.Error(), "newer version") {
   181  				switch name {
   182  				case "test_go1.11_999i.a":
   183  					continue
   184  				}
   185  				// fall through
   186  			}
   187  			t.Errorf("import %q failed: %v", pkgpath, err)
   188  			continue
   189  		}
   190  
   191  		// create file with corrupted export data
   192  		// 1) read file
   193  		data, err := os.ReadFile(filepath.Join(dir, name))
   194  		if err != nil {
   195  			t.Fatal(err)
   196  		}
   197  		// 2) find export data
   198  		i := bytes.Index(data, []byte("\n$$B\n")) + 5
   199  		j := bytes.Index(data[i:], []byte("\n$$\n")) + i
   200  		if i < 0 || j < 0 || i > j {
   201  			t.Fatalf("export data section not found (i = %d, j = %d)", i, j)
   202  		}
   203  		// 3) corrupt the data (increment every 7th byte)
   204  		for k := j - 13; k >= i; k -= 7 {
   205  			data[k]++
   206  		}
   207  		// 4) write the file
   208  		pkgpath += "_corrupted"
   209  		filename := filepath.Join(corruptdir, pkgpath) + ".a"
   210  		os.WriteFile(filename, data, 0666)
   211  
   212  		// test that importing the corrupted file results in an error
   213  		_, err = Import(make(map[string]*types2.Package), pkgpath, corruptdir, nil)
   214  		if err == nil {
   215  			t.Errorf("import corrupted %q succeeded", pkgpath)
   216  		} else if msg := err.Error(); !strings.Contains(msg, "version skew") {
   217  			t.Errorf("import %q error incorrect (%s)", pkgpath, msg)
   218  		}
   219  	}
   220  }
   221  
   222  func TestImportStdLib(t *testing.T) {
   223  	if testing.Short() {
   224  		t.Skip("the imports can be expensive, and this test is especially slow when the build cache is empty")
   225  	}
   226  	testenv.MustHaveGoBuild(t)
   227  
   228  	// This package only handles gc export data.
   229  	if runtime.Compiler != "gc" {
   230  		t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
   231  	}
   232  
   233  	// Get list of packages in stdlib. Filter out test-only packages with {{if .GoFiles}} check.
   234  	var stderr bytes.Buffer
   235  	cmd := exec.Command("go", "list", "-f", "{{if .GoFiles}}{{.ImportPath}}{{end}}", "std")
   236  	cmd.Stderr = &stderr
   237  	out, err := cmd.Output()
   238  	if err != nil {
   239  		t.Fatalf("failed to run go list to determine stdlib packages: %v\nstderr:\n%v", err, stderr.String())
   240  	}
   241  	pkgs := strings.Fields(string(out))
   242  
   243  	var nimports int
   244  	for _, pkg := range pkgs {
   245  		t.Run(pkg, func(t *testing.T) {
   246  			if testPath(t, pkg, filepath.Join(testenv.GOROOT(t), "src", path.Dir(pkg))) != nil {
   247  				nimports++
   248  			}
   249  		})
   250  	}
   251  	const minPkgs = 225 // 'GOOS=plan9 go1.18 list std | wc -l' reports 228; most other platforms have more.
   252  	if len(pkgs) < minPkgs {
   253  		t.Fatalf("too few packages (%d) were imported", nimports)
   254  	}
   255  
   256  	t.Logf("tested %d imports", nimports)
   257  }
   258  
   259  var importedObjectTests = []struct {
   260  	name string
   261  	want string
   262  }{
   263  	// non-interfaces
   264  	{"crypto.Hash", "type Hash uint"},
   265  	{"go/ast.ObjKind", "type ObjKind int"},
   266  	{"go/types.Qualifier", "type Qualifier func(*Package) string"},
   267  	{"go/types.Comparable", "func Comparable(T Type) bool"},
   268  	{"math.Pi", "const Pi untyped float"},
   269  	{"math.Sin", "func Sin(x float64) float64"},
   270  	{"go/ast.NotNilFilter", "func NotNilFilter(_ string, v reflect.Value) bool"},
   271  	{"go/internal/gcimporter.FindPkg", "func FindPkg(path string, srcDir string) (filename string, id string, err error)"},
   272  
   273  	// interfaces
   274  	{"context.Context", "type Context interface{Deadline() (deadline time.Time, ok bool); Done() <-chan struct{}; Err() error; Value(key any) any}"},
   275  	{"crypto.Decrypter", "type Decrypter interface{Decrypt(rand io.Reader, msg []byte, opts DecrypterOpts) (plaintext []byte, err error); Public() PublicKey}"},
   276  	{"encoding.BinaryMarshaler", "type BinaryMarshaler interface{MarshalBinary() (data []byte, err error)}"},
   277  	{"io.Reader", "type Reader interface{Read(p []byte) (n int, err error)}"},
   278  	{"io.ReadWriter", "type ReadWriter interface{Reader; Writer}"},
   279  	{"go/ast.Node", "type Node interface{End() go/token.Pos; Pos() go/token.Pos}"},
   280  	{"go/types.Type", "type Type interface{String() string; Underlying() Type}"},
   281  }
   282  
   283  func TestImportedTypes(t *testing.T) {
   284  	testenv.MustHaveGoBuild(t)
   285  
   286  	// This package only handles gc export data.
   287  	if runtime.Compiler != "gc" {
   288  		t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
   289  	}
   290  
   291  	for _, test := range importedObjectTests {
   292  		s := strings.Split(test.name, ".")
   293  		if len(s) != 2 {
   294  			t.Fatal("inconsistent test data")
   295  		}
   296  		importPath := s[0]
   297  		objName := s[1]
   298  
   299  		pkg, err := Import(make(map[string]*types2.Package), importPath, ".", nil)
   300  		if err != nil {
   301  			t.Error(err)
   302  			continue
   303  		}
   304  
   305  		obj := pkg.Scope().Lookup(objName)
   306  		if obj == nil {
   307  			t.Errorf("%s: object not found", test.name)
   308  			continue
   309  		}
   310  
   311  		got := types2.ObjectString(obj, types2.RelativeTo(pkg))
   312  		if got != test.want {
   313  			t.Errorf("%s: got %q; want %q", test.name, got, test.want)
   314  		}
   315  
   316  		if named, _ := obj.Type().(*types2.Named); named != nil {
   317  			verifyInterfaceMethodRecvs(t, named, 0)
   318  		}
   319  	}
   320  }
   321  
   322  // verifyInterfaceMethodRecvs verifies that method receiver types
   323  // are named if the methods belong to a named interface type.
   324  func verifyInterfaceMethodRecvs(t *testing.T, named *types2.Named, level int) {
   325  	// avoid endless recursion in case of an embedding bug that lead to a cycle
   326  	if level > 10 {
   327  		t.Errorf("%s: embeds itself", named)
   328  		return
   329  	}
   330  
   331  	iface, _ := named.Underlying().(*types2.Interface)
   332  	if iface == nil {
   333  		return // not an interface
   334  	}
   335  
   336  	// The unified IR importer always sets interface method receiver
   337  	// parameters to point to the Interface type, rather than the Named.
   338  	// See #49906.
   339  	//
   340  	// TODO(mdempsky): This is only true for the types2 importer. For
   341  	// the go/types importer, we duplicate the Interface and rewrite its
   342  	// receiver methods to match historical behavior.
   343  	var want types2.Type = named
   344  	if true /* was goexperiment.Unified */ {
   345  		want = iface
   346  	}
   347  
   348  	// check explicitly declared methods
   349  	for i := 0; i < iface.NumExplicitMethods(); i++ {
   350  		m := iface.ExplicitMethod(i)
   351  		recv := m.Type().(*types2.Signature).Recv()
   352  		if recv == nil {
   353  			t.Errorf("%s: missing receiver type", m)
   354  			continue
   355  		}
   356  		if recv.Type() != want {
   357  			t.Errorf("%s: got recv type %s; want %s", m, recv.Type(), named)
   358  		}
   359  	}
   360  
   361  	// check embedded interfaces (if they are named, too)
   362  	for i := 0; i < iface.NumEmbeddeds(); i++ {
   363  		// embedding of interfaces cannot have cycles; recursion will terminate
   364  		if etype, _ := iface.EmbeddedType(i).(*types2.Named); etype != nil {
   365  			verifyInterfaceMethodRecvs(t, etype, level+1)
   366  		}
   367  	}
   368  }
   369  
   370  func TestIssue5815(t *testing.T) {
   371  	testenv.MustHaveGoBuild(t)
   372  
   373  	// This package only handles gc export data.
   374  	if runtime.Compiler != "gc" {
   375  		t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
   376  	}
   377  
   378  	pkg := importPkg(t, "strings", ".")
   379  
   380  	scope := pkg.Scope()
   381  	for _, name := range scope.Names() {
   382  		obj := scope.Lookup(name)
   383  		if obj.Pkg() == nil {
   384  			t.Errorf("no pkg for %s", obj)
   385  		}
   386  		if tname, _ := obj.(*types2.TypeName); tname != nil {
   387  			named := tname.Type().(*types2.Named)
   388  			for i := 0; i < named.NumMethods(); i++ {
   389  				m := named.Method(i)
   390  				if m.Pkg() == nil {
   391  					t.Errorf("no pkg for %s", m)
   392  				}
   393  			}
   394  		}
   395  	}
   396  }
   397  
   398  // Smoke test to ensure that imported methods get the correct package.
   399  func TestCorrectMethodPackage(t *testing.T) {
   400  	testenv.MustHaveGoBuild(t)
   401  
   402  	// This package only handles gc export data.
   403  	if runtime.Compiler != "gc" {
   404  		t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
   405  	}
   406  
   407  	imports := make(map[string]*types2.Package)
   408  	_, err := Import(imports, "net/http", ".", nil)
   409  	if err != nil {
   410  		t.Fatal(err)
   411  	}
   412  
   413  	mutex := imports["sync"].Scope().Lookup("Mutex").(*types2.TypeName).Type()
   414  	obj, _, _ := types2.LookupFieldOrMethod(types2.NewPointer(mutex), false, nil, "Lock")
   415  	lock := obj.(*types2.Func)
   416  	if got, want := lock.Pkg().Path(), "sync"; got != want {
   417  		t.Errorf("got package path %q; want %q", got, want)
   418  	}
   419  }
   420  
   421  func TestIssue13566(t *testing.T) {
   422  	testenv.MustHaveGoBuild(t)
   423  
   424  	// This package only handles gc export data.
   425  	if runtime.Compiler != "gc" {
   426  		t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
   427  	}
   428  
   429  	tmpdir := mktmpdir(t)
   430  	testoutdir := filepath.Join(tmpdir, "testdata")
   431  
   432  	// b.go needs to be compiled from the output directory so that the compiler can
   433  	// find the compiled package a. We pass the full path to compile() so that we
   434  	// don't have to copy the file to that directory.
   435  	bpath, err := filepath.Abs(filepath.Join("testdata", "b.go"))
   436  	if err != nil {
   437  		t.Fatal(err)
   438  	}
   439  
   440  	jsonExport, _, err := FindPkg("encoding/json", "testdata")
   441  	if jsonExport == "" {
   442  		t.Fatalf("no export data found for encoding/json: %v", err)
   443  	}
   444  
   445  	compile(t, "testdata", "a.go", testoutdir, map[string]string{"encoding/json": jsonExport})
   446  	compile(t, testoutdir, bpath, testoutdir, map[string]string{"testdata/a": filepath.Join(testoutdir, "a.o")})
   447  
   448  	// import must succeed (test for issue at hand)
   449  	pkg := importPkg(t, "./testdata/b", tmpdir)
   450  
   451  	// make sure all indirectly imported packages have names
   452  	for _, imp := range pkg.Imports() {
   453  		if imp.Name() == "" {
   454  			t.Errorf("no name for %s package", imp.Path())
   455  		}
   456  	}
   457  }
   458  
   459  func TestIssue13898(t *testing.T) {
   460  	testenv.MustHaveGoBuild(t)
   461  
   462  	// This package only handles gc export data.
   463  	if runtime.Compiler != "gc" {
   464  		t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
   465  	}
   466  
   467  	// import go/internal/gcimporter which imports go/types partially
   468  	imports := make(map[string]*types2.Package)
   469  	_, err := Import(imports, "go/internal/gcimporter", ".", nil)
   470  	if err != nil {
   471  		t.Fatal(err)
   472  	}
   473  
   474  	// look for go/types package
   475  	var goTypesPkg *types2.Package
   476  	for path, pkg := range imports {
   477  		if path == "go/types" {
   478  			goTypesPkg = pkg
   479  			break
   480  		}
   481  	}
   482  	if goTypesPkg == nil {
   483  		t.Fatal("go/types not found")
   484  	}
   485  
   486  	// look for go/types.Object type
   487  	obj := lookupObj(t, goTypesPkg.Scope(), "Object")
   488  	typ, ok := obj.Type().(*types2.Named)
   489  	if !ok {
   490  		t.Fatalf("go/types.Object type is %v; wanted named type", typ)
   491  	}
   492  
   493  	// lookup go/types.Object.Pkg method
   494  	m, index, indirect := types2.LookupFieldOrMethod(typ, false, nil, "Pkg")
   495  	if m == nil {
   496  		t.Fatalf("go/types.Object.Pkg not found (index = %v, indirect = %v)", index, indirect)
   497  	}
   498  
   499  	// the method must belong to go/types
   500  	if m.Pkg().Path() != "go/types" {
   501  		t.Fatalf("found %v; want go/types", m.Pkg())
   502  	}
   503  }
   504  
   505  func TestIssue15517(t *testing.T) {
   506  	testenv.MustHaveGoBuild(t)
   507  
   508  	// This package only handles gc export data.
   509  	if runtime.Compiler != "gc" {
   510  		t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
   511  	}
   512  
   513  	tmpdir := mktmpdir(t)
   514  
   515  	compile(t, "testdata", "p.go", filepath.Join(tmpdir, "testdata"), nil)
   516  
   517  	// Multiple imports of p must succeed without redeclaration errors.
   518  	// We use an import path that's not cleaned up so that the eventual
   519  	// file path for the package is different from the package path; this
   520  	// will expose the error if it is present.
   521  	//
   522  	// (Issue: Both the textual and the binary importer used the file path
   523  	// of the package to be imported as key into the shared packages map.
   524  	// However, the binary importer then used the package path to identify
   525  	// the imported package to mark it as complete; effectively marking the
   526  	// wrong package as complete. By using an "unclean" package path, the
   527  	// file and package path are different, exposing the problem if present.
   528  	// The same issue occurs with vendoring.)
   529  	imports := make(map[string]*types2.Package)
   530  	for i := 0; i < 3; i++ {
   531  		if _, err := Import(imports, "./././testdata/p", tmpdir, nil); err != nil {
   532  			t.Fatal(err)
   533  		}
   534  	}
   535  }
   536  
   537  func TestIssue15920(t *testing.T) {
   538  	testenv.MustHaveGoBuild(t)
   539  
   540  	// This package only handles gc export data.
   541  	if runtime.Compiler != "gc" {
   542  		t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
   543  	}
   544  
   545  	compileAndImportPkg(t, "issue15920")
   546  }
   547  
   548  func TestIssue20046(t *testing.T) {
   549  	testenv.MustHaveGoBuild(t)
   550  
   551  	// This package only handles gc export data.
   552  	if runtime.Compiler != "gc" {
   553  		t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
   554  	}
   555  
   556  	// "./issue20046".V.M must exist
   557  	pkg := compileAndImportPkg(t, "issue20046")
   558  	obj := lookupObj(t, pkg.Scope(), "V")
   559  	if m, index, indirect := types2.LookupFieldOrMethod(obj.Type(), false, nil, "M"); m == nil {
   560  		t.Fatalf("V.M not found (index = %v, indirect = %v)", index, indirect)
   561  	}
   562  }
   563  func TestIssue25301(t *testing.T) {
   564  	testenv.MustHaveGoBuild(t)
   565  
   566  	// This package only handles gc export data.
   567  	if runtime.Compiler != "gc" {
   568  		t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
   569  	}
   570  
   571  	compileAndImportPkg(t, "issue25301")
   572  }
   573  
   574  func TestIssue25596(t *testing.T) {
   575  	testenv.MustHaveGoBuild(t)
   576  
   577  	// This package only handles gc export data.
   578  	if runtime.Compiler != "gc" {
   579  		t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
   580  	}
   581  
   582  	compileAndImportPkg(t, "issue25596")
   583  }
   584  
   585  func importPkg(t *testing.T, path, srcDir string) *types2.Package {
   586  	pkg, err := Import(make(map[string]*types2.Package), path, srcDir, nil)
   587  	if err != nil {
   588  		t.Helper()
   589  		t.Fatal(err)
   590  	}
   591  	return pkg
   592  }
   593  
   594  func compileAndImportPkg(t *testing.T, name string) *types2.Package {
   595  	t.Helper()
   596  	tmpdir := mktmpdir(t)
   597  	compile(t, "testdata", name+".go", filepath.Join(tmpdir, "testdata"), nil)
   598  	return importPkg(t, "./testdata/"+name, tmpdir)
   599  }
   600  
   601  func lookupObj(t *testing.T, scope *types2.Scope, name string) types2.Object {
   602  	if obj := scope.Lookup(name); obj != nil {
   603  		return obj
   604  	}
   605  	t.Helper()
   606  	t.Fatalf("%s not found", name)
   607  	return nil
   608  }
   609  

View as plain text