| 1 | // Copyright 2016 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 gcimporter_test |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "fmt" |
| 10 | "go/ast" |
| 11 | "go/parser" |
| 12 | "go/token" |
| 13 | "go/types" |
| 14 | "path/filepath" |
| 15 | "reflect" |
| 16 | "runtime" |
| 17 | "sort" |
| 18 | "strings" |
| 19 | "testing" |
| 20 | |
| 21 | "golang.org/x/tools/internal/gcimporter" |
| 22 | ) |
| 23 | |
| 24 | var isRace = false |
| 25 | |
| 26 | func fileLine(fset *token.FileSet, obj types.Object) string { |
| 27 | posn := fset.Position(obj.Pos()) |
| 28 | filename := filepath.Clean(strings.ReplaceAll(posn.Filename, "$GOROOT", runtime.GOROOT())) |
| 29 | return fmt.Sprintf("%s:%d", filename, posn.Line) |
| 30 | } |
| 31 | |
| 32 | func equalType(x, y types.Type) error { |
| 33 | x = types.Unalias(x) |
| 34 | y = types.Unalias(y) |
| 35 | if reflect.TypeOf(x) != reflect.TypeOf(y) { |
| 36 | return fmt.Errorf("unequal kinds: %T vs %T", x, y) |
| 37 | } |
| 38 | switch x := x.(type) { |
| 39 | case *types.Interface: |
| 40 | y := y.(*types.Interface) |
| 41 | // TODO(gri): enable separate emission of Embedded interfaces |
| 42 | // and ExplicitMethods then use this logic. |
| 43 | // if x.NumEmbeddeds() != y.NumEmbeddeds() { |
| 44 | // return fmt.Errorf("unequal number of embedded interfaces: %d vs %d", |
| 45 | // x.NumEmbeddeds(), y.NumEmbeddeds()) |
| 46 | // } |
| 47 | // for i := 0; i < x.NumEmbeddeds(); i++ { |
| 48 | // xi := x.Embedded(i) |
| 49 | // yi := y.Embedded(i) |
| 50 | // if xi.String() != yi.String() { |
| 51 | // return fmt.Errorf("mismatched %th embedded interface: %s vs %s", |
| 52 | // i, xi, yi) |
| 53 | // } |
| 54 | // } |
| 55 | // if x.NumExplicitMethods() != y.NumExplicitMethods() { |
| 56 | // return fmt.Errorf("unequal methods: %d vs %d", |
| 57 | // x.NumExplicitMethods(), y.NumExplicitMethods()) |
| 58 | // } |
| 59 | // for i := 0; i < x.NumExplicitMethods(); i++ { |
| 60 | // xm := x.ExplicitMethod(i) |
| 61 | // ym := y.ExplicitMethod(i) |
| 62 | // if xm.Name() != ym.Name() { |
| 63 | // return fmt.Errorf("mismatched %th method: %s vs %s", i, xm, ym) |
| 64 | // } |
| 65 | // if err := equalType(xm.Type(), ym.Type()); err != nil { |
| 66 | // return fmt.Errorf("mismatched %s method: %s", xm.Name(), err) |
| 67 | // } |
| 68 | // } |
| 69 | if x.NumMethods() != y.NumMethods() { |
| 70 | return fmt.Errorf("unequal methods: %d vs %d", |
| 71 | x.NumMethods(), y.NumMethods()) |
| 72 | } |
| 73 | for i := 0; i < x.NumMethods(); i++ { |
| 74 | xm := x.Method(i) |
| 75 | ym := y.Method(i) |
| 76 | if xm.Name() != ym.Name() { |
| 77 | return fmt.Errorf("mismatched %dth method: %s vs %s", i, xm, ym) |
| 78 | } |
| 79 | if err := equalType(xm.Type(), ym.Type()); err != nil { |
| 80 | return fmt.Errorf("mismatched %s method: %s", xm.Name(), err) |
| 81 | } |
| 82 | } |
| 83 | // Constraints are handled explicitly in the *TypeParam case below, so we |
| 84 | // don't yet need to consider embeddeds here. |
| 85 | // TODO(rfindley): consider the type set here. |
| 86 | case *types.Array: |
| 87 | y := y.(*types.Array) |
| 88 | if x.Len() != y.Len() { |
| 89 | return fmt.Errorf("unequal array lengths: %d vs %d", x.Len(), y.Len()) |
| 90 | } |
| 91 | if err := equalType(x.Elem(), y.Elem()); err != nil { |
| 92 | return fmt.Errorf("array elements: %s", err) |
| 93 | } |
| 94 | case *types.Basic: |
| 95 | y := y.(*types.Basic) |
| 96 | if x.Kind() != y.Kind() { |
| 97 | return fmt.Errorf("unequal basic types: %s vs %s", x, y) |
| 98 | } |
| 99 | case *types.Chan: |
| 100 | y := y.(*types.Chan) |
| 101 | if x.Dir() != y.Dir() { |
| 102 | return fmt.Errorf("unequal channel directions: %d vs %d", x.Dir(), y.Dir()) |
| 103 | } |
| 104 | if err := equalType(x.Elem(), y.Elem()); err != nil { |
| 105 | return fmt.Errorf("channel elements: %s", err) |
| 106 | } |
| 107 | case *types.Map: |
| 108 | y := y.(*types.Map) |
| 109 | if err := equalType(x.Key(), y.Key()); err != nil { |
| 110 | return fmt.Errorf("map keys: %s", err) |
| 111 | } |
| 112 | if err := equalType(x.Elem(), y.Elem()); err != nil { |
| 113 | return fmt.Errorf("map values: %s", err) |
| 114 | } |
| 115 | case *types.Named: |
| 116 | y := y.(*types.Named) |
| 117 | return cmpNamed(x, y) |
| 118 | case *types.Pointer: |
| 119 | y := y.(*types.Pointer) |
| 120 | if err := equalType(x.Elem(), y.Elem()); err != nil { |
| 121 | return fmt.Errorf("pointer elements: %s", err) |
| 122 | } |
| 123 | case *types.Signature: |
| 124 | y := y.(*types.Signature) |
| 125 | if err := equalType(x.Params(), y.Params()); err != nil { |
| 126 | return fmt.Errorf("parameters: %s", err) |
| 127 | } |
| 128 | if err := equalType(x.Results(), y.Results()); err != nil { |
| 129 | return fmt.Errorf("results: %s", err) |
| 130 | } |
| 131 | if x.Variadic() != y.Variadic() { |
| 132 | return fmt.Errorf("unequal variadicity: %t vs %t", |
| 133 | x.Variadic(), y.Variadic()) |
| 134 | } |
| 135 | if (x.Recv() != nil) != (y.Recv() != nil) { |
| 136 | return fmt.Errorf("unequal receivers: %s vs %s", x.Recv(), y.Recv()) |
| 137 | } |
| 138 | if x.Recv() != nil { |
| 139 | // TODO(adonovan): fix: this assertion fires for interface methods. |
| 140 | // The type of the receiver of an interface method is a named type |
| 141 | // if the Package was loaded from export data, or an unnamed (interface) |
| 142 | // type if the Package was produced by type-checking ASTs. |
| 143 | // if err := equalType(x.Recv().Type(), y.Recv().Type()); err != nil { |
| 144 | // return fmt.Errorf("receiver: %s", err) |
| 145 | // } |
| 146 | } |
| 147 | if err := equalTypeParams(x.TypeParams(), y.TypeParams()); err != nil { |
| 148 | return fmt.Errorf("type params: %s", err) |
| 149 | } |
| 150 | if err := equalTypeParams(x.RecvTypeParams(), y.RecvTypeParams()); err != nil { |
| 151 | return fmt.Errorf("recv type params: %s", err) |
| 152 | } |
| 153 | case *types.Slice: |
| 154 | y := y.(*types.Slice) |
| 155 | if err := equalType(x.Elem(), y.Elem()); err != nil { |
| 156 | return fmt.Errorf("slice elements: %s", err) |
| 157 | } |
| 158 | case *types.Struct: |
| 159 | y := y.(*types.Struct) |
| 160 | if x.NumFields() != y.NumFields() { |
| 161 | return fmt.Errorf("unequal struct fields: %d vs %d", |
| 162 | x.NumFields(), y.NumFields()) |
| 163 | } |
| 164 | for i := 0; i < x.NumFields(); i++ { |
| 165 | xf := x.Field(i) |
| 166 | yf := y.Field(i) |
| 167 | if xf.Name() != yf.Name() { |
| 168 | return fmt.Errorf("mismatched fields: %s vs %s", xf, yf) |
| 169 | } |
| 170 | if err := equalType(xf.Type(), yf.Type()); err != nil { |
| 171 | return fmt.Errorf("struct field %s: %s", xf.Name(), err) |
| 172 | } |
| 173 | if x.Tag(i) != y.Tag(i) { |
| 174 | return fmt.Errorf("struct field %s has unequal tags: %q vs %q", |
| 175 | xf.Name(), x.Tag(i), y.Tag(i)) |
| 176 | } |
| 177 | } |
| 178 | case *types.Tuple: |
| 179 | y := y.(*types.Tuple) |
| 180 | if x.Len() != y.Len() { |
| 181 | return fmt.Errorf("unequal tuple lengths: %d vs %d", x.Len(), y.Len()) |
| 182 | } |
| 183 | for i := 0; i < x.Len(); i++ { |
| 184 | if err := equalType(x.At(i).Type(), y.At(i).Type()); err != nil { |
| 185 | return fmt.Errorf("tuple element %d: %s", i, err) |
| 186 | } |
| 187 | } |
| 188 | case *types.TypeParam: |
| 189 | y := y.(*types.TypeParam) |
| 190 | if x.String() != y.String() { |
| 191 | return fmt.Errorf("unequal named types: %s vs %s", x, y) |
| 192 | } |
| 193 | // For now, just compare constraints by type string to short-circuit |
| 194 | // cycles. We have to make interfaces explicit as export data currently |
| 195 | // doesn't support marking interfaces as implicit. |
| 196 | // TODO(rfindley): remove makeExplicit once export data contains an |
| 197 | // implicit bit. |
| 198 | xc := makeExplicit(x.Constraint()).String() |
| 199 | yc := makeExplicit(y.Constraint()).String() |
| 200 | if xc != yc { |
| 201 | return fmt.Errorf("unequal constraints: %s vs %s", xc, yc) |
| 202 | } |
| 203 | |
| 204 | default: |
| 205 | panic(fmt.Sprintf("unexpected %T type", x)) |
| 206 | } |
| 207 | return nil |
| 208 | } |
| 209 | |
| 210 | // cmpNamed compares two named types x and y, returning an error for any |
| 211 | // discrepancies. It does not compare their underlying types. |
| 212 | func cmpNamed(x, y *types.Named) error { |
| 213 | xOrig := x.Origin() |
| 214 | yOrig := y.Origin() |
| 215 | if xOrig.String() != yOrig.String() { |
| 216 | return fmt.Errorf("unequal named types: %s vs %s", x, y) |
| 217 | } |
| 218 | if err := equalTypeParams(x.TypeParams(), y.TypeParams()); err != nil { |
| 219 | return fmt.Errorf("type parameters: %s", err) |
| 220 | } |
| 221 | if err := equalTypeArgs(x.TypeArgs(), y.TypeArgs()); err != nil { |
| 222 | return fmt.Errorf("type arguments: %s", err) |
| 223 | } |
| 224 | if x.NumMethods() != y.NumMethods() { |
| 225 | return fmt.Errorf("unequal methods: %d vs %d", |
| 226 | x.NumMethods(), y.NumMethods()) |
| 227 | } |
| 228 | // Unfortunately method sorting is not canonical, so sort before comparing. |
| 229 | var xms, yms []*types.Func |
| 230 | for i := 0; i < x.NumMethods(); i++ { |
| 231 | xms = append(xms, x.Method(i)) |
| 232 | yms = append(yms, y.Method(i)) |
| 233 | } |
| 234 | for _, ms := range [][]*types.Func{xms, yms} { |
| 235 | sort.Slice(ms, func(i, j int) bool { |
| 236 | return ms[i].Name() < ms[j].Name() |
| 237 | }) |
| 238 | } |
| 239 | for i, xm := range xms { |
| 240 | ym := yms[i] |
| 241 | if xm.Name() != ym.Name() { |
| 242 | return fmt.Errorf("mismatched %dth method: %s vs %s", i, xm, ym) |
| 243 | } |
| 244 | // Calling equalType here leads to infinite recursion, so just compare |
| 245 | // strings. |
| 246 | if xm.String() != ym.String() { |
| 247 | return fmt.Errorf("unequal methods: %s vs %s", x, y) |
| 248 | } |
| 249 | } |
| 250 | return nil |
| 251 | } |
| 252 | |
| 253 | // makeExplicit returns an explicit version of typ, if typ is an implicit |
| 254 | // interface. Otherwise it returns typ unmodified. |
| 255 | func makeExplicit(typ types.Type) types.Type { |
| 256 | if iface, _ := typ.(*types.Interface); iface != nil && iface.IsImplicit() { |
| 257 | var methods []*types.Func |
| 258 | for i := 0; i < iface.NumExplicitMethods(); i++ { |
| 259 | methods = append(methods, iface.Method(i)) |
| 260 | } |
| 261 | var embeddeds []types.Type |
| 262 | for etyp := range iface.EmbeddedTypes() { |
| 263 | embeddeds = append(embeddeds, etyp) |
| 264 | } |
| 265 | return types.NewInterfaceType(methods, embeddeds) |
| 266 | } |
| 267 | return typ |
| 268 | } |
| 269 | |
| 270 | func equalTypeArgs(x, y *types.TypeList) error { |
| 271 | if x.Len() != y.Len() { |
| 272 | return fmt.Errorf("unequal lengths: %d vs %d", x.Len(), y.Len()) |
| 273 | } |
| 274 | for i := 0; i < x.Len(); i++ { |
| 275 | if err := equalType(x.At(i), y.At(i)); err != nil { |
| 276 | return fmt.Errorf("type %d: %s", i, err) |
| 277 | } |
| 278 | } |
| 279 | return nil |
| 280 | } |
| 281 | |
| 282 | func equalTypeParams(x, y *types.TypeParamList) error { |
| 283 | if x.Len() != y.Len() { |
| 284 | return fmt.Errorf("unequal lengths: %d vs %d", x.Len(), y.Len()) |
| 285 | } |
| 286 | for i := 0; i < x.Len(); i++ { |
| 287 | if err := equalType(x.At(i), y.At(i)); err != nil { |
| 288 | return fmt.Errorf("type parameter %d: %s", i, err) |
| 289 | } |
| 290 | } |
| 291 | return nil |
| 292 | } |
| 293 | |
| 294 | // TestVeryLongFile tests the position of an import object declared in |
| 295 | // a very long input file. Line numbers greater than maxlines are |
| 296 | // reported as line 1, not garbage or token.NoPos. |
| 297 | func TestVeryLongFile(t *testing.T) { |
| 298 | // parse and typecheck |
| 299 | longFile := "package foo" + strings.Repeat("\n", 123456) + "var X int" |
| 300 | fset1 := token.NewFileSet() |
| 301 | f, err := parser.ParseFile(fset1, "foo.go", longFile, 0) |
| 302 | if err != nil { |
| 303 | t.Fatal(err) |
| 304 | } |
| 305 | var conf types.Config |
| 306 | pkg, err := conf.Check("foo", fset1, []*ast.File{f}, nil) |
| 307 | if err != nil { |
| 308 | t.Fatal(err) |
| 309 | } |
| 310 | |
| 311 | // export |
| 312 | var out bytes.Buffer |
| 313 | if err := gcimporter.IExportData(&out, fset1, pkg); err != nil { |
| 314 | t.Fatal(err) |
| 315 | } |
| 316 | exportdata := out.Bytes() |
| 317 | |
| 318 | // import |
| 319 | imports := make(map[string]*types.Package) |
| 320 | fset2 := token.NewFileSet() |
| 321 | _, pkg2, err := gcimporter.IImportData(fset2, imports, exportdata, pkg.Path()) |
| 322 | if err != nil { |
| 323 | t.Fatalf("BImportData(%s): %v", pkg.Path(), err) |
| 324 | } |
| 325 | |
| 326 | // compare |
| 327 | posn1 := fset1.Position(pkg.Scope().Lookup("X").Pos()) |
| 328 | posn2 := fset2.Position(pkg2.Scope().Lookup("X").Pos()) |
| 329 | if want := "foo.go:1:1"; posn2.String() != want { |
| 330 | t.Errorf("X position = %s, want %s (orig was %s)", |
| 331 | posn2, want, posn1) |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | const src = ` |
| 336 | package p |
| 337 | |
| 338 | type ( |
| 339 | T0 = int32 |
| 340 | T1 = struct{} |
| 341 | T2 = struct{ T1 } |
| 342 | Invalid = foo // foo is undeclared |
| 343 | ) |
| 344 | ` |
| 345 | |
| 346 | func checkPkg(t *testing.T, pkg *types.Package, label string) { |
| 347 | T1 := types.NewStruct(nil, nil) |
| 348 | T2 := types.NewStruct([]*types.Var{types.NewField(0, pkg, "T1", T1, true)}, nil) |
| 349 | |
| 350 | for _, test := range []struct { |
| 351 | name string |
| 352 | typ types.Type |
| 353 | }{ |
| 354 | {"T0", types.Typ[types.Int32]}, |
| 355 | {"T1", T1}, |
| 356 | {"T2", T2}, |
| 357 | {"Invalid", types.Typ[types.Invalid]}, |
| 358 | } { |
| 359 | obj := pkg.Scope().Lookup(test.name) |
| 360 | if obj == nil { |
| 361 | t.Errorf("%s: %s not found", label, test.name) |
| 362 | continue |
| 363 | } |
| 364 | tname, _ := obj.(*types.TypeName) |
| 365 | if tname == nil { |
| 366 | t.Errorf("%s: %v not a type name", label, obj) |
| 367 | continue |
| 368 | } |
| 369 | if !tname.IsAlias() { |
| 370 | t.Errorf("%s: %v: not marked as alias", label, tname) |
| 371 | continue |
| 372 | } |
| 373 | if got := tname.Type(); !types.Identical(got, test.typ) { |
| 374 | t.Errorf("%s: %v: got %v; want %v", label, tname, got, test.typ) |
| 375 | } |
| 376 | } |
| 377 | } |