cmd/compile: implement range over func

Add compiler support for range over functions.
See the large comment at the top of
cmd/compile/internal/rangefunc/rewrite.go for details.

This is only reachable if GOEXPERIMENT=range is set,
because otherwise type checking will fail.

For proposal #61405 (but behind a GOEXPERIMENT).
For #61717.

Change-Id: I05717f94e63089c503acc49b28b47edeb4e011b4
Reviewed-on: https://go-review.googlesource.com/c/go/+/510541
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Auto-Submit: Russ Cox <rsc@golang.org>
This commit is contained in:
Russ Cox 2023-06-14 10:56:49 -04:00 committed by Gopher Robot
parent a94347a05c
commit 2fba42cb52
17 changed files with 2101 additions and 217 deletions

View file

@ -583,6 +583,17 @@ func walkCall(n *ir.CallExpr, init *ir.Nodes) ir.Node {
return e
}
if name, ok := n.X.(*ir.Name); ok {
sym := name.Sym()
if sym.Pkg.Path == "go.runtime" && sym.Name == "deferrangefunc" {
// Call to runtime.deferrangefunc is being shared with a range-over-func
// body that might add defers to this frame, so we cannot use open-coded defers
// and we need to call deferreturn even if we don't see any other explicit defers.
ir.CurFunc.SetHasDefer(true)
ir.CurFunc.SetOpenCodedDeferDisallowed(true)
}
}
walkCall1(n, init)
return n
}