Skip to content

cmd/compile: return statement does not set result parameters before executing deferred function returned by a literal function #32175

Closed

Description

@sandan

What version of Go are you using (go version)?

$ go version
go version go1.12.4 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/mark/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"

What did you do?

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

Activity

  1. 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
  2. bcmills commented on May 22, 2019

    @bcmills
    Contributor

    An equivalent but slightly-more-readable version: https://play.golang.org/p/h5m8NezzYro

  3. 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
  4. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on May 22, 2019
  5. added this to the Go1.13 milestone on May 22, 2019
  6. bcmills commented on May 22, 2019

    @bcmills
    Contributor
  7. ianlancetaylor commented on May 22, 2019

    @ianlancetaylor
    Contributor

    Works as expected with gccgo.

    Works as expected with Go 1.4 and earlier, but not with Go 1.5 and later.

  8. mdempsky commented on May 22, 2019

    @mdempsky
    Contributor

    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.

  9. mdempsky commented on May 22, 2019

    @mdempsky
    Contributor

    Actually, I think it's even more mundane than that: the first occurrence of outer here should be outermost, like the rest of the line:

    // out parameters will be assigned to implicitly upon return.
    if outer.Class() != PPARAMOUT && !outermost.Addrtaken() && !outermost.Assigned() && v.Type.Width <= 128 {
    v.Name.SetByval(true)
    } else {
    outermost.SetAddrtaken(true)
    outer = nod(OADDR, outer, nil)
    }

  10. StephanVerbeeck commented on May 22, 2019

    @StephanVerbeeck

    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:

    1. direct via the return statement (bypassing the output variables that were declared as function arguments)
    2. 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.

  11. mdempsky commented on May 22, 2019

    @mdempsky
    Contributor

    @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.

  12. gopherbot commented on May 22, 2019

    @gopherbot
    Contributor

    Change https://golang.org/cl/178541 mentions this issue: cmd/compile: fix capture-by-reference of return parameters

  13. sandan commented on May 28, 2019

    @sandan
    Author

    Thanks! How do I find out what version of golang will have the fix?

  14. mdempsky commented on May 28, 2019

    @mdempsky
    Contributor

    @sandan The fix will be included in Go 1.13. Thanks for the report!

  15. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    and removed
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Jun 12, 2019
  16. locked and limited conversation to collaborators on Jun 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions