1 // Copyright 2019 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.
5 // This is a copy of bexport_test.go for iexport.go.
10 package gcimporter_test
31 "golang.org/x/tools/go/buildutil"
32 "golang.org/x/tools/go/internal/gcimporter"
33 "golang.org/x/tools/go/loader"
36 func readExportFile(filename string) ([]byte, error) {
37 f, err := os.Open(filename)
43 buf := bufio.NewReader(f)
44 if _, err := gcimporter.FindExportData(buf); err != nil {
48 if ch, err := buf.ReadByte(); err != nil {
51 return nil, fmt.Errorf("unexpected byte: %v", ch)
54 return ioutil.ReadAll(buf)
57 func iexport(fset *token.FileSet, pkg *types.Package) ([]byte, error) {
59 if err := gcimporter.IExportData(&buf, fset, pkg); err != nil {
62 return buf.Bytes(), nil
65 func TestIExportData_stdlib(t *testing.T) {
66 if runtime.Compiler == "gccgo" {
67 t.Skip("gccgo standard library is inaccessible")
69 if runtime.GOOS == "android" {
70 t.Skipf("incomplete std lib on %s", runtime.GOOS)
73 t.Skipf("stdlib tests take too long in race mode and flake on builders")
76 // Load, parse and type-check the program.
77 ctxt := build.Default // copy
78 ctxt.GOPATH = "" // disable GOPATH
79 conf := loader.Config{
82 TypeChecker: types.Config{
83 Sizes: types.SizesFor(ctxt.Compiler, ctxt.GOARCH),
86 for _, path := range buildutil.AllPackages(conf.Build) {
90 // Create a package containing type and value errors to ensure
91 // they are properly encoded/decoded.
92 f, err := conf.ParseFile("haserrors/haserrors.go", `package haserrors
93 const UnknownValue = "" + 0
94 type UnknownType undefined
99 conf.CreateFromFiles("haserrors", f)
101 prog, err := conf.Load()
103 t.Fatalf("Load failed: %v", err)
106 numPkgs := len(prog.AllPackages)
107 if want := 248; numPkgs < want {
108 t.Errorf("Loaded only %d packages, want at least %d", numPkgs, want)
111 var sorted []*types.Package
112 for pkg, info := range prog.AllPackages {
113 if info.Files != nil { // non-empty directory
114 sorted = append(sorted, pkg)
117 sort.Slice(sorted, func(i, j int) bool {
118 return sorted[i].Path() < sorted[j].Path()
121 for _, pkg := range sorted {
122 if exportdata, err := iexport(conf.Fset, pkg); err != nil {
125 testPkgData(t, conf.Fset, pkg, exportdata)
128 if pkg.Name() == "main" || pkg.Name() == "haserrors" {
129 // skip; no export data
130 } else if bp, err := ctxt.Import(pkg.Path(), "", build.FindOnly); err != nil {
131 t.Log("warning:", err)
132 } else if exportdata, err := readExportFile(bp.PkgObj); err != nil {
133 t.Log("warning:", err)
135 testPkgData(t, conf.Fset, pkg, exportdata)
139 var bundle bytes.Buffer
140 if err := gcimporter.IExportBundle(&bundle, conf.Fset, sorted); err != nil {
143 fset2 := token.NewFileSet()
144 imports := make(map[string]*types.Package)
145 pkgs2, err := gcimporter.IImportBundle(fset2, imports, bundle.Bytes())
150 for i, pkg := range sorted {
151 testPkg(t, conf.Fset, pkg, fset2, pkgs2[i])
155 func testPkgData(t *testing.T, fset *token.FileSet, pkg *types.Package, exportdata []byte) {
156 imports := make(map[string]*types.Package)
157 fset2 := token.NewFileSet()
158 _, pkg2, err := gcimporter.IImportData(fset2, imports, exportdata, pkg.Path())
160 t.Errorf("IImportData(%s): %v", pkg.Path(), err)
163 testPkg(t, fset, pkg, fset2, pkg2)
166 func testPkg(t *testing.T, fset *token.FileSet, pkg *types.Package, fset2 *token.FileSet, pkg2 *types.Package) {
167 if _, err := iexport(fset2, pkg2); err != nil {
168 t.Errorf("reexport %q: %v", pkg.Path(), err)
171 // Compare the packages' corresponding members.
172 for _, name := range pkg.Scope().Names() {
173 if !ast.IsExported(name) {
176 obj1 := pkg.Scope().Lookup(name)
177 obj2 := pkg2.Scope().Lookup(name)
179 t.Errorf("%s.%s not found, want %s", pkg.Path(), name, obj1)
183 fl1 := fileLine(fset, obj1)
184 fl2 := fileLine(fset2, obj2)
186 t.Errorf("%s.%s: got posn %s, want %s",
187 pkg.Path(), name, fl2, fl1)
190 if err := cmpObj(obj1, obj2); err != nil {
191 t.Errorf("%s.%s: %s\ngot: %s\nwant: %s",
192 pkg.Path(), name, err, obj2, obj1)
197 // TestVeryLongFile tests the position of an import object declared in
198 // a very long input file. Line numbers greater than maxlines are
199 // reported as line 1, not garbage or token.NoPos.
200 func TestIExportData_long(t *testing.T) {
201 // parse and typecheck
202 longFile := "package foo" + strings.Repeat("\n", 123456) + "var X int"
203 fset1 := token.NewFileSet()
204 f, err := parser.ParseFile(fset1, "foo.go", longFile, 0)
208 var conf types.Config
209 pkg, err := conf.Check("foo", fset1, []*ast.File{f}, nil)
215 exportdata, err := iexport(fset1, pkg)
221 imports := make(map[string]*types.Package)
222 fset2 := token.NewFileSet()
223 _, pkg2, err := gcimporter.IImportData(fset2, imports, exportdata, pkg.Path())
225 t.Fatalf("IImportData(%s): %v", pkg.Path(), err)
229 posn1 := fset1.Position(pkg.Scope().Lookup("X").Pos())
230 posn2 := fset2.Position(pkg2.Scope().Lookup("X").Pos())
231 if want := "foo.go:1:1"; posn2.String() != want {
232 t.Errorf("X position = %s, want %s (orig was %s)",
237 func TestIExportData_typealiases(t *testing.T) {
238 // parse and typecheck
239 fset1 := token.NewFileSet()
240 f, err := parser.ParseFile(fset1, "p.go", src, 0)
244 var conf types.Config
245 pkg1, err := conf.Check("p", fset1, []*ast.File{f}, nil)
247 // foo in undeclared in src; we should see an error
248 t.Fatal("invalid source type-checked without error")
251 // despite incorrect src we should see a (partially) type-checked package
252 t.Fatal("nil package returned")
254 checkPkg(t, pkg1, "export")
257 // use a nil fileset here to confirm that it doesn't panic
258 exportdata, err := iexport(nil, pkg1)
264 imports := make(map[string]*types.Package)
265 fset2 := token.NewFileSet()
266 _, pkg2, err := gcimporter.IImportData(fset2, imports, exportdata, pkg1.Path())
268 t.Fatalf("IImportData(%s): %v", pkg1.Path(), err)
270 checkPkg(t, pkg2, "import")
273 // cmpObj reports how x and y differ. They are assumed to belong to different
274 // universes so cannot be compared directly. It is an adapted version of
275 // equalObj in bexport_test.go.
276 func cmpObj(x, y types.Object) error {
277 if reflect.TypeOf(x) != reflect.TypeOf(y) {
278 return fmt.Errorf("%T vs %T", x, y)
283 case *types.Var, *types.Func:
286 xval := x.(*types.Const).Val()
287 yval := y.(*types.Const).Val()
288 equal := constant.Compare(xval, token.EQL, yval)
290 // try approx. comparison
293 if xkind == constant.Complex || ykind == constant.Complex {
294 equal = same(constant.Real(xval), constant.Real(yval)) &&
295 same(constant.Imag(xval), constant.Imag(yval))
296 } else if xkind == constant.Float || ykind == constant.Float {
297 equal = same(xval, yval)
298 } else if xkind == constant.Unknown && ykind == constant.Unknown {
303 return fmt.Errorf("unequal constants %s vs %s", xval, yval)
305 case *types.TypeName:
309 return fmt.Errorf("unexpected %T", x)
311 return equalType(xt, yt)
314 // Use the same floating-point precision (512) as cmd/compile
315 // (see Mpprec in cmd/compile/internal/gc/mpfloat.go).
318 // same compares non-complex numeric values and reports if they are approximately equal.
319 func same(x, y constant.Value) bool {
320 xf := constantToFloat(x)
321 yf := constantToFloat(y)
322 d := new(big.Float).Sub(xf, yf)
324 eps := big.NewFloat(1.0 / (1 << (mpprec - 1))) // allow for 1 bit of error
325 return d.Cmp(eps) < 0
328 // copy of the function with the same name in iexport.go.
329 func constantToFloat(x constant.Value) *big.Float {
332 if v, exact := constant.Float64Val(x); exact {
335 } else if num, denom := constant.Num(x), constant.Denom(x); num.Kind() == constant.Int {
336 // TODO(gri): add big.Rat accessor to constant.Value.
338 d := valueToRat(denom)
339 f.SetRat(n.Quo(n, d))
341 // Value too large to represent as a fraction => inaccessible.
342 // TODO(gri): add big.Float accessor to constant.Value.
343 _, ok := f.SetString(x.ExactString())
345 panic("should not reach here")
351 // copy of the function with the same name in iexport.go.
352 func valueToRat(x constant.Value) *big.Rat {
353 // Convert little-endian to big-endian.
354 // I can't believe this is necessary.
355 bytes := constant.Bytes(x)
356 for i := 0; i < len(bytes)/2; i++ {
357 bytes[i], bytes[len(bytes)-1-i] = bytes[len(bytes)-1-i], bytes[i]
359 return new(big.Rat).SetInt(new(big.Int).SetBytes(bytes))