func equalType(x, y types.Type) error { if reflect.TypeOf(x) != reflect.TypeOf(y) { return fmt.Errorf("unequal kinds: %T vs %T", x, y) } switch x := x.(type) { case *types.Interface: y := y.(*types.Interface) // TODO(gri): enable separate emission of Embedded interfaces // and ExplicitMethods then use this logic. // if x.NumEmbeddeds() != y.NumEmbeddeds() { // return fmt.Errorf("unequal number of embedded interfaces: %d vs %d", // x.NumEmbeddeds(), y.NumEmbeddeds()) // } // for i := 0; i < x.NumEmbeddeds(); i++ { // xi := x.Embedded(i) // yi := y.Embedded(i) // if xi.String() != yi.String() { // return fmt.Errorf("mismatched %th embedded interface: %s vs %s", // i, xi, yi) // } // } // if x.NumExplicitMethods() != y.NumExplicitMethods() { // return fmt.Errorf("unequal methods: %d vs %d", // x.NumExplicitMethods(), y.NumExplicitMethods()) // } // for i := 0; i < x.NumExplicitMethods(); i++ { // xm := x.ExplicitMethod(i) // ym := y.ExplicitMethod(i) // if xm.Name() != ym.Name() { // return fmt.Errorf("mismatched %th method: %s vs %s", i, xm, ym) // } // if err := equalType(xm.Type(), ym.Type()); err != nil { // return fmt.Errorf("mismatched %s method: %s", xm.Name(), err) // } // } if x.NumMethods() != y.NumMethods() { return fmt.Errorf("unequal methods: %d vs %d", x.NumMethods(), y.NumMethods()) } for i := 0; i < x.NumMethods(); i++ { xm := x.Method(i) ym := y.Method(i) if xm.Name() != ym.Name() { return fmt.Errorf("mismatched %dth method: %s vs %s", i, xm, ym) } if err := equalType(xm.Type(), ym.Type()); err != nil { return fmt.Errorf("mismatched %s method: %s", xm.Name(), err) } } case *types.Array: y := y.(*types.Array) if x.Len() != y.Len() { return fmt.Errorf("unequal array lengths: %d vs %d", x.Len(), y.Len()) } if err := equalType(x.Elem(), y.Elem()); err != nil { return fmt.Errorf("array elements: %s", err) } case *types.Basic: y := y.(*types.Basic) if x.Kind() != y.Kind() { return fmt.Errorf("unequal basic types: %s vs %s", x, y) } case *types.Chan: y := y.(*types.Chan) if x.Dir() != y.Dir() { return fmt.Errorf("unequal channel directions: %d vs %d", x.Dir(), y.Dir()) } if err := equalType(x.Elem(), y.Elem()); err != nil { return fmt.Errorf("channel elements: %s", err) } case *types.Map: y := y.(*types.Map) if err := equalType(x.Key(), y.Key()); err != nil { return fmt.Errorf("map keys: %s", err) } if err := equalType(x.Elem(), y.Elem()); err != nil { return fmt.Errorf("map values: %s", err) } case *types.Named: y := y.(*types.Named) if x.String() != y.String() { return fmt.Errorf("unequal named types: %s vs %s", x, y) } case *types.Pointer: y := y.(*types.Pointer) if err := equalType(x.Elem(), y.Elem()); err != nil { return fmt.Errorf("pointer elements: %s", err) } case *types.Signature: y := y.(*types.Signature) if err := equalType(x.Params(), y.Params()); err != nil { return fmt.Errorf("parameters: %s", err) } if err := equalType(x.Results(), y.Results()); err != nil { return fmt.Errorf("results: %s", err) } if x.Variadic() != y.Variadic() { return fmt.Errorf("unequal varidicity: %t vs %t", x.Variadic(), y.Variadic()) } if (x.Recv() != nil) != (y.Recv() != nil) { return fmt.Errorf("unequal receivers: %s vs %s", x.Recv(), y.Recv()) } if x.Recv() != nil { // TODO(adonovan): fix: this assertion fires for interface methods. // The type of the receiver of an interface method is a named type // if the Package was loaded from export data, or an unnamed (interface) // type if the Package was produced by type-checking ASTs. // if err := equalType(x.Recv().Type(), y.Recv().Type()); err != nil { // return fmt.Errorf("receiver: %s", err) // } } case *types.Slice: y := y.(*types.Slice) if err := equalType(x.Elem(), y.Elem()); err != nil { return fmt.Errorf("slice elements: %s", err) } case *types.Struct: y := y.(*types.Struct) if x.NumFields() != y.NumFields() { return fmt.Errorf("unequal struct fields: %d vs %d", x.NumFields(), y.NumFields()) } for i := 0; i < x.NumFields(); i++ { xf := x.Field(i) yf := y.Field(i) if xf.Name() != yf.Name() { return fmt.Errorf("mismatched fields: %s vs %s", xf, yf) } if err := equalType(xf.Type(), yf.Type()); err != nil { return fmt.Errorf("struct field %s: %s", xf.Name(), err) } if x.Tag(i) != y.Tag(i) { return fmt.Errorf("struct field %s has unequal tags: %q vs %q", xf.Name(), x.Tag(i), y.Tag(i)) } } case *types.Tuple: y := y.(*types.Tuple) if x.Len() != y.Len() { return fmt.Errorf("unequal tuple lengths: %d vs %d", x.Len(), y.Len()) } for i := 0; i < x.Len(); i++ { if err := equalType(x.At(i).Type(), y.At(i).Type()); err != nil { return fmt.Errorf("tuple element %d: %s", i, err) } } } return nil }