-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
x/tools/go/analysis: checkers do not (but should) reject Go 1.22 programs #61174
Comments
Change https://go.dev/cl/507975 mentions this issue: |
Change https://go.dev/cl/507880 mentions this issue: |
Change https://go.dev/cl/507879 mentions this issue: |
This concern affects the combination of go/packages, go/analysis, and go/types: The various GoVersion fields are only populated when a minimum Go version was deduced from go.work or go.mod. But this leaves us with no GoVersion for the standard library, which A consequence of that is that tools can't rely exclusively on the GoVersion field to know if they're too old to correctly interpret the code; they also need to figure out the actual Go version and use that when GoVersion isn't set. Is there something we can do about that? |
For Go 1.21, I don't think we have a great answer. Luckily it shouldn't be a huge problem either: Nothing should really care about the Go version for the Go 1.21 release. These changes are aimed at setting us up for Go 1.22, specifically the loopvar change. For non-standard-library packages, only code in modules will see the new semantics, so the check can be written so that "no version" = "old loop variable semantics". And any module that says "go 1.22" should be properly rejected by an old tool. That "no version" = "old loopvar semantics" does not handle the standard library though. I wonder if for Go 1.22 we should try having go list -json report the "std" and "cmd" modules in the Package.Module field. That's too big a change to land this late in the Go 1.21 cycle, but as long as it was in Go 1.22, that would be enough to handle the standard library in any checker that cared because of loopvar. |
Having the "std" and "cmd" modules would be great. Do note that for checkers, language changes may not be the only reason to care about the Go version. Staticcheck has long been using the Go version to guard checks that make recommendations concerning use of the standard library, such as suggesting None of this is really important for 1.21 specifically, though, and waiting until 1.22 is fine. As an aside, all of the GoVersion-related changes, including per-file versions, really benefit Staticcheck. We went from having a |
Glad to hear it. Thanks for the feedback @dominikh! |
…method Clients of go/types, such as analyzers, may need to know which specific Go version a package is written for. Record that information in the Package and expose it using the new GoVersion method. Update parseGoVersion to handle the new Go versions that may be passed around starting in Go 1.21.0: versions like "go1.21.0" and "go1.21rc2". This is not strictly necessary today, but it adds some valuable future-proofing. While we are here, change NewChecker from panicking on invalid version to saving an error for returning later from Files. Go versions are now likely to be coming from a variety of sources, not just hard-coded in calls to NewChecker, making a panic inappropriate. For #61174. Fixes #61175. Change-Id: Ibe41fe207c1b6e71064b1fe448ac55776089c541 Reviewed-on: https://go-review.googlesource.com/c/go/+/507975 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Robert Findley <[email protected]>
Type checking of a package depends on the Go language version in effect for that package. We have been not setting it and assuming "latest" is good enough, but that is likely to become untrue in the future, and it violates Go 1.21's emphasis on forward compatibility, namely tools recognizing when they shouldn't be processing newer code. Pass the Go version along from go/packages to go/types, to allow go/types to apply the version when type-checking. This is tested by CL 507880. For golang/go#61174. Change-Id: I49353dede9c7c095c2cd0c4a6959f9f6e6a06ec5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/507879 gopls-CI: kokoro <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Russ Cox <[email protected]>
I haven't brought the code into the main repo yet. |
Change https://go.dev/cl/509099 mentions this issue: |
Submitted the revendor. |
Generated by: go install golang.org/x/tools/cmd/bundle@latest go install golang.org/x/build/cmd/updatestd@latest updatestd -goroot=$GOROOT -branch=master For #36905. For #55079. Fixes #61174 (vet checkers understanding Go language version). Fixes #61200 (slog InfoCtx -> InfoContext etc). Change-Id: I4f2c86960ce72d6df06e23da1b1297ab3ff2eecf Reviewed-on: https://go-review.googlesource.com/c/go/+/509099 Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]> Reviewed-by: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Damien Neil <[email protected]>
…method Clients of go/types, such as analyzers, may need to know which specific Go version a package is written for. Record that information in the Package and expose it using the new GoVersion method. Update parseGoVersion to handle the new Go versions that may be passed around starting in Go 1.21.0: versions like "go1.21.0" and "go1.21rc2". This is not strictly necessary today, but it adds some valuable future-proofing. While we are here, change NewChecker from panicking on invalid version to saving an error for returning later from Files. Go versions are now likely to be coming from a variety of sources, not just hard-coded in calls to NewChecker, making a panic inappropriate. For golang#61174. Fixes golang#61175. Change-Id: Ibe41fe207c1b6e71064b1fe448ac55776089c541 Reviewed-on: https://go-review.googlesource.com/c/go/+/507975 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Robert Findley <[email protected]>
Generated by: go install golang.org/x/tools/cmd/bundle@latest go install golang.org/x/build/cmd/updatestd@latest updatestd -goroot=$GOROOT -branch=master For golang#36905. For golang#55079. Fixes golang#61174 (vet checkers understanding Go language version). Fixes golang#61200 (slog InfoCtx -> InfoContext etc). Change-Id: I4f2c86960ce72d6df06e23da1b1297ab3ff2eecf Reviewed-on: https://go-review.googlesource.com/c/go/+/509099 Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]> Reviewed-by: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Damien Neil <[email protected]>
…method Clients of go/types, such as analyzers, may need to know which specific Go version a package is written for. Record that information in the Package and expose it using the new GoVersion method. Update parseGoVersion to handle the new Go versions that may be passed around starting in Go 1.21.0: versions like "go1.21.0" and "go1.21rc2". This is not strictly necessary today, but it adds some valuable future-proofing. While we are here, change NewChecker from panicking on invalid version to saving an error for returning later from Files. Go versions are now likely to be coming from a variety of sources, not just hard-coded in calls to NewChecker, making a panic inappropriate. For golang#61174. Fixes golang#61175. Change-Id: Ibe41fe207c1b6e71064b1fe448ac55776089c541 Reviewed-on: https://go-review.googlesource.com/c/go/+/507975 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Robert Findley <[email protected]>
Generated by: go install golang.org/x/tools/cmd/bundle@latest go install golang.org/x/build/cmd/updatestd@latest updatestd -goroot=$GOROOT -branch=master For golang#36905. For golang#55079. Fixes golang#61174 (vet checkers understanding Go language version). Fixes golang#61200 (slog InfoCtx -> InfoContext etc). Change-Id: I4f2c86960ce72d6df06e23da1b1297ab3ff2eecf Reviewed-on: https://go-review.googlesource.com/c/go/+/509099 Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]> Reviewed-by: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Damien Neil <[email protected]>
Checkers built on x/tools/go/analysis do not reject Go 1.22 programs but should. Will fix.
The text was updated successfully, but these errors were encountered: