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.
9 package gcimporter_test
26 "golang.org/x/tools/go/buildutil"
27 "golang.org/x/tools/go/internal/gcimporter"
28 "golang.org/x/tools/go/loader"
31 func TestIExportData_stdlib(t *testing.T) {
32 if runtime.Compiler == "gccgo" {
33 t.Skip("gccgo standard library is inaccessible")
35 if runtime.GOOS == "android" {
36 t.Skipf("incomplete std lib on %s", runtime.GOOS)
39 t.Skipf("stdlib tests take too long in race mode and flake on builders")
42 // Load, parse and type-check the program.
43 ctxt := build.Default // copy
44 ctxt.GOPATH = "" // disable GOPATH
45 conf := loader.Config{
49 for _, path := range buildutil.AllPackages(conf.Build) {
53 // Create a package containing type and value errors to ensure
54 // they are properly encoded/decoded.
55 f, err := conf.ParseFile("haserrors/haserrors.go", `package haserrors
56 const UnknownValue = "" + 0
57 type UnknownType undefined
62 conf.CreateFromFiles("haserrors", f)
64 prog, err := conf.Load()
66 t.Fatalf("Load failed: %v", err)
69 numPkgs := len(prog.AllPackages)
70 if want := 248; numPkgs < want {
71 t.Errorf("Loaded only %d packages, want at least %d", numPkgs, want)
74 var sorted []*types.Package
75 for pkg := range prog.AllPackages {
76 sorted = append(sorted, pkg)
78 sort.Slice(sorted, func(i, j int) bool {
79 return sorted[i].Path() < sorted[j].Path()
82 for _, pkg := range sorted {
83 info := prog.AllPackages[pkg]
84 if info.Files == nil {
85 continue // empty directory
87 exportdata, err := gcimporter.IExportData(conf.Fset, pkg)
91 if exportdata[0] == 'i' {
92 exportdata = exportdata[1:] // trim the 'i' in the header
94 t.Fatalf("unexpected first character of export data: %v", exportdata[0])
97 imports := make(map[string]*types.Package)
98 fset2 := token.NewFileSet()
99 n, pkg2, err := gcimporter.IImportData(fset2, imports, exportdata, pkg.Path())
101 t.Errorf("IImportData(%s): %v", pkg.Path(), err)
104 if n != len(exportdata) {
105 t.Errorf("IImportData(%s) decoded %d bytes, want %d",
106 pkg.Path(), n, len(exportdata))
109 // Compare the packages' corresponding members.
110 for _, name := range pkg.Scope().Names() {
111 if !ast.IsExported(name) {
114 obj1 := pkg.Scope().Lookup(name)
115 obj2 := pkg2.Scope().Lookup(name)
117 t.Fatalf("%s.%s not found, want %s", pkg.Path(), name, obj1)
121 fl1 := fileLine(conf.Fset, obj1)
122 fl2 := fileLine(fset2, obj2)
124 t.Errorf("%s.%s: got posn %s, want %s",
125 pkg.Path(), name, fl2, fl1)
128 if err := cmpObj(obj1, obj2); err != nil {
129 t.Errorf("%s.%s: %s\ngot: %s\nwant: %s",
130 pkg.Path(), name, err, obj2, obj1)
136 // TestVeryLongFile tests the position of an import object declared in
137 // a very long input file. Line numbers greater than maxlines are
138 // reported as line 1, not garbage or token.NoPos.
139 func TestIExportData_long(t *testing.T) {
140 // parse and typecheck
141 longFile := "package foo" + strings.Repeat("\n", 123456) + "var X int"
142 fset1 := token.NewFileSet()
143 f, err := parser.ParseFile(fset1, "foo.go", longFile, 0)
147 var conf types.Config
148 pkg, err := conf.Check("foo", fset1, []*ast.File{f}, nil)
154 exportdata, err := gcimporter.IExportData(fset1, pkg)
158 if exportdata[0] == 'i' {
159 exportdata = exportdata[1:] // trim the 'i' in the header
161 t.Fatalf("unexpected first character of export data: %v", exportdata[0])
165 imports := make(map[string]*types.Package)
166 fset2 := token.NewFileSet()
167 _, pkg2, err := gcimporter.IImportData(fset2, imports, exportdata, pkg.Path())
169 t.Fatalf("IImportData(%s): %v", pkg.Path(), err)
173 posn1 := fset1.Position(pkg.Scope().Lookup("X").Pos())
174 posn2 := fset2.Position(pkg2.Scope().Lookup("X").Pos())
175 if want := "foo.go:1:1"; posn2.String() != want {
176 t.Errorf("X position = %s, want %s (orig was %s)",
181 func TestIExportData_typealiases(t *testing.T) {
182 // parse and typecheck
183 fset1 := token.NewFileSet()
184 f, err := parser.ParseFile(fset1, "p.go", src, 0)
188 var conf types.Config
189 pkg1, err := conf.Check("p", fset1, []*ast.File{f}, nil)
191 // foo in undeclared in src; we should see an error
192 t.Fatal("invalid source type-checked without error")
195 // despite incorrect src we should see a (partially) type-checked package
196 t.Fatal("nil package returned")
198 checkPkg(t, pkg1, "export")
201 // use a nil fileset here to confirm that it doesn't panic
202 exportdata, err := gcimporter.IExportData(nil, pkg1)
206 if exportdata[0] == 'i' {
207 exportdata = exportdata[1:] // trim the 'i' in the header
209 t.Fatalf("unexpected first character of export data: %v", exportdata[0])
213 imports := make(map[string]*types.Package)
214 fset2 := token.NewFileSet()
215 _, pkg2, err := gcimporter.IImportData(fset2, imports, exportdata, pkg1.Path())
217 t.Fatalf("IImportData(%s): %v", pkg1.Path(), err)
219 checkPkg(t, pkg2, "import")
222 // cmpObj reports how x and y differ. They are assumed to belong to different
223 // universes so cannot be compared directly. It is an adapted version of
224 // equalObj in bexport_test.go.
225 func cmpObj(x, y types.Object) error {
226 if reflect.TypeOf(x) != reflect.TypeOf(y) {
227 return fmt.Errorf("%T vs %T", x, y)
232 case *types.Var, *types.Func:
235 xval := x.(*types.Const).Val()
236 yval := y.(*types.Const).Val()
237 equal := constant.Compare(xval, token.EQL, yval)
239 // try approx. comparison
242 if xkind == constant.Complex || ykind == constant.Complex {
243 equal = same(constant.Real(xval), constant.Real(yval)) &&
244 same(constant.Imag(xval), constant.Imag(yval))
245 } else if xkind == constant.Float || ykind == constant.Float {
246 equal = same(xval, yval)
247 } else if xkind == constant.Unknown && ykind == constant.Unknown {
252 return fmt.Errorf("unequal constants %s vs %s", xval, yval)
254 case *types.TypeName:
258 return fmt.Errorf("unexpected %T", x)
260 return equalType(xt, yt)
263 // Use the same floating-point precision (512) as cmd/compile
264 // (see Mpprec in cmd/compile/internal/gc/mpfloat.go).
267 // same compares non-complex numeric values and reports if they are approximately equal.
268 func same(x, y constant.Value) bool {
269 xf := constantToFloat(x)
270 yf := constantToFloat(y)
271 d := new(big.Float).Sub(xf, yf)
273 eps := big.NewFloat(1.0 / (1 << (mpprec - 1))) // allow for 1 bit of error
274 return d.Cmp(eps) < 0
277 // copy of the function with the same name in iexport.go.
278 func constantToFloat(x constant.Value) *big.Float {
281 if v, exact := constant.Float64Val(x); exact {
284 } else if num, denom := constant.Num(x), constant.Denom(x); num.Kind() == constant.Int {
285 // TODO(gri): add big.Rat accessor to constant.Value.
287 d := valueToRat(denom)
288 f.SetRat(n.Quo(n, d))
290 // Value too large to represent as a fraction => inaccessible.
291 // TODO(gri): add big.Float accessor to constant.Value.
292 _, ok := f.SetString(x.ExactString())
294 panic("should not reach here")
300 // copy of the function with the same name in iexport.go.
301 func valueToRat(x constant.Value) *big.Rat {
302 // Convert little-endian to big-endian.
303 // I can't believe this is necessary.
304 bytes := constant.Bytes(x)
305 for i := 0; i < len(bytes)/2; i++ {
306 bytes[i], bytes[len(bytes)-1-i] = bytes[len(bytes)-1-i], bytes[i]
308 return new(big.Rat).SetInt(new(big.Int).SetBytes(bytes))