[dev.go2go] go/go2go: don't drop type arguments in instantiateType

It's not correct to do it here, as we aren't instantiating this
particular type.

Also had a loop check to avoid endless instantiating a recursive type.

Fixes #39688

Change-Id: Ief4c57bb5cea3013501e33ab5bc82a29707af5be
Reviewed-on: https://go-review.googlesource.com/c/go/+/239137
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Ian Lance Taylor 2020-06-19 16:59:26 -07:00
parent 6cf535a27b
commit cab5f803e8
3 changed files with 18 additions and 8 deletions

View File

@ -29,7 +29,7 @@ func newTypeArgs(typeTypes []types.Type) *typeArgs {
}
}
// typeArgsFromTParams builds mappings from a list of type parameters
// typeArgsFromFields builds mappings from a list of type parameters
// expressed as ast.Field values.
func typeArgsFromFields(t *translator, astTypes []ast.Expr, typeTypes []types.Type, tparams []*ast.Field) *typeArgs {
ta := newTypeArgs(typeTypes)
@ -52,7 +52,7 @@ func typeArgsFromFields(t *translator, astTypes []ast.Expr, typeTypes []types.Ty
return ta
}
// typeArgsFromTParams builds mappings from a list of type parameters
// typeArgsFromExprs builds mappings from a list of type parameters
// expressed as ast.Expr values.
func typeArgsFromExprs(t *translator, astTypes []ast.Expr, typeTypes []types.Type, tparams []ast.Expr) *typeArgs {
ta := newTypeArgs(typeTypes)

View File

@ -128,6 +128,9 @@ type translator struct {
typeInstantiations map[types.Type][]*typeInstantiation
typePackages map[*types.Package]bool
// typeDepth tracks recursive type instantiations.
typeDepth int
// err is set if we have seen an error during this translation.
// This is used by the rewrite methods.
err error

View File

@ -43,12 +43,19 @@ func (t *translator) setType(e ast.Expr, nt types.Type) {
// instantiateType instantiates typ using ta.
func (t *translator) instantiateType(ta *typeArgs, typ types.Type) types.Type {
var inProgress *typeInstantiation
key := typ
if named, ok := typ.(*types.Named); ok {
key = t.typeWithoutArgs(named)
if t.err != nil {
return nil
}
for _, inst := range t.typeInstantiations[key] {
t.typeDepth++
defer func() { t.typeDepth-- }()
if t.typeDepth > 25 {
t.err = fmt.Errorf("looping while instantiating %v %v", typ, ta.types)
return nil
}
var inProgress *typeInstantiation
for _, inst := range t.typeInstantiations[typ] {
if t.sameTypes(ta.types, inst.types) {
if inst.typ == nil {
inProgress = inst
@ -70,7 +77,7 @@ func (t *translator) instantiateType(ta *typeArgs, typ types.Type) types.Type {
types: ta.types,
typ: ityp,
}
t.typeInstantiations[key] = append(t.typeInstantiations[key], typinst)
t.typeInstantiations[typ] = append(t.typeInstantiations[typ], typinst)
}
return ityp