go/types: make sure we are safe for nil in underIs

This CL is a clean port CL 363658 from types2 to go/types.

Change-Id: Ie2032f85a9cfca62161c2e629c78f1ecd8c6e4c0
Reviewed-on: https://go-review.googlesource.com/c/go/+/364537
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2021-11-16 17:51:58 -08:00
parent 3c00a2839a
commit 03dd049d6e
3 changed files with 9 additions and 1 deletions

View File

@ -679,6 +679,9 @@ func (check *Checker) implicitTypeAndValue(x *operand, target Type) (Type, const
case *TypeParam:
// TODO(gri) review this code - doesn't look quite right
ok := u.underIs(func(t Type) bool {
if t == nil {
return false
}
target, _, _ := check.implicitTypeAndValue(x, t)
return target != nil
})

View File

@ -149,7 +149,9 @@ func hasNil(t Type) bool {
case *Slice, *Pointer, *Signature, *Interface, *Map, *Chan:
return true
case *TypeParam:
return u.underIs(hasNil)
return u.underIs(func(u Type) bool {
return u != nil && hasNil(u)
})
}
return false
}

View File

@ -65,6 +65,9 @@ func match(x, y Type) Type {
func structuralType(typ Type) Type {
var su Type
if underIs(typ, func(u Type) bool {
if u == nil {
return false
}
if su != nil {
u = match(su, u)
if u == nil {