go/types: improved type strings for instantiated generic types

Better output for now, but the printing code is a hack and needs
a rethink and cleanup.

Change-Id: Ieb1f13a21645b48f9494ceda50f58017b31bbd82
This commit is contained in:
Robert Griesemer 2020-01-25 23:13:52 -08:00
parent b1322d38b6
commit 5a2e660efa
3 changed files with 13 additions and 3 deletions

View File

@ -772,6 +772,11 @@ func (check *Checker) declareTypeParams(tparams []*TypeName, names []*ast.Ident)
check.declare(check.scope, name, tpar, check.scope.pos) // TODO(gri) check scope position
tparams = append(tparams, tpar)
}
if check.conf.Trace && len(names) > 0 {
check.trace(names[0].Pos(), "type params = %v", tparams[len(tparams)-len(names):])
}
return tparams
}

View File

@ -250,7 +250,7 @@ func (subst *subster) typ(typ Type) Type {
// already instantiated
dump(">>> %s already instantiated", t)
assert(len(t.targs) == len(t.targs))
assert(len(t.targs) == len(t.tparams))
// For each (existing) type argument targ, determine if it needs
// to be substituted; i.e., if it is or contains a type parameter
// that has a type argument for it.
@ -282,7 +282,9 @@ func (subst *subster) typ(typ Type) Type {
// TODO(gri) revisit name creation (function local types, etc.) and factor out
// (also, stripArgNames call is an awful hack)
name := stripArgNames(TypeString(t, nil)) + "<" + typeListString(new_targs) + ">"
var buf bytes.Buffer
writeTypeName(&buf, t.obj, nil)
name := stripArgNames(buf.String()) + "<" + typeListString(new_targs) + ">"
dump(">>> new type name: %s", name)
if tname, found := subst.check.typMap[name]; found {
dump(">>> instantiated %s found", tname)

View File

@ -259,7 +259,10 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) {
case *Named:
writeTypeName(buf, t.obj, qf)
if t.tparams != nil {
// Don't write type parameter list again if we have an instantiated type,
// recognized by a closing '>'.
// TODO(gri) clean up - this is too subtle a hack and too dependent on subst.
if buf.Bytes()[buf.Len()-1] != '>' && t.tparams != nil {
writeTParamList(buf, t.tparams, qf, visited)
}