From 03dd049d6efcbca6b829d7ed504ceea6318f2036 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 16 Nov 2021 17:51:58 -0800 Subject: [PATCH] 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 Reviewed-by: Robert Findley --- src/go/types/expr.go | 3 +++ src/go/types/predicates.go | 4 +++- src/go/types/type.go | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/go/types/expr.go b/src/go/types/expr.go index 660c92de3b..ddb0149bf4 100644 --- a/src/go/types/expr.go +++ b/src/go/types/expr.go @@ -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 }) diff --git a/src/go/types/predicates.go b/src/go/types/predicates.go index d0697b1ad7..78ad6c4f23 100644 --- a/src/go/types/predicates.go +++ b/src/go/types/predicates.go @@ -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 } diff --git a/src/go/types/type.go b/src/go/types/type.go index 555eb9e8b9..756bdcf0a5 100644 --- a/src/go/types/type.go +++ b/src/go/types/type.go @@ -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 { -- 2.50.0