From 3a4b95073a9fd7bca6e9fd80016275ef04bc1987 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Fri, 12 Nov 2021 13:23:45 -0800 Subject: [PATCH] cmd/compile/internal/types2: make sure we are safe for nil in underIs Reviewed all uses of underIs (global function and method) and made sure we are ok with a nil incoming argument (indicating a type set with no specific types). Added a couple of checks where we didn't have them (and somehow didn't run into a problem yet). Change-Id: Ifde45a3a80ddf2b1a19c83f79258ad8207dfb09f Reviewed-on: https://go-review.googlesource.com/c/go/+/363658 Trust: Robert Griesemer Reviewed-by: Robert Findley --- src/cmd/compile/internal/types2/expr.go | 3 +++ src/cmd/compile/internal/types2/predicates.go | 4 +++- src/cmd/compile/internal/types2/type.go | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/cmd/compile/internal/types2/expr.go b/src/cmd/compile/internal/types2/expr.go index f86606375c..77e497b9cc 100644 --- a/src/cmd/compile/internal/types2/expr.go +++ b/src/cmd/compile/internal/types2/expr.go @@ -740,6 +740,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/cmd/compile/internal/types2/predicates.go b/src/cmd/compile/internal/types2/predicates.go index 5cb1c33814..ab490372fc 100644 --- a/src/cmd/compile/internal/types2/predicates.go +++ b/src/cmd/compile/internal/types2/predicates.go @@ -147,7 +147,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/cmd/compile/internal/types2/type.go b/src/cmd/compile/internal/types2/type.go index 24d44442e9..ba260d2b7d 100644 --- a/src/cmd/compile/internal/types2/type.go +++ b/src/cmd/compile/internal/types2/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