aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/rangefunc
AgeCommit message (Collapse)Author
2026-03-31cmd/compile: fix wrong label loop during rangefunc rewriteCuong Manh Le
During the rangefunc rewrite, the compiler must correctly identify the target of branch statements. When a label is defined within a nested scope - such as inside a function literal or a closure - it can shadow a label with the same name in the outer scope. If the rewrite logic does not account for this shadowing, it may incorrectly associate a branch with a nested label rather than the intended loop label. Since the typechecker already guarantees that labels are unique within their respective scopes, any duplicate label name encountered must belong to a nested scope. These should be skipped to ensure branch computing correctly targets the current range-loop scope. Fixes #78408 Change-Id: I4dce8a4d956f41b3a717a509f8c3f7478720be9f Reviewed-on: https://go-review.googlesource.com/c/go/+/761420 Reviewed-by: Keith Randall <khr@golang.org> Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jakub Ciolek <jakub@ciolek.dev> Reviewed-by: Junyang Shao <shaojunyang@google.com> Reviewed-by: Keith Randall <khr@google.com>
2026-03-25cmd/compile: preserve variadic signature on range-over-funcCuong Manh Le
When rewriting range-over-func loops, copy the variadic bit from the original function type into the synthesized body closure's type info. This keeps the generated closure signature aligned with the source function and avoids losing variadic-ness during rewrite. Fixes #78314 Change-Id: I4b5f4628e8c8c14d4ff89dd6b996837264c5cb61 Reviewed-on: https://go-review.googlesource.com/c/go/+/758041 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jakub Ciolek <jakub@ciolek.dev> Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2024-11-13cmd/compile: change status of "bad iterator" panicDavid Chase
Execution of the loop body previously either terminated the iteration (returned false because of a break, goto, or return) or actually panicked. The check against abi.RF_READY ensures that the body can no longer run and also panics. This CL in addition transitions the loop state to abi.RF_PANIC so that if this already badly-behaved iterator defer-recovers this panic, then the exit check at the loop context will catch the problem and panic there. Previously, panics triggered by attempted execution of a no-longer active loop would not trigger a panic at the loop context if they were defer-recovered. Change-Id: Ieeed2fafd0d65edb66098dc27dc9ae8c1e6bcc8c Reviewed-on: https://go-review.googlesource.com/c/go/+/625455 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Tim King <taking@google.com>
2024-10-26cmd/compile: use a non-fragile test for "does f contain closure c?"David Chase
The old test relied on naming conventions. The new test uses an explicit parent pointer chain initialized when the closures are created (in the same place that the names used in the older fragile test were assigned). Fixes #70035. Change-Id: Ie834103c7096e4505faaff3bed1fc6e918a21211 Reviewed-on: https://go-review.googlesource.com/c/go/+/622656 Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-07-30cmd/compile: verify that rangefunc assigning to no vars worksDavid Chase
This adds a test for for range seq2rangefunc { ... } and for onevar := range seq2rangefunc { ... } For #65236. Change-Id: I083f8e4c19eb4ba0d6024d5314ac29d941141778 Reviewed-on: https://go-review.googlesource.com/c/go/+/596135 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Keith Randall <khr@golang.org>
2024-07-24cmd/compile: fix typo in commentguoguangwu
Change-Id: Ied098312399d2d6557ebf0ee294ca0e71dfa677b GitHub-Last-Rev: 82f914e85756728976646b490223352059c1953a GitHub-Pull-Request: golang/go#68565 Reviewed-on: https://go-review.googlesource.com/c/go/+/600655 Auto-Submit: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2024-06-28cmd/compile: drop internal range-over-func vars from DWARF outputAlessandro Arzilli
Drops internal range-over-func variables from the DWARF output (excluding #yield which is used by Delve). Fixes #68238 Change-Id: Ic035e37ca3560347276cdc3b469fd564da33f4f5 Reviewed-on: https://go-review.googlesource.com/c/go/+/594257 Reviewed-by: Than McIntosh <thanm@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com> Auto-Submit: Hyang-Ah Hana Kim <hyangah@gmail.com>
2024-06-14cmd/compile: give the closure generated for rangefunc bodies a name.David Chase
The generated name has the form "#yield%d" for %d = 1, 2, 3, ... This may help the debugger connect execution within a rangefunc loop's body to the frame containing the rest of the source code. (It may not actually be necessary; we need to confirm with Alessandro Aarzilli or someone else on the Delve team.) Change-Id: Iabbb2ea5604a4bc1558c160819ac80197e1f2242 Reviewed-on: https://go-review.googlesource.com/c/go/+/592175 Reviewed-by: Than McIntosh <thanm@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Alessandro Arzilli <alessandro.arzilli@gmail.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2024-06-03all: make function comments match function namescuishuang
Change-Id: Ideb9ef00e7bc660b005fc080973fd9f3d36c5a1f Reviewed-on: https://go-review.googlesource.com/c/go/+/589536 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Run-TryBot: shuang cui <imcusg@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2024-05-23cmd/compile: repairing variable names and commentsDavid Chase
Change-Id: I2e775e92dcebf068426b3e2acbe088679c318ec4 Reviewed-on: https://go-review.googlesource.com/c/go/+/587578 Reviewed-by: Keith Randall <khr@golang.org> Auto-Submit: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-05-23internal/abi, cmd/compile, runtime: deduplicate rangefunc constsDavid Chase
Change-Id: I61ec5a7fa0c10f95ae2261c3349743d6fda2c1d2 Reviewed-on: https://go-review.googlesource.com/c/go/+/587596 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Keith Randall <khr@golang.org> Auto-Submit: Keith Randall <khr@golang.org>
2024-05-22runtime,cmd/compile: fix wording of rangefunc panic messagesDavid Chase
Change-Id: I883e7b6554646f32ad44e4ea6583440c33f02b84 Reviewed-on: https://go-review.googlesource.com/c/go/+/587595 Reviewed-by: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-05-21cmd/compile: for rangefunc, add checks and tests, fix panic interactionsDavid Chase
Modify rangefunc #next protocol to make it more robust Extra-terrible nests of rangefunc iterators caused the prior implementation to misbehave non-locally (in outer loops). Add more rangefunc exit flag tests, parallel and tricky This tests the assertion that a rangefunc iterator running in parallel can trigger the race detector if any of the parallel goroutines attempts an early exit. It also verifies that if everything else is carefully written, that it does NOT trigger the race detector if all the parts run time completion. Another test tries to rerun a yield function within a loop, so that any per-line shared checking would be fooled. Added all the use-of-body/yield-function checking. These checks handle pathological cases that would cause rangefunc for loops to behave in surprising ways (compared to "regular" for loops). For example, a rangefunc iterator might defer-recover a panic thrown in the syntactic body of a loop; this notices the fault and panics with an explanation Modified closure naming to ID rangefunc bodies Add a "-range<N>" suffix to the name of any closure generated for a rangefunc loop body, as provided in Alessandro Arzilli's CL (which is merged into this one). Fix return values for panicky range functions This removes the delayed implementation of "return x" by ensuring that return values (in rangefunc-return-containing functions) always have names and translating the "return x" into "#rv1 = x" where #rv1 is the synthesized name of the first result. Updates #61405. Change-Id: I933299ecce04ceabcf1c0c2de8e610b2ecd1cfd8 Reviewed-on: https://go-review.googlesource.com/c/go/+/584596 Reviewed-by: Matthew Dempsky <mdempsky@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Tim King <taking@google.com>
2024-01-08cmd/compile: handle defined iter func type correctlyCuong Manh Le
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>
2023-11-16cmd/compile: add compiler debug flag to disable range func iterator checkingDavid Chase
E.g. `GOEXPERIMENT=rangefunc go test -v -gcflags=-d=rangefunccheck=0 rangefunc_test.go` will turn off the checking and fail. The benchmarks, which do not use pathological iterators, run slightly faster. Change-Id: Ia3e175e86d67ef74bbae9bcc5d2def6a2cdf519d Reviewed-on: https://go-review.googlesource.com/c/go/+/541995 Run-TryBot: David Chase <drchase@google.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-11-15cmd/compile: check for iteration after range func loop exitDavid Chase
When this happens, panic. This is a revised version of a check that used #next, where this one instead uses a per-loop #exit flag, and catches more problematic iterators. Updates #56413. Updates #61405. Change-Id: I6574f754e475bb67b9236b4f6c25979089f9b629 Reviewed-on: https://go-review.googlesource.com/c/go/+/540263 Run-TryBot: David Chase <drchase@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2023-11-15cmd/compile: replace magic numbers "2" and "1" with named constantDavid Chase
This was originally done for a #next-encoding-based check for misbehaving loops, but it's a good idea anyhow because it makes the code slightly easier to follow or change (we may decide to check for errors the "other way" anyhow, later). Change-Id: I2ba8f6e0f9146f0ff148a900eabdefd0fffebf8b Reviewed-on: https://go-review.googlesource.com/c/go/+/540261 Run-TryBot: David Chase <drchase@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2023-09-20cmd/compile: implement range over funcRuss Cox
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>