]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: slightly simplify rangeKeyVal function
authorRobert Griesemer <gri@golang.org>
Mon, 16 Sep 2024 20:50:24 +0000 (13:50 -0700)
committerGopher Robot <gobot@golang.org>
Mon, 16 Sep 2024 21:21:10 +0000 (21:21 +0000)
Compute the signature type of an iterator function argument
only once. This eliminates the need for two separate toSig
calls.

Change-Id: Ifeb33d21e381010d2012d74eac045856f1cca312
Reviewed-on: https://go-review.googlesource.com/c/go/+/613635
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Tim King <taking@google.com>
src/cmd/compile/internal/types2/stmt.go
src/go/types/stmt.go

index ac22f89ab8eb181048421eb160ac5c5de5bdabd3..a6767321a4c7294033fb581f89d1c48454716885 100644 (file)
@@ -1010,10 +1010,6 @@ func rangeKeyVal(typ Type, allowVersion func(goVersion) bool) (key, val Type, ca
        bad := func(cause string) (Type, Type, string, bool) {
                return Typ[Invalid], Typ[Invalid], cause, false
        }
-       toSig := func(t Type) *Signature {
-               sig, _ := coreType(t).(*Signature)
-               return sig
-       }
 
        orig := typ
        switch typ := arrayPtrDeref(coreType(typ)).(type) {
@@ -1044,23 +1040,26 @@ func rangeKeyVal(typ Type, allowVersion func(goVersion) bool) (key, val Type, ca
                if !buildcfg.Experiment.RangeFunc && allowVersion != nil && !allowVersion(go1_23) {
                        return bad("requires go1.23 or later")
                }
-               assert(typ.Recv() == nil)
+               // check iterator arity
                switch {
                case typ.Params().Len() != 1:
                        return bad("func must be func(yield func(...) bool): wrong argument count")
-               case toSig(typ.Params().At(0).Type()) == nil:
-                       return bad("func must be func(yield func(...) bool): argument is not func")
                case typ.Results().Len() != 0:
                        return bad("func must be func(yield func(...) bool): unexpected results")
                }
-               cb := toSig(typ.Params().At(0).Type())
-               assert(cb.Recv() == nil)
+               assert(typ.Recv() == nil)
+               // check iterator argument type
+               cb, _ := coreType(typ.Params().At(0).Type()).(*Signature)
                switch {
+               case cb == nil:
+                       return bad("func must be func(yield func(...) bool): argument is not func")
                case cb.Params().Len() > 2:
                        return bad("func must be func(yield func(...) bool): yield func has too many parameters")
                case cb.Results().Len() != 1 || !isBoolean(cb.Results().At(0).Type()):
                        return bad("func must be func(yield func(...) bool): yield func does not return bool")
                }
+               assert(cb.Recv() == nil)
+               // determine key and value types, if any
                if cb.Params().Len() >= 1 {
                        key = cb.Params().At(0).Type()
                }
index b1346bb27ed3810470f0f5725b62ad1d29bd86a8..e4af27dffe620528453e6ba7b49b58120b73ff7b 100644 (file)
@@ -1028,10 +1028,6 @@ func rangeKeyVal(typ Type, allowVersion func(goVersion) bool) (key, val Type, ca
        bad := func(cause string) (Type, Type, string, bool) {
                return Typ[Invalid], Typ[Invalid], cause, false
        }
-       toSig := func(t Type) *Signature {
-               sig, _ := coreType(t).(*Signature)
-               return sig
-       }
 
        orig := typ
        switch typ := arrayPtrDeref(coreType(typ)).(type) {
@@ -1062,23 +1058,26 @@ func rangeKeyVal(typ Type, allowVersion func(goVersion) bool) (key, val Type, ca
                if !buildcfg.Experiment.RangeFunc && allowVersion != nil && !allowVersion(go1_23) {
                        return bad("requires go1.23 or later")
                }
-               assert(typ.Recv() == nil)
+               // check iterator arity
                switch {
                case typ.Params().Len() != 1:
                        return bad("func must be func(yield func(...) bool): wrong argument count")
-               case toSig(typ.Params().At(0).Type()) == nil:
-                       return bad("func must be func(yield func(...) bool): argument is not func")
                case typ.Results().Len() != 0:
                        return bad("func must be func(yield func(...) bool): unexpected results")
                }
-               cb := toSig(typ.Params().At(0).Type())
-               assert(cb.Recv() == nil)
+               assert(typ.Recv() == nil)
+               // check iterator argument type
+               cb, _ := coreType(typ.Params().At(0).Type()).(*Signature)
                switch {
+               case cb == nil:
+                       return bad("func must be func(yield func(...) bool): argument is not func")
                case cb.Params().Len() > 2:
                        return bad("func must be func(yield func(...) bool): yield func has too many parameters")
                case cb.Results().Len() != 1 || !isBoolean(cb.Results().At(0).Type()):
                        return bad("func must be func(yield func(...) bool): yield func does not return bool")
                }
+               assert(cb.Recv() == nil)
+               // determine key and value types, if any
                if cb.Params().Len() >= 1 {
                        key = cb.Params().At(0).Type()
                }