-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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/go: vet incorrectly flags loop variable captured by func literal in GOPATH mode #70641
Comments
CC @golang/tools-team |
It sounds like the loopvar analyzer needs to be made aware of this behavior in GOPATH mode. @matloob can you clarify the semantics here? Should we assume the effective language version in GOPATH mode is the runtime version? |
My understanding is that in the absence of a go.mod file the go tool uses Go 1.16 semantics. So the warning is correct when not using modules. |
@ianlancetaylor I think we intended to limit GOPATH mode to Go 1.16 at first, but when we decided to keep GOPATH mode around for people who are for one reason or another not using modules, we decided to let the language version float forward, since otherwise you couldn't use generics ever, for example. The behavior of the 'go test' confirms that. Or at least, 'go test' and 'go vet' clearly disagree here. |
Ah, thanks. |
This was previously reported as #65612 in the command-line-arguments context, but it's the same underlying issue for GOPATH. We fixed this in 1.23 with https://go.dev/cl/593156, explicitly setting the go version to the toolchain version when outside of a module. We considered backporting the change to Go 1.22 but decided not to because it didn't seem to be a serious problem that prevented a program from working at all (and Go 1.22 already had issues with not passing in -lang to the compiler that we wanted to fix at the same time, and would change the behavior for building programs). (edit: also good job gaby for surfacing #65612 in the related issues!) |
Thanks @matloob, I'd missed (or forgotten) that context. I think it's reasonable not to backport this fix for 1.22 GOPATH mode. Therefore, closing as a dupe. |
Just FRI: The PR https://go.dev/cl/593156 tired to fix #66092 but not totally. |
Go version
go version go1.22.9 darwin/amd64
Output of
go env
in your module/workspace:What did you do?
I wrote the follow unit test in GOPATH mode.
Then I ran
go vet
on the package.What did you see happen?
What did you expect to see?
No issues.
In Go 1.22+, the compiler uses the new loop semantics, even in GOPATH mode. This is proven by running the test, which gives output like so. (The exact address is different each time of course, but the important part is that
x
has a different address in each test case.)Therefore,
go vet
should not have applied this check at all.The text was updated successfully, but these errors were encountered: