You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/compile: return statement does not set result parameters before executing deferred function returned by a literal function #32175
See the playground example: https://play.golang.org/p/SptuCsFa3uw
I have two defer statements in their own functions. They both call anonymous functions that return a function to execute at the end of the function (when the result variable has been assigned the value in the return statement).
The functions differ in how the result variable is assigned it's value. One does it implicitly by returning the expression. The other does a bare return after modifying the named return variable.
What did you expect to see?
I expected to see the anonymous function (that prints "on exit") print the result variable with a value of 16 in the not_working() function.
What did you see instead?
That anonymous function printed 0 for the result return variable (the initial value of the result name return variable).
changed the title [-]bug: defer on-entry and on-semantics with anonymous functions[/-][+]defer: on-entry and on-semantics with anonymous functions[/+]on May 21, 2019
changed the title [-]defer: on-entry and on-semantics with anonymous functions[/-][+]cmd/compile: return statement does not set result parameters before executing deferred function returned by a literal function[/+]on May 22, 2019
I suspect the issue is that return statements don't mark the result parameters as Assigned, so we perform the closure pass-by-value optimization. E.g., adding result = 0 after var f func() = ... in not_working makes it behave correctly.
Could be related to the introduction of named output arguments and the duality involved (#31630) ?
There are in fact two paths now to return the values:
direct via the return statement (bypassing the output variables that were declared as function arguments)
assignment to output variables where they are picked up by return statement without arguments.
The timeline (sequence of instructions) might not be correct in combination with one or more levels of defer.
@StephanVerbeeck I don't think so. I'm pretty sure it's like I stated in my last comment: outer.Class() != PPARAMOUT should be outermost.Class() != PPARAMOUT, because in multiply-nested function literals when referencing the outermost function's return parameters (like in the issue report), outer.Class() will be PAUTOHEAP instead.
Activity
[-]bug: defer on-entry and on-semantics with anonymous functions[/-][+]defer: on-entry and on-semantics with anonymous functions[/+]bcmills commentedon May 22, 2019
An equivalent but slightly-more-readable version: https://play.golang.org/p/h5m8NezzYro
[-]defer: on-entry and on-semantics with anonymous functions[/-][+]cmd/compile: return statement does not set result parameters before executing deferred function returned by a literal function[/+]bcmills commentedon May 22, 2019
CC @randall77 @griesemer @mdempsky
ianlancetaylor commentedon May 22, 2019
Works as expected with gccgo.
Works as expected with Go 1.4 and earlier, but not with Go 1.5 and later.
mdempsky commentedon May 22, 2019
I suspect the issue is that
returnstatements don't mark the result parameters asAssigned, so we perform the closure pass-by-value optimization. E.g., addingresult = 0aftervar f func() = ...innot_workingmakes it behave correctly.mdempsky commentedon May 22, 2019
Actually, I think it's even more mundane than that: the first occurrence of
outerhere should beoutermost, like the rest of the line:go/src/cmd/compile/internal/gc/closure.go
Lines 188 to 194 in 24b4301
StephanVerbeeck commentedon May 22, 2019
Could be related to the introduction of named output arguments and the duality involved (#31630) ?
There are in fact two paths now to return the values:
The timeline (sequence of instructions) might not be correct in combination with one or more levels of defer.
mdempsky commentedon May 22, 2019
@StephanVerbeeck I don't think so. I'm pretty sure it's like I stated in my last comment:
outer.Class() != PPARAMOUTshould beoutermost.Class() != PPARAMOUT, because in multiply-nested function literals when referencing the outermost function's return parameters (like in the issue report),outer.Class()will bePAUTOHEAPinstead.gopherbot commentedon May 22, 2019
Change https://golang.org/cl/178541 mentions this issue:
cmd/compile: fix capture-by-reference of return parameterssandan commentedon May 28, 2019
Thanks! How do I find out what version of golang will have the fix?
mdempsky commentedon May 28, 2019
@sandan The fix will be included in Go 1.13. Thanks for the report!