Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile,wiki: clarify whether or not the panics created in loop bodies should propagate to function iterators #71830

Open
zigo101 opened this issue Feb 19, 2025 · 4 comments
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation Issues describing a change to documentation. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@zigo101
Copy link

zigo101 commented Feb 19, 2025

By the current doc, the loop body is viewed as being directly nested both in the innermost nesting function of the loop and in the yield function which is called in the iterator function.

In my opinion, the loop body should be only viewed as directly nested both in the innermost nesting function of the loop.
In other words, when a function is used as an iterator, the yield calls should viewed as being removed from the iterator function.
The effect of the doc change is that panics created in loop body are not catch-able in the iterator function.

(Another explanation: the panics created in loop bodies don't propagate to iterator functions.)

For example, the following two programs should have the same behavior, but they don't in 1.24.0 implementation. This is weird to users. By adopting the above explanations, then their behavior will become the same (the two recover calls should be both no-op).

package main

import "fmt"

func main() {
	defer foo()
}

func foo() {
	for range iter {}
	panic(123)
}

func iter(yield func(int) bool) {
	defer func() {
		fmt.Println(recover())
	}()
	yield(0)
}
package main

import "fmt"

func main() {
	defer foo()
}

func foo() {
	for range iter {
		panic(123)
	}
}

func iter(yield func(int) bool) {
	defer func() {
		fmt.Println(recover())
	}()
	yield(0)
}
@gopherbot gopherbot added the Documentation Issues describing a change to documentation. label Feb 19, 2025
@zigo101
Copy link
Author

zigo101 commented Feb 19, 2025

[edit]: sorry, pls ignored this comment. It looks I over-thought about it. It already works as such now. So the problem is much simpler than I thought.

If the above explanations are adopted, the following program should exit without unrecovered pancis (it crashes now). the two panics in the following program should be both caught? Now, only one is caught.

package main

import "fmt"

func main() {
	foo()
}

func foo() {
	defer func() {
		fmt.Println("##", recover());
	}()
	for range iter {
		panic(123)
	}
}

func iter(yield func() bool) {
	defer func() {
		fmt.Println(">>", recover());
	}()
	defer panic(789)
	yield()
}

A bit related to #34530.

In other words, if the above explanations are adopted, there might be multiple coexisting "newest" unrecovered panics in a goroutine.

@zigo101 zigo101 changed the title doc: in ranging over an iterator function, clarify the relation of the loop body and the call to the iterator function spec and doc: in ranging over an iterator function, clarify the relation of the loop body and the call to the iterator function Feb 19, 2025
@zigo101 zigo101 changed the title spec and doc: in ranging over an iterator function, clarify the relation of the loop body and the call to the iterator function spec,doc: in ranging over an iterator function, clarify the relation of the loop body and the call to the iterator function Feb 19, 2025
@seankhliao seankhliao changed the title spec,doc: in ranging over an iterator function, clarify the relation of the loop body and the call to the iterator function spec: clarify scopes for function iterators Feb 19, 2025
@seankhliao
Copy link
Member

cc @rsc

@mknyszek
Copy link
Contributor

CC @dr2chase

@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 19, 2025
@mknyszek mknyszek added this to the Backlog milestone Feb 19, 2025
@ianlancetaylor ianlancetaylor added the compiler/runtime Issues related to the Go compiler and/or runtime. label Feb 19, 2025
@zigo101 zigo101 changed the title spec: clarify scopes for function iterators cmd/compile,wiki: clarify whether or not the panics created in loop bodies should propagate to function iterators Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation Issues describing a change to documentation. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
Development

No branches or pull requests

7 participants