Skip to content

defer on inline function runs indefinitely #1697

@kenny-statsig

Description

@kenny-statsig

The following program sample.go triggers an unexpected result

package main

import "fmt"

func run(fn func() bool) bool {
	fmt.Println("run")
	for {
		return fn()
	}
}

func main() {
	run(func() bool {
		deferred := func() {
			fmt.Println("deferred")
		}
		defer deferred()
		fmt.Println("done fn")
		return true
	})

	fmt.Println("done main")
}

Expected result

run
done fn
deferred
done main

Got

Nothing, Eval runs indefinitely

Yaegi Version

v0.16.1

Additional Notes

Seems to only happen when defer is used on a function defined inline/same scope as where defer is called from. For example, the following code works:

package main

import "fmt"

func run(fn func() bool) bool {
	fmt.Println("run")
	for {
		return fn()
	}
}

func deferred() {
	fmt.Println("deferred")
}

func main() {
	run(func() bool {
		defer deferred()
		fmt.Println("done fn")
		return true
	})

	fmt.Println("done main")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions