Skip to content

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

Open
@zigo101

Description

@zigo101

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. The yield call just returns a false when panicking,)

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)
}

Metadata

Metadata

Assignees

Labels

DocumentationIssues describing a change to documentation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Status

Todo

Relationships

None yet

Development

No branches or pull requests

Issue actions