Commit f11109fb authored by Alan Donovan's avatar Alan Donovan

go/types: change {Type,Object,Selection}String to accept a Qualifier function

The optional Qualifier function determines what prefix to attach to
package-level names, enabling clients to qualify packages in different
ways, for example, using only the package name instead of its complete
path, or using the locally appropriate name for package given a set of
(possibly renaming) imports.

Prior to this change, clients wanting this behavior had to copy
hundreds of lines of complex printing logic.

Fun fact: (*types.Package).Path and (*types.Package).Name are valid
Qualifier functions.

We provide the RelativeTo helper function to create Qualifiers so that
the old behavior remains a one-liner.

Fixes golang/go#11133

This CL is a copy of https://go-review.googlesource.com/#/c/11692/
to the golang.org/x/tools repository.

Change-Id: I26d0f3644d077a26bfe350989f9c545f018eefbf
Reviewed-on: https://go-review.googlesource.com/11790Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
parent 6f80e5ed
...@@ -34,7 +34,7 @@ func runImporterTest(t *testing.T, imp Importer, initmap map[*types.Package]Init ...@@ -34,7 +34,7 @@ func runImporterTest(t *testing.T, imp Importer, initmap map[*types.Package]Init
return return
} }
got := types.ObjectString(pkg, obj) got := types.ObjectString(obj, types.RelativeTo(pkg))
if got != test.want { if got != test.want {
t.Errorf("%s: got %q; want %q", test.name, got, test.want) t.Errorf("%s: got %q; want %q", test.name, got, test.want)
} }
......
...@@ -165,7 +165,7 @@ func TestImportedTypes(t *testing.T) { ...@@ -165,7 +165,7 @@ func TestImportedTypes(t *testing.T) {
continue continue
} }
got := types.ObjectString(pkg, obj) got := types.ObjectString(obj, types.RelativeTo(pkg))
if got != test.want { if got != test.want {
t.Errorf("%s: got %q; want %q", test.name, got, test.want) t.Errorf("%s: got %q; want %q", test.name, got, test.want)
} }
......
...@@ -23,6 +23,13 @@ func unreachable() { ...@@ -23,6 +23,13 @@ func unreachable() {
panic("unreachable") panic("unreachable")
} }
func (check *Checker) qualifier(pkg *Package) string {
if pkg != check.pkg {
return pkg.path
}
return ""
}
func (check *Checker) sprintf(format string, args ...interface{}) string { func (check *Checker) sprintf(format string, args ...interface{}) string {
for i, arg := range args { for i, arg := range args {
switch a := arg.(type) { switch a := arg.(type) {
...@@ -31,15 +38,15 @@ func (check *Checker) sprintf(format string, args ...interface{}) string { ...@@ -31,15 +38,15 @@ func (check *Checker) sprintf(format string, args ...interface{}) string {
case operand: case operand:
panic("internal error: should always pass *operand") panic("internal error: should always pass *operand")
case *operand: case *operand:
arg = operandString(check.pkg, a) arg = operandString(a, check.qualifier)
case token.Pos: case token.Pos:
arg = check.fset.Position(a).String() arg = check.fset.Position(a).String()
case ast.Expr: case ast.Expr:
arg = ExprString(a) arg = ExprString(a)
case Object: case Object:
arg = ObjectString(check.pkg, a) arg = ObjectString(a, check.qualifier)
case Type: case Type:
arg = TypeString(check.pkg, a) arg = TypeString(a, check.qualifier)
} }
args[i] = arg args[i] = arg
} }
......
...@@ -199,7 +199,7 @@ func fib(x int) int { ...@@ -199,7 +199,7 @@ func fib(x int) int {
for obj, uses := range usesByObj { for obj, uses := range usesByObj {
sort.Strings(uses) sort.Strings(uses)
item := fmt.Sprintf("%s:\n defined at %s\n used at %s", item := fmt.Sprintf("%s:\n defined at %s\n used at %s",
types.ObjectString(pkg, obj), types.ObjectString(obj, types.RelativeTo(pkg)),
fset.Position(obj.Pos()), fset.Position(obj.Pos()),
strings.Join(uses, ", ")) strings.Join(uses, ", "))
items = append(items, item) items = append(items, item)
......
...@@ -207,7 +207,7 @@ func NewFunc(pos token.Pos, pkg *Package, name string, sig *Signature) *Func { ...@@ -207,7 +207,7 @@ func NewFunc(pos token.Pos, pkg *Package, name string, sig *Signature) *Func {
// function or method obj. // function or method obj.
func (obj *Func) FullName() string { func (obj *Func) FullName() string {
var buf bytes.Buffer var buf bytes.Buffer
writeFuncName(&buf, nil, obj) writeFuncName(&buf, obj, nil)
return buf.String() return buf.String()
} }
...@@ -241,7 +241,7 @@ type Nil struct { ...@@ -241,7 +241,7 @@ type Nil struct {
object object
} }
func writeObject(buf *bytes.Buffer, this *Package, obj Object) { func writeObject(buf *bytes.Buffer, obj Object, qf Qualifier) {
typ := obj.Type() typ := obj.Type()
switch obj := obj.(type) { switch obj := obj.(type) {
case *PkgName: case *PkgName:
...@@ -267,9 +267,9 @@ func writeObject(buf *bytes.Buffer, this *Package, obj Object) { ...@@ -267,9 +267,9 @@ func writeObject(buf *bytes.Buffer, this *Package, obj Object) {
case *Func: case *Func:
buf.WriteString("func ") buf.WriteString("func ")
writeFuncName(buf, this, obj) writeFuncName(buf, obj, qf)
if typ != nil { if typ != nil {
WriteSignature(buf, this, typ.(*Signature)) WriteSignature(buf, typ.(*Signature), qf)
} }
return return
...@@ -291,39 +291,52 @@ func writeObject(buf *bytes.Buffer, this *Package, obj Object) { ...@@ -291,39 +291,52 @@ func writeObject(buf *bytes.Buffer, this *Package, obj Object) {
buf.WriteByte(' ') buf.WriteByte(' ')
// For package-level objects, package-qualify the name, // For package-level objects, qualify the name.
// except for intra-package references (this != nil). if obj.Pkg() != nil && obj.Pkg().scope.Lookup(obj.Name()) == obj {
if pkg := obj.Pkg(); pkg != nil && this != pkg && pkg.scope.Lookup(obj.Name()) == obj { writePackage(buf, obj.Pkg(), qf)
buf.WriteString(pkg.path)
buf.WriteByte('.')
} }
buf.WriteString(obj.Name()) buf.WriteString(obj.Name())
if typ != nil { if typ != nil {
buf.WriteByte(' ') buf.WriteByte(' ')
WriteType(buf, this, typ) WriteType(buf, typ, qf)
}
}
func writePackage(buf *bytes.Buffer, pkg *Package, qf Qualifier) {
if pkg == nil {
return
}
var s string
if qf != nil {
s = qf(pkg)
} else {
s = pkg.Path()
}
if s != "" {
buf.WriteString(s)
buf.WriteByte('.')
} }
} }
// ObjectString returns the string form of obj. // ObjectString returns the string form of obj.
// Object and type names are printed package-qualified // The Qualifier controls the printing of
// only if they do not belong to this package. // package-level objects, and may be nil.
// func ObjectString(obj Object, qf Qualifier) string {
func ObjectString(this *Package, obj Object) string {
var buf bytes.Buffer var buf bytes.Buffer
writeObject(&buf, this, obj) writeObject(&buf, obj, qf)
return buf.String() return buf.String()
} }
func (obj *PkgName) String() string { return ObjectString(nil, obj) } func (obj *PkgName) String() string { return ObjectString(obj, nil) }
func (obj *Const) String() string { return ObjectString(nil, obj) } func (obj *Const) String() string { return ObjectString(obj, nil) }
func (obj *TypeName) String() string { return ObjectString(nil, obj) } func (obj *TypeName) String() string { return ObjectString(obj, nil) }
func (obj *Var) String() string { return ObjectString(nil, obj) } func (obj *Var) String() string { return ObjectString(obj, nil) }
func (obj *Func) String() string { return ObjectString(nil, obj) } func (obj *Func) String() string { return ObjectString(obj, nil) }
func (obj *Label) String() string { return ObjectString(nil, obj) } func (obj *Label) String() string { return ObjectString(obj, nil) }
func (obj *Builtin) String() string { return ObjectString(nil, obj) } func (obj *Builtin) String() string { return ObjectString(obj, nil) }
func (obj *Nil) String() string { return ObjectString(nil, obj) } func (obj *Nil) String() string { return ObjectString(obj, nil) }
func writeFuncName(buf *bytes.Buffer, this *Package, f *Func) { func writeFuncName(buf *bytes.Buffer, f *Func, qf Qualifier) {
if f.typ != nil { if f.typ != nil {
sig := f.typ.(*Signature) sig := f.typ.(*Signature)
if recv := sig.Recv(); recv != nil { if recv := sig.Recv(); recv != nil {
...@@ -335,13 +348,12 @@ func writeFuncName(buf *bytes.Buffer, this *Package, f *Func) { ...@@ -335,13 +348,12 @@ func writeFuncName(buf *bytes.Buffer, this *Package, f *Func) {
// Don't print it in full. // Don't print it in full.
buf.WriteString("interface") buf.WriteString("interface")
} else { } else {
WriteType(buf, this, recv.Type()) WriteType(buf, recv.Type(), qf)
} }
buf.WriteByte(')') buf.WriteByte(')')
buf.WriteByte('.') buf.WriteByte('.')
} else if f.pkg != nil && f.pkg != this { } else if f.pkg != nil {
buf.WriteString(f.pkg.path) writePackage(buf, f.pkg, qf)
buf.WriteByte('.')
} }
} }
buf.WriteString(f.name) buf.WriteString(f.name)
......
...@@ -93,7 +93,7 @@ func (x *operand) pos() token.Pos { ...@@ -93,7 +93,7 @@ func (x *operand) pos() token.Pos {
// commaok <expr> (<untyped kind> <mode> ) // commaok <expr> (<untyped kind> <mode> )
// commaok <expr> ( <mode> of type <typ>) // commaok <expr> ( <mode> of type <typ>)
// //
func operandString(this *Package, x *operand) string { func operandString(x *operand, qf Qualifier) string {
var buf bytes.Buffer var buf bytes.Buffer
var expr string var expr string
...@@ -104,7 +104,7 @@ func operandString(this *Package, x *operand) string { ...@@ -104,7 +104,7 @@ func operandString(this *Package, x *operand) string {
case builtin: case builtin:
expr = predeclaredFuncs[x.id].name expr = predeclaredFuncs[x.id].name
case typexpr: case typexpr:
expr = TypeString(this, x.typ) expr = TypeString(x.typ, qf)
case constant: case constant:
expr = x.val.String() expr = x.val.String()
} }
...@@ -146,7 +146,7 @@ func operandString(this *Package, x *operand) string { ...@@ -146,7 +146,7 @@ func operandString(this *Package, x *operand) string {
if hasType { if hasType {
if x.typ != Typ[Invalid] { if x.typ != Typ[Invalid] {
buf.WriteString(" of type ") buf.WriteString(" of type ")
WriteType(&buf, this, x.typ) WriteType(&buf, x.typ, qf)
} else { } else {
buf.WriteString(" with invalid type") buf.WriteString(" with invalid type")
} }
...@@ -161,7 +161,7 @@ func operandString(this *Package, x *operand) string { ...@@ -161,7 +161,7 @@ func operandString(this *Package, x *operand) string {
} }
func (x *operand) String() string { func (x *operand) String() string {
return operandString(nil, x) return operandString(x, nil)
} }
// setConst sets x to the untyped constant for literal lit. // setConst sets x to the untyped constant for literal lit.
......
...@@ -105,18 +105,18 @@ func (s *Selection) Index() []int { return s.index } ...@@ -105,18 +105,18 @@ func (s *Selection) Index() []int { return s.index }
// x to f in x.f. // x to f in x.f.
func (s *Selection) Indirect() bool { return s.indirect } func (s *Selection) Indirect() bool { return s.indirect }
func (s *Selection) String() string { return SelectionString(nil, s) } func (s *Selection) String() string { return SelectionString(s, nil) }
// SelectionString returns the string form of s. // SelectionString returns the string form of s.
// Type names are printed package-qualified // The Qualifier controls the printing of
// only if they do not belong to this package. // package-level objects, and may be nil.
// //
// Examples: // Examples:
// "field (T) f int" // "field (T) f int"
// "method (T) f(X) Y" // "method (T) f(X) Y"
// "method expr (T) f(X) Y" // "method expr (T) f(X) Y"
// //
func SelectionString(this *Package, s *Selection) string { func SelectionString(s *Selection, qf Qualifier) string {
var k string var k string
switch s.kind { switch s.kind {
case FieldVal: case FieldVal:
...@@ -131,13 +131,13 @@ func SelectionString(this *Package, s *Selection) string { ...@@ -131,13 +131,13 @@ func SelectionString(this *Package, s *Selection) string {
var buf bytes.Buffer var buf bytes.Buffer
buf.WriteString(k) buf.WriteString(k)
buf.WriteByte('(') buf.WriteByte('(')
WriteType(&buf, this, s.Recv()) WriteType(&buf, s.Recv(), qf)
fmt.Fprintf(&buf, ") %s", s.obj.Name()) fmt.Fprintf(&buf, ") %s", s.obj.Name())
if T := s.Type(); s.kind == FieldVal { if T := s.Type(); s.kind == FieldVal {
buf.WriteByte(' ') buf.WriteByte(' ')
WriteType(&buf, this, T) WriteType(&buf, T, qf)
} else { } else {
WriteSignature(&buf, this, T.(*Signature)) WriteSignature(&buf, T.(*Signature), qf)
} }
return buf.String() return buf.String()
} }
...@@ -441,14 +441,14 @@ func (t *Map) Underlying() Type { return t } ...@@ -441,14 +441,14 @@ func (t *Map) Underlying() Type { return t }
func (t *Chan) Underlying() Type { return t } func (t *Chan) Underlying() Type { return t }
func (t *Named) Underlying() Type { return t.underlying } func (t *Named) Underlying() Type { return t.underlying }
func (t *Basic) String() string { return TypeString(nil, t) } func (t *Basic) String() string { return TypeString(t, nil) }
func (t *Array) String() string { return TypeString(nil, t) } func (t *Array) String() string { return TypeString(t, nil) }
func (t *Slice) String() string { return TypeString(nil, t) } func (t *Slice) String() string { return TypeString(t, nil) }
func (t *Struct) String() string { return TypeString(nil, t) } func (t *Struct) String() string { return TypeString(t, nil) }
func (t *Pointer) String() string { return TypeString(nil, t) } func (t *Pointer) String() string { return TypeString(t, nil) }
func (t *Tuple) String() string { return TypeString(nil, t) } func (t *Tuple) String() string { return TypeString(t, nil) }
func (t *Signature) String() string { return TypeString(nil, t) } func (t *Signature) String() string { return TypeString(t, nil) }
func (t *Interface) String() string { return TypeString(nil, t) } func (t *Interface) String() string { return TypeString(t, nil) }
func (t *Map) String() string { return TypeString(nil, t) } func (t *Map) String() string { return TypeString(t, nil) }
func (t *Chan) String() string { return TypeString(nil, t) } func (t *Chan) String() string { return TypeString(t, nil) }
func (t *Named) String() string { return TypeString(nil, t) } func (t *Named) String() string { return TypeString(t, nil) }
...@@ -11,6 +11,33 @@ import ( ...@@ -11,6 +11,33 @@ import (
"fmt" "fmt"
) )
// A Qualifier controls how named package-level objects are printed in
// calls to TypeString, ObjectString, and SelectionString.
//
// These three formatting routines call the Qualifier for each
// package-level object O, and if the Qualifier returns a non-empty
// string p, the object is printed in the form p.O.
// If it returns an empty string, only the object name O is printed.
//
// Using a nil Qualifier is equivalent to using (*Package).Path: the
// object is qualified by the import path, e.g., "encoding/json.Marshal".
//
type Qualifier func(*Package) string
// RelativeTo(pkg) returns a Qualifier that fully qualifies members of
// all packages other than pkg.
func RelativeTo(pkg *Package) Qualifier {
if pkg == nil {
return nil
}
return func(other *Package) string {
if pkg == other {
return "" // same package; unqualified
}
return other.Path()
}
}
// If gcCompatibilityMode is set, printing of types is modified // If gcCompatibilityMode is set, printing of types is modified
// to match the representation of some types in the gc compiler: // to match the representation of some types in the gc compiler:
// //
...@@ -32,22 +59,22 @@ import ( ...@@ -32,22 +59,22 @@ import (
var gcCompatibilityMode bool var gcCompatibilityMode bool
// TypeString returns the string representation of typ. // TypeString returns the string representation of typ.
// Named types are printed package-qualified if they // The Qualifier controls the printing of
// do not belong to this package. // package-level objects, and may be nil.
func TypeString(this *Package, typ Type) string { func TypeString(typ Type, qf Qualifier) string {
var buf bytes.Buffer var buf bytes.Buffer
WriteType(&buf, this, typ) WriteType(&buf, typ, qf)
return buf.String() return buf.String()
} }
// WriteType writes the string representation of typ to buf. // WriteType writes the string representation of typ to buf.
// Named types are printed package-qualified if they // The Qualifier controls the printing of
// do not belong to this package. // package-level objects, and may be nil.
func WriteType(buf *bytes.Buffer, this *Package, typ Type) { func WriteType(buf *bytes.Buffer, typ Type, qf Qualifier) {
writeType(buf, this, typ, make([]Type, 8)) writeType(buf, typ, qf, make([]Type, 8))
} }
func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) { func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) {
// Theoretically, this is a quadratic lookup algorithm, but in // Theoretically, this is a quadratic lookup algorithm, but in
// practice deeply nested composite types with unnamed component // practice deeply nested composite types with unnamed component
// types are uncommon. This code is likely more efficient than // types are uncommon. This code is likely more efficient than
...@@ -81,11 +108,11 @@ func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) { ...@@ -81,11 +108,11 @@ func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) {
case *Array: case *Array:
fmt.Fprintf(buf, "[%d]", t.len) fmt.Fprintf(buf, "[%d]", t.len)
writeType(buf, this, t.elem, visited) writeType(buf, t.elem, qf, visited)
case *Slice: case *Slice:
buf.WriteString("[]") buf.WriteString("[]")
writeType(buf, this, t.elem, visited) writeType(buf, t.elem, qf, visited)
case *Struct: case *Struct:
buf.WriteString("struct{") buf.WriteString("struct{")
...@@ -97,7 +124,7 @@ func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) { ...@@ -97,7 +124,7 @@ func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) {
buf.WriteString(f.name) buf.WriteString(f.name)
buf.WriteByte(' ') buf.WriteByte(' ')
} }
writeType(buf, this, f.typ, visited) writeType(buf, f.typ, qf, visited)
if tag := t.Tag(i); tag != "" { if tag := t.Tag(i); tag != "" {
fmt.Fprintf(buf, " %q", tag) fmt.Fprintf(buf, " %q", tag)
} }
...@@ -106,14 +133,14 @@ func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) { ...@@ -106,14 +133,14 @@ func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) {
case *Pointer: case *Pointer:
buf.WriteByte('*') buf.WriteByte('*')
writeType(buf, this, t.base, visited) writeType(buf, t.base, qf, visited)
case *Tuple: case *Tuple:
writeTuple(buf, this, t, false, visited) writeTuple(buf, t, false, qf, visited)
case *Signature: case *Signature:
buf.WriteString("func") buf.WriteString("func")
writeSignature(buf, this, t, visited) writeSignature(buf, t, qf, visited)
case *Interface: case *Interface:
// We write the source-level methods and embedded types rather // We write the source-level methods and embedded types rather
...@@ -136,7 +163,7 @@ func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) { ...@@ -136,7 +163,7 @@ func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) {
buf.WriteString("; ") buf.WriteString("; ")
} }
buf.WriteString(m.name) buf.WriteString(m.name)
writeSignature(buf, this, m.typ.(*Signature), visited) writeSignature(buf, m.typ.(*Signature), qf, visited)
} }
} else { } else {
// print explicit interface methods and embedded types // print explicit interface methods and embedded types
...@@ -145,22 +172,22 @@ func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) { ...@@ -145,22 +172,22 @@ func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) {
buf.WriteString("; ") buf.WriteString("; ")
} }
buf.WriteString(m.name) buf.WriteString(m.name)
writeSignature(buf, this, m.typ.(*Signature), visited) writeSignature(buf, m.typ.(*Signature), qf, visited)
} }
for i, typ := range t.embeddeds { for i, typ := range t.embeddeds {
if i > 0 || len(t.methods) > 0 { if i > 0 || len(t.methods) > 0 {
buf.WriteString("; ") buf.WriteString("; ")
} }
writeType(buf, this, typ, visited) writeType(buf, typ, qf, visited)
} }
} }
buf.WriteByte('}') buf.WriteByte('}')
case *Map: case *Map:
buf.WriteString("map[") buf.WriteString("map[")
writeType(buf, this, t.key, visited) writeType(buf, t.key, qf, visited)
buf.WriteByte(']') buf.WriteByte(']')
writeType(buf, this, t.elem, visited) writeType(buf, t.elem, qf, visited)
case *Chan: case *Chan:
var s string var s string
...@@ -183,7 +210,7 @@ func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) { ...@@ -183,7 +210,7 @@ func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) {
if parens { if parens {
buf.WriteByte('(') buf.WriteByte('(')
} }
writeType(buf, this, t.elem, visited) writeType(buf, t.elem, qf, visited)
if parens { if parens {
buf.WriteByte(')') buf.WriteByte(')')
} }
...@@ -191,9 +218,8 @@ func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) { ...@@ -191,9 +218,8 @@ func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) {
case *Named: case *Named:
s := "<Named w/o object>" s := "<Named w/o object>"
if obj := t.obj; obj != nil { if obj := t.obj; obj != nil {
if pkg := obj.pkg; pkg != nil && pkg != this { if obj.pkg != nil {
buf.WriteString(pkg.path) writePackage(buf, obj.pkg, qf)
buf.WriteByte('.')
} }
// TODO(gri): function-local named types should be displayed // TODO(gri): function-local named types should be displayed
// differently from named types at package level to avoid // differently from named types at package level to avoid
...@@ -208,7 +234,7 @@ func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) { ...@@ -208,7 +234,7 @@ func writeType(buf *bytes.Buffer, this *Package, typ Type, visited []Type) {
} }
} }
func writeTuple(buf *bytes.Buffer, this *Package, tup *Tuple, variadic bool, visited []Type) { func writeTuple(buf *bytes.Buffer, tup *Tuple, variadic bool, qf Qualifier, visited []Type) {
buf.WriteByte('(') buf.WriteByte('(')
if tup != nil { if tup != nil {
for i, v := range tup.vars { for i, v := range tup.vars {
...@@ -230,12 +256,12 @@ func writeTuple(buf *bytes.Buffer, this *Package, tup *Tuple, variadic bool, vis ...@@ -230,12 +256,12 @@ func writeTuple(buf *bytes.Buffer, this *Package, tup *Tuple, variadic bool, vis
if t, ok := typ.Underlying().(*Basic); !ok || t.kind != String { if t, ok := typ.Underlying().(*Basic); !ok || t.kind != String {
panic("internal error: string type expected") panic("internal error: string type expected")
} }
writeType(buf, this, typ, visited) writeType(buf, typ, qf, visited)
buf.WriteString("...") buf.WriteString("...")
continue continue
} }
} }
writeType(buf, this, typ, visited) writeType(buf, typ, qf, visited)
} }
} }
buf.WriteByte(')') buf.WriteByte(')')
...@@ -243,14 +269,14 @@ func writeTuple(buf *bytes.Buffer, this *Package, tup *Tuple, variadic bool, vis ...@@ -243,14 +269,14 @@ func writeTuple(buf *bytes.Buffer, this *Package, tup *Tuple, variadic bool, vis
// WriteSignature writes the representation of the signature sig to buf, // WriteSignature writes the representation of the signature sig to buf,
// without a leading "func" keyword. // without a leading "func" keyword.
// Named types are printed package-qualified if they // The Qualifier controls the printing of
// do not belong to this package. // package-level objects, and may be nil.
func WriteSignature(buf *bytes.Buffer, this *Package, sig *Signature) { func WriteSignature(buf *bytes.Buffer, sig *Signature, qf Qualifier) {
writeSignature(buf, this, sig, make([]Type, 8)) writeSignature(buf, sig, qf, make([]Type, 8))
} }
func writeSignature(buf *bytes.Buffer, this *Package, sig *Signature, visited []Type) { func writeSignature(buf *bytes.Buffer, sig *Signature, qf Qualifier, visited []Type) {
writeTuple(buf, this, sig.params, sig.variadic, visited) writeTuple(buf, sig.params, sig.variadic, qf, visited)
n := sig.results.Len() n := sig.results.Len()
if n == 0 { if n == 0 {
...@@ -261,10 +287,10 @@ func writeSignature(buf *bytes.Buffer, this *Package, sig *Signature, visited [] ...@@ -261,10 +287,10 @@ func writeSignature(buf *bytes.Buffer, this *Package, sig *Signature, visited []
buf.WriteByte(' ') buf.WriteByte(' ')
if n == 1 && sig.results.vars[0].name == "" { if n == 1 && sig.results.vars[0].name == "" {
// single unnamed result // single unnamed result
writeType(buf, this, sig.results.vars[0].typ, visited) writeType(buf, sig.results.vars[0].typ, qf, visited)
return return
} }
// multiple or named result(s) // multiple or named result(s)
writeTuple(buf, this, sig.results, false, visited) writeTuple(buf, sig.results, false, qf, visited)
} }
...@@ -154,7 +154,13 @@ func TestQualifiedTypeString(t *testing.T) { ...@@ -154,7 +154,13 @@ func TestQualifiedTypeString(t *testing.T) {
{NewPointer(pT), p, "*T"}, {NewPointer(pT), p, "*T"},
{NewPointer(pT), q, "*p.T"}, {NewPointer(pT), q, "*p.T"},
} { } {
if got := TypeString(test.this, test.typ); got != test.want { qualifier := func(pkg *Package) string {
if pkg != test.this {
return pkg.Name()
}
return ""
}
if got := TypeString(test.typ, qualifier); got != test.want {
t.Errorf("TypeString(%s, %s) = %s, want %s", t.Errorf("TypeString(%s, %s) = %s, want %s",
test.this, test.typ, got, test.want) test.this, test.typ, got, test.want)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment