]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: optimize common case in structuralType
authorRobert Findley <rfindley@google.com>
Thu, 18 Nov 2021 00:22:22 +0000 (19:22 -0500)
committerRobert Findley <rfindley@google.com>
Thu, 18 Nov 2021 02:16:39 +0000 (02:16 +0000)
This is a port of CL 363668 from types2 to go/types.

Change-Id: Ic55acb2e27f57c33467cef2f687cd695e092ba6d
Reviewed-on: https://go-review.googlesource.com/c/go/+/364898
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>
src/go/types/type.go

index 6611c25f2533a7a10d89e1a4209051955603781a..e283c65289e5807ad64f6608bef84102869a1381 100644 (file)
@@ -56,15 +56,20 @@ func match(x, y Type) Type {
        return nil
 }
 
-// If typ is a type parameter, structuralType returns the single underlying
-// type of all types in the corresponding type constraint if it exists, or
-// nil otherwise. If the type set contains only unrestricted and restricted
-// channel types (with identical element types), the single underlying type
-// is the restricted channel type if the restrictions are always the same.
-// If typ is not a type parameter, structuralType returns the underlying type.
-func structuralType(typ Type) Type {
+// If t is not a type parameter, structuralType returns the underlying type.
+// If t is a type parameter, structuralType returns the single underlying
+// type of all types in its type set if it exists, or nil otherwise. If the
+// type set contains only unrestricted and restricted channel types (with
+// identical element types), the single underlying type is the restricted
+// channel type if the restrictions are always the same, or nil otherwise.
+func structuralType(t Type) Type {
+       tpar, _ := t.(*TypeParam)
+       if tpar == nil {
+               return under(t)
+       }
+
        var su Type
-       if underIs(typ, func(u Type) bool {
+       if tpar.underIs(func(u Type) bool {
                if u == nil {
                        return false
                }