1
0
mirror of https://github.com/golang/go synced 2024-11-25 06:57:58 -07:00

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 <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>
This commit is contained in:
Robert Griesemer 2024-09-16 13:50:24 -07:00 committed by Gopher Robot
parent ae8708f744
commit f6c89abf89
2 changed files with 16 additions and 18 deletions

View 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()
}

View 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()
}