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

cmd/compile: generate closure for go/defer builtin calls #19710

Closed
unixpickle opened this issue Mar 25, 2017 · 11 comments
Closed

cmd/compile: generate closure for go/defer builtin calls #19710

unixpickle opened this issue Mar 25, 2017 · 11 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@unixpickle
Copy link

unixpickle commented Mar 25, 2017

Please answer these questions before submitting your issue. Thanks!

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

Go 1.8.
Also happens on play.golang.org.

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"

What did you do?

Evaluate this code:

package main

import "fmt"

func main() {
	fmt.Println("should be 0:", len(foobar()))
}

func foobar() map[int]bool {
	m := map[int]bool{}
	for i := 0; i < 3; i++ {
		m[i] = true
		defer delete(m, i)
	}
	return m
}

What did you expect to see?

should be 0: 0

What did you see instead?

should be 0: 2

The defer statement is supposed to freeze its arguments the instant it is evaluated, but it does not when used with delete. Compare to this program, which works correctly:

package main

import "fmt"

func main() {
	fmt.Println("should be 0:", len(foobar()))
}

func foobar() map[int]bool {
	m := map[int]bool{}
	for i := 0; i < 3; i++ {
		m[i] = true
		defer myDelete(m, i)
	}
	return m
}

func myDelete(m map[int]bool, k int) {
	delete(m, k)
}
unixpickle added a commit to unixpickle/anynet that referenced this issue Mar 25, 2017
unixpickle added a commit to unixpickle/sgdstore that referenced this issue Mar 25, 2017
@mattn
Copy link
Member

mattn commented Mar 25, 2017

This is my interesting. Why you use defer delete?

@unixpickle
Copy link
Author

@mattn I have a map that I occasionally add temporary values to (for intermediate computations). I was trying to use defer+delete to remove those temporary entries automatically.

@bradfitz
Copy link
Contributor

bradfitz commented Mar 25, 2017

Good news and bad news:

Bad news is this has been broken forever. (I tested Go 1.5, Go 1.6, Go 1.7, and Go 1.8) Because it's not a regression and has been broken forever, it doesn't qualify for a Go 1.8.1 cherry-pick.

Good news is that it's fixed at tip (what will be Go 1.9).

I haven't bisected to see where it was fixed and whether it was intentional. If it was fixed by accident, it likely still lacks a test.

/cc @griesemer @randall77 @mdempsky @josharian

@bradfitz bradfitz changed the title defer+delete argument bug cmd/compile: defer+delete argument bug Mar 25, 2017
@bradfitz bradfitz added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Testing An issue that has been verified to require only test changes, not just a test failure. labels Mar 25, 2017
@bradfitz bradfitz added this to the Go1.9 milestone Mar 25, 2017
@josharian
Copy link
Contributor

Probably fixed by the order.go changes for mapdelete_fast*. Yes, needs a test. Should double-check go delete(x), and defer/delete of the other built-in functions.

@cespare
Copy link
Contributor

cespare commented Mar 25, 2017

A bisect points at 5d6b7fc (CL 38172)

runtime: add mapdelete_fast*

@josharian
Copy link
Contributor

josharian commented Mar 25, 2017

order.go, walk.go, and ssa.go are scattered with special handling of go/defer of built-in functions. I wonder whether instead we should just do a very early rewrite of

defer builtin(a, b, c)

to

defer func(a typa, b typb, c typc) {
  builtin(a, b, c)
}(a, b, c)

That would probably also simplify instrumentation of such cases, which are also the topic of scattered special handling.

@randall77
Copy link
Contributor

@josharian I was thinking the same thing.

@josharian
Copy link
Contributor

I think we should add a test for 1.9 and and leave the closurization of deferred built-ins for 1.10. (That will also enable us to simplify other things, like not lowering OPANIC in walk.go.)

I'll send a test CL.

@gopherbot
Copy link

CL https://golang.org/cl/43497 mentions this issue.

gopherbot pushed a commit that referenced this issue May 15, 2017
Updates #19710

Change-Id: I37d19a4a02b9010cb5f9062b3d141d5d65e12e01
Reviewed-on: https://go-review.googlesource.com/43497
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
@josharian josharian modified the milestones: Go1.10, Go1.9 May 15, 2017
@josharian josharian removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Testing An issue that has been verified to require only test changes, not just a test failure. labels May 15, 2017
@josharian
Copy link
Contributor

Note to self: walkprintfunc does this now for defer println(...). It also has a minor naming bug: lookupN("print·%d", walkprintfunc_prgen) generates names like "".print·%d1·f. :P

@cherrymui cherrymui changed the title cmd/compile: defer+delete argument bug cmd/compile: generate closure for go/defer builtin calls Nov 29, 2017
@cherrymui cherrymui modified the milestones: Go1.10, Go1.11 Nov 29, 2017
@gopherbot gopherbot modified the milestones: Go1.11, Unplanned May 23, 2018
@ALTree ALTree added the NeedsFix The path to resolution is known, but the work has not been done. label Sep 26, 2019
@cuonglm
Copy link
Member

cuonglm commented Oct 22, 2020

This is fixed at tip, we can close this now cc @mdempsky

@golang golang locked and limited conversation to collaborators Oct 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests