From 91abc02562e2fa39f5e88a7bb879e0237666c370 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 16 Dec 2013 13:53:08 -0800 Subject: [PATCH] go.tools/go/types: print interface methods in sorted order Required for testing code of clients. R=adonovan CC=golang-dev https://golang.org/cl/35940049 --- go/types/objects.go | 8 ++++++-- go/types/typestring.go | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/go/types/objects.go b/go/types/objects.go index 718092490ee..595378c12ca 100644 --- a/go/types/objects.go +++ b/go/types/objects.go @@ -48,7 +48,11 @@ func Id(pkg *Package, name string) string { return name } // unexported names need the package path for differentiation - path := "" + // (if there's no package, make sure we don't start with '.' + // as that may change the order of methods between a setup + // inside a package and outside a package - which breaks some + // tests) + path := "_" // TODO(gri): shouldn't !ast.IsExported(name) => pkg != nil be an precondition? // if pkg == nil { // panic("nil package in lookup of unexported name") @@ -56,7 +60,7 @@ func Id(pkg *Package, name string) string { if pkg != nil { path = pkg.path if path == "" { - path = "?" + path = "_" } } return path + "." + name diff --git a/go/types/typestring.go b/go/types/typestring.go index 9f4905c2ffc..c2b7161e7df 100644 --- a/go/types/typestring.go +++ b/go/types/typestring.go @@ -10,6 +10,7 @@ import ( "bytes" "fmt" "go/ast" + "sort" ) // TypeString returns the string representation of typ. @@ -84,7 +85,19 @@ func WriteType(buf *bytes.Buffer, this *Package, typ Type) { // } // buf.WriteString("interface{") - for i, m := range t.methods { + // Sort methods instead of printing them in source order. + // This is needed at the moment because interfaces are + // created by providing the list of source methods and + // embedded interfaces, but only have an accessor to the list + // of all methods (which is sorted by name). By sorting here + // we guarantee that the list is printed the same independent + // of how the Interface was created. + // TODO(gri) remove this extra step once we have the complete + // set of accessors for Interface. + var methods []*Func + methods = append(methods, t.methods...) // make a copy + sort.Sort(byUniqueMethodName(methods)) + for i, m := range methods { if i > 0 { buf.WriteString("; ") }