[dev.regabi] cmd/compile: improve findTypeLoop

When checking if a defined type is part of a type loop, we can
short-circuit if it was defined in another package. We can assume any
package we import already successfully compiled, so any types it
contains cannot be part of a type loop.

This also allows us to simplify the logic for recursing into the type
used in the type declaration, because any defined type from this
package will have a properly setup node.

Change-Id: Ic024814d95533afd9e59f2103c8ddb22bd87e900
Reviewed-on: https://go-review.googlesource.com/c/go/+/274294
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Matthew Dempsky 2020-11-30 20:34:25 -08:00
parent 45f3b646d4
commit dadfc80bc1

View File

@ -190,6 +190,13 @@ func findTypeLoop(t *types.Type, path *[]*types.Type) bool {
// recurse on the type expression used in the type
// declaration.
// Type imported from package, so it can't be part of
// a type loop (otherwise that package should have
// failed to compile).
if t.Sym.Pkg != ir.LocalPkg {
return false
}
for i, x := range *path {
if x == t {
*path = (*path)[i:]
@ -198,10 +205,8 @@ func findTypeLoop(t *types.Type, path *[]*types.Type) bool {
}
*path = append(*path, t)
if n := ir.AsNode(t.Nod); n != nil {
if name := n.Name(); name != nil && name.Ntype != nil && findTypeLoop(name.Ntype.Type(), path) {
return true
}
if findTypeLoop(ir.AsNode(t.Nod).Name().Ntype.Type(), path) {
return true
}
*path = (*path)[:len(*path)-1]
} else {