From f6c89abf897c4adf7dbd598029f0c12452b4ca25 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 16 Sep 2024 13:50:24 -0700 Subject: [PATCH] go/types, types2: slightly simplify rangeKeyVal function 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 Reviewed-by: Robert Griesemer Auto-Submit: Robert Griesemer Reviewed-by: Tim King --- src/cmd/compile/internal/types2/stmt.go | 17 ++++++++--------- src/go/types/stmt.go | 17 ++++++++--------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/cmd/compile/internal/types2/stmt.go b/src/cmd/compile/internal/types2/stmt.go index ac22f89ab8..a6767321a4 100644 --- a/src/cmd/compile/internal/types2/stmt.go +++ b/src/cmd/compile/internal/types2/stmt.go @@ -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() } diff --git a/src/go/types/stmt.go b/src/go/types/stmt.go index b1346bb27e..e4af27dffe 100644 --- a/src/go/types/stmt.go +++ b/src/go/types/stmt.go @@ -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() } -- 2.48.1