1
0
mirror of https://github.com/golang/go synced 2024-11-24 04:50:07 -07:00

go/types: simplify under() and fix a crash

This is a port of CL 363665 from types2 to go/types.

Change-Id: I20c4e20ab97f1e4de66a29095dc4a9b160810fe5
Reviewed-on: https://go-review.googlesource.com/c/go/+/364897
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Robert Findley 2021-11-17 19:20:52 -05:00
parent b95bff0318
commit 2463b4fcaf
4 changed files with 11 additions and 5 deletions

View File

@ -469,6 +469,13 @@ func (check *Checker) assertableTo(V *Interface, T Type) (method, wrongType *Fun
// Otherwise it returns (typ, false).
func deref(typ Type) (Type, bool) {
if p, _ := typ.(*Pointer); p != nil {
// p.base should never be nil, but be conservative
if p.base == nil {
if debug {
panic("pointer with nil base type (possibly due to an invalid cyclic declaration)")
}
return Typ[Invalid], true
}
return p.base, true
}
return typ, false

View File

@ -45,6 +45,7 @@ type (
// pointers
P0 *P0
PP *struct{ PP.f /* ERROR no field or method f */ }
// functions
F0 func(F0)

View File

@ -21,13 +21,10 @@ type Type interface {
// under must only be called when a type is known
// to be fully set up.
func under(t Type) Type {
switch t := t.(type) {
case *Named:
if t, _ := t.(*Named); t != nil {
return t.under()
case *TypeParam:
return t.iface()
}
return t
return t.Underlying()
}
// If x and y are identical, match returns x.

View File

@ -308,6 +308,7 @@ func (check *Checker) typInternal(e0 ast.Expr, def *Named) (T Type) {
case *ast.StarExpr:
typ := new(Pointer)
typ.base = Typ[Invalid] // avoid nil base in invalid recursive type declaration
def.setUnderlying(typ)
typ.base = check.varType(e.X)
return typ