go/types: fix instantiated type name computation (for now)

This needs more work eventually.

Change-Id: I33fce18c454113620df9bb1db71a60b38b0a1306
This commit is contained in:
Robert Griesemer 2019-07-17 17:25:21 -07:00
parent de712f2df8
commit 38fa8c1e9b
3 changed files with 27 additions and 19 deletions

View File

@ -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()
}

View File

@ -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)){}
)

View File

@ -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 := "<Named w/o object>"
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