cmd/compile: handle defined iter func type correctly

Fixed #64930

Change-Id: I916de7f97116fb20cb2f3f0b425ac34409afd494
Reviewed-on: https://go-review.googlesource.com/c/go/+/553436
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
Cuong Manh Le 2024-01-02 22:28:49 +07:00 committed by Gopher Robot
parent 1ae729e6d3
commit 881869dde0
2 changed files with 26 additions and 1 deletions

View File

@ -934,7 +934,7 @@ func (r *rewriter) endLoop(loop *forLoop) {
if rfunc.Params().Len() != 1 {
base.Fatalf("invalid typecheck of range func")
}
ftyp := rfunc.Params().At(0).Type().(*types2.Signature) // func(...) bool
ftyp := types2.CoreType(rfunc.Params().At(0).Type()).(*types2.Signature) // func(...) bool
if ftyp.Results().Len() != 1 {
base.Fatalf("invalid typecheck of range func")
}

View File

@ -311,6 +311,30 @@ func testcalls() {
}
}
type iter3YieldFunc func(int, int) bool
func iter3(list ...int) func(iter3YieldFunc) {
return func(yield iter3YieldFunc) {
for k, v := range list {
if !yield(k, v) {
return
}
}
}
}
func testcalls1() {
ncalls := 0
for k, v := range iter3(1, 2, 3) {
_, _ = k, v
ncalls++
}
if ncalls != 3 {
println("wrong number of calls:", ncalls, "!= 3")
panic("fail")
}
}
func main() {
testfunc0()
testfunc1()
@ -323,4 +347,5 @@ func main() {
testfunc8()
testfunc9()
testcalls()
testcalls1()
}