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

proposal: spec: define return statement's result assignment order? #58233

bradfitz opened this issue Feb 1, 2023 · 6 comments

proposal: spec: define return statement's result assignment order? #58233

bradfitz opened this issue Feb 1, 2023 · 6 comments


Copy link

bradfitz commented Feb 1, 2023

A coworker sent out a code review with:

func As(err error) (_ Error, ok bool) {
	var e Error
	return e, errors.As(err, &e)

... and during review I said I wasn't sure whether it works. Turns out it worked but it's undefined. It happens to work in cmd/compile and doesn't work in gccgo.

The spec defines assignment order for e.g. a, b = foo(), bar() or a, b = b, a but not for return.

Another example which has different results between gc and gccgo:

I propose the spec defines this. My preference would be left to right assignment to results.

But admittedly it might break code like above. (which we fortunately rewrote to be simpler and explicit)

/cc @ianlancetaylor @griesemer (sorry)

Copy link

CC @mdempsky

Copy link

I'd think that returns work like assignments (of the result values to the result variables) and thus should behave the same way with respect to evaluation order. (Haven't investigated the spec yet.)

@griesemer griesemer self-assigned this Feb 1, 2023
Copy link

mdempsky commented Feb 2, 2023

Note that even x, y = e, f(&e) is undefined as to whether e is evaluated before or after the f(&e) call.

FWIW, cmd/compile already uses the same logic to handle assignment and return statements.

Copy link

go101 commented Feb 2, 2023

Related: #27821 (Edit: sorry, it should be #27804)

Copy link

beoran commented Feb 3, 2023

Perhaps this should be documented as an implementation restriction? "A compiler may disallow return statements for which the order of evaluation cannot be determined due to side effects." Or something similar?

Copy link

go101 commented Feb 3, 2023

It can determine for sure, it just doesn't now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Status: Incoming

No branches or pull requests

7 participants