Skip to content

Commit

Permalink
cmd/compile: handle defined iter func type correctly
Browse files Browse the repository at this point in the history
Fixed #64930

Change-Id: I916de7f97116fb20cb2f3f0b425ac34409afd494
Reviewed-on: https://go-review.googlesource.com/c/go/+/553436
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Reviewed-by: Matthew Dempsky <[email protected]>
Auto-Submit: Cuong Manh Le <[email protected]>
  • Loading branch information
cuonglm authored and gopherbot committed Jan 8, 2024
1 parent 1ae729e commit 881869d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/rangefunc/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
25 changes: 25 additions & 0 deletions test/range4.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -323,4 +347,5 @@ func main() {
testfunc8()
testfunc9()
testcalls()
testcalls1()
}

0 comments on commit 881869d

Please sign in to comment.