Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Function expression should be evaluated before arguments #1271

Open
@nevkontakte

Description

@nevkontakte

Consider the following snippet:

package main

var ok = false

func f() func(int, int) {
	ok = true
	return func(int, int) {}
}

func g() (int, int) {
	if !ok {
		panic("Bad order!")
	}
	return 0, 0
}

func main() {
	f()(g())
}

In it, the f()(g()) should be equivalent to:

funExpr := f()
a, b := g()
funExpr(a, b)

However, in GopherJS it is:

a, b := g()
funExpr := f()
funExpr(a, b)

https://gopherjs.github.io/playground/#/FMGNfOX9RA

https://go.dev/play/p/DGqo4zOvkUM

The bug is here:

argExprs = fc.expandTupleArgs(argExprs)
. When a function argument is another function's returned tuple, the inner function is broken out into a separate statement that's evaluated too early. To avoid that we can use ES6 spread operator and avoid a separate statement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions