From 38fa8c1e9bb24e53292ed82b7094309240a96ab5 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 17 Jul 2019 17:25:21 -0700 Subject: [PATCH] go/types: fix instantiated type name computation (for now) This needs more work eventually. Change-Id: I33fce18c454113620df9bb1db71a60b38b0a1306 --- src/go/types/subst.go | 9 ++++----- src/go/types/testdata/tmp.go2 | 9 ++++++--- src/go/types/typestring.go | 28 +++++++++++++++++----------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/go/types/subst.go b/src/go/types/subst.go index c0224fe691..155619f6fa 100644 --- a/src/go/types/subst.go +++ b/src/go/types/subst.go @@ -127,7 +127,8 @@ func (s *subster) typ(typ Type) (res Type) { panic("cannot handle instantiation of types with methods yet") } // TODO(gri) review name creation and factor out - name := t.obj.name + typeArgsString(s.targs) + name := TypeString(t, nil) + "<" + typeListString(s.targs) + ">" + //s.check.dump("NAME = %s", name) tname, found := s.check.typMap[name] if !found { // TODO(gri) what is the correct position to use here? @@ -208,15 +209,13 @@ func (s *subster) varList(in []*Var) (out []*Var, copied bool) { return } -func typeArgsString(targs []Type) string { +func typeListString(targs []Type) string { var buf bytes.Buffer - buf.WriteByte('<') for i, arg := range targs { if i > 0 { buf.WriteString(", ") } - buf.WriteString(TypeString(arg, nil)) + WriteType(&buf, arg, nil) } - buf.WriteByte('>') return buf.String() } diff --git a/src/go/types/testdata/tmp.go2 b/src/go/types/testdata/tmp.go2 index a75d304ffb..308c438b4e 100644 --- a/src/go/types/testdata/tmp.go2 +++ b/src/go/types/testdata/tmp.go2 @@ -11,7 +11,10 @@ func f(type P) (x P) List(P) { } var ( - _ []int = f(0) - _ []float32 = f(float32)(10) - //_ []List(int) = f(List(int){}) + //_ []int = f(0) + //_ []float32 = f(float32)(10) + // TODO(gri) fix next line + // [](List(int)) = f(List(int){}) + // _ List(List(int)) = [](List(int)){} + // _ = [](List(int)){} ) diff --git a/src/go/types/typestring.go b/src/go/types/typestring.go index 9e291922ca..46e254d293 100644 --- a/src/go/types/typestring.go +++ b/src/go/types/typestring.go @@ -9,6 +9,7 @@ package types import ( "bytes" "fmt" + "strings" ) // A Qualifier controls how named package-level objects are printed in @@ -236,16 +237,16 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) { case *Named: writeTypeName(buf, t.obj, qf) - if t.obj != nil && len(t.obj.tparams) > 0 { - buf.WriteByte('(') - for i, tpar := range t.obj.tparams { - if i > 0 { - buf.WriteString(", ") - } - writeTypeName(buf, tpar, qf) - } - buf.WriteByte(')') - } + // if t.obj != nil && len(t.obj.tparams) > 0 { + // buf.WriteByte('(') + // for i, tpar := range t.obj.tparams { + // if i > 0 { + // buf.WriteString(", ") + // } + // writeTypeName(buf, tpar, qf) + // } + // buf.WriteByte(')') + // } case *Parameterized: writeTypeName(buf, t.tname, qf) @@ -289,7 +290,12 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) { func writeTypeName(buf *bytes.Buffer, obj *TypeName, qf Qualifier) { s := "" if obj != nil { - if obj.pkg != nil { + // Don't prefix an instantiated type (which is marked by a closing '>') + // with yet another package name - the instantiated type is already fully + // qualified. See code in subst.go. + // TODO(gri) Need to factor out '>' or named type string generation; + // this dependency is too subtle. + if obj.pkg != nil && !strings.HasSuffix(obj.name, ">") { writePackage(buf, obj.pkg, qf) } // TODO(gri): function-local named types should be displayed