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/vet: warn about capturing loop iterator variables #16520

Open
wolf0403 opened this Issue Jul 28, 2016 · 6 comments

Comments

Projects
None yet
7 participants
@wolf0403

wolf0403 commented Jul 28, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go version go1.6 darwin/amd64
  2. What operating system and processor architecture are you using (go env)?
    GOARCH="amd64"
    GOBIN=""
    GOEXE=""
    GOHOSTARCH="amd64"
    GOHOSTOS="darwin"
    GOOS="darwin"
    GOPATH="/Users/ryangao/go"
    GORACE=""
    GOROOT="/Users/ryangao/homebrew/Cellar/go/1.6/libexec"
    GOTOOLDIR="/Users/ryangao/homebrew/Cellar/go/1.6/libexec/pkg/tool/darwin_amd64"
    GO15VENDOREXPERIMENT="1"
    CC="clang"
    GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
    CXX="clang++"
    CGO_ENABLED="1"
  3. What did you do?
    Run "go vet" on code from https://play.golang.org/p/mUiuNfZDHT
  4. What did you expect to see?
    Expect "go vet" warns about error mentioned in
    https://golang.org/doc/faq#closures_and_goroutines
  5. What did you see instead?
    No warning issued.

@quentinmit quentinmit added this to the Go1.8Maybe milestone Aug 1, 2016

@quentinmit quentinmit added the NeedsFix label Oct 10, 2016

@rsc

This comment has been minimized.

Contributor

rsc commented Oct 20, 2016

This does seem like it would catch real problems if done well.
/cc @adonovan

@rsc rsc modified the milestones: Go1.9, Go1.8Maybe Oct 20, 2016

@rsc

This comment has been minimized.

Contributor

rsc commented Oct 20, 2016

Actually, note that those captures - at least when they are definite bugs - should usually show up in the race detector. It's not clear vet can contribute more than the race detector does without significant false positives.

@alandonovan

This comment has been minimized.

Contributor

alandonovan commented Oct 20, 2016

Although this bug pattern most often manifests with go statements (leading to data races), sequential (non-racy) instances are possible too, such as with defer or the simple example presented in this Issue.

vet already checks for patterns of the form:

for k, v := range seq {
    go/defer func() {
        ... k, v ...
    }()
}

The example presented in this Issue is more challenging to analyze because it requires proving that the function is not called within the loop, or at least failing to prove that it is called within the loop. Once the anonymous function has been stored in a data structure or passed to another function, vet can no longer precisely determine when it might be called.

In other words, I think the current vet check is probably as good as we can do without interprocedural analysis.

@mvdan

This comment has been minimized.

Member

mvdan commented May 11, 2018

It seems to me like we're late to add a new vet check in 1.11, given that the tree is frozen.

Is anyone intending to work on this for 1.12?

@mvdan mvdan modified the milestones: Go1.11, Go1.12 May 11, 2018

@alandonovan

This comment has been minimized.

Contributor

alandonovan commented May 11, 2018

Does anyone even know how to solve this problem? I don't.

@gopherbot gopherbot modified the milestones: Go1.12, Unplanned May 23, 2018

@mvdan mvdan added NeedsInvestigation and removed NeedsFix labels May 25, 2018

@mvdan

This comment has been minimized.

Member

mvdan commented May 25, 2018

I'm labelling as NeedsInvestigation, just to clarify that we're not sure that a fix is possible.

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