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/gopls: incorrect size computation in fieldalignment analyzer #51016
Comments
This is a consequence of AST trimming that gopls does to reduce memory. There are other similar issues (for example #50196). IIRC AST trimming provides modest but not major benefit to most users (~25% memory reduction). I am not sure that is worth the loss in precision. I would like to remove our AST trimming in favor of teaching gopls to load fewer packages, but have hesitated to simply remove it before we have offsetting optimizations. Maybe we should. |
We've found a case in which there is a conflict between type t struct {
a *int
b netip.AddrPort
} The analyser complains with Is there a way to disable the AST trimming optimization? If not, could adding that option or fixing the analyser receive a higher priority from the gopls team? Thanks. |
There is no option, but we can add such an option with higher priority. It will take us longer to disable trimming entirely (we'd like to offset with other optimizations). |
Change https://go.dev/cl/415503 mentions this issue: |
I've checked out change 415503, built it, cleared the cache, and tried using the new git fetch https://go.googlesource.com/tools refs/changes/03/415503/5 && git checkout -b change-415503 FETCH_HEAD
cd ./gopls/
go install
go clean -cache
nvim ~/tmp/main.go Yet, the incorrect warning is still there. Is there any other cache I need to remove? I haven't found anything that would seem like it belongs to Am I doing something wrong? |
@ainar-g thanks for testing this out. Unfortunately this CL addresses only half of the problem: the other half is that we don't compute analysis facts for the imported packages: https://go.dev/issue/48738. We are going to work on that soon, as well. |
Indeed, I meant to use "updates" not "fixes" in the CL description to relate it to this issue. Fixed (I mean updated) now. And thanks @ainar-g for testing it out. |
The trimming optimization deletes parts of the syntax tree that don't affect the type checking of package-level declarations. It used to remove unexported struct fields, but this had observable consequences: it would affect the offset of later fields, and the size and aligment of structs, causing the 'fieldalignment' analyzer to report incorrect findings. Also, it required a complex workaround in the UI element for hovering over a type to account for the missing parts. This change restores unexported fields. The logic of recordFieldsUses has been inlined and specialized for each case (params+results, struct fields, interface methods) as they are more different than alike. BenchmarkMemStats on k8s shows +4% HeapAlloc: a lot, but a small part of the 32% saving of the trimming optimization as a whole. Also: - trimAST: delete func bodies without visiting them. - minor clarifications. Updates golang/go#51016 Change-Id: Ifae15564a8fb86af3ea186af351a2a92eb9deb22 Reviewed-on: https://go-review.googlesource.com/c/tools/+/415503 gopls-CI: kokoro <noreply+kokoro@google.com> Run-TryBot: Alan Donovan <adonovan@google.com> Auto-Submit: Alan Donovan <adonovan@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
Computing analysis facts is a large project. I have tentatively moved this to v0.10.0, just so it stays among our top priorities, but the likely reality is that it will take us ~months to have proper analysis facts. Proper analysis facts are one of the major outstanding priorities for gopls. |
@adonovan is this fixed now that you've landed your new analysis driver? |
Yes, I just verified that the original example now works. I was thinking of adding a test, but it's the exact same mechanism tested by the existing test of facts using the printf driver, so it doesn't seem necessary. |
For anyone who finds this issue: be aware that the new analysis driver is a big change to gopls' execution model (we're now doing analysis using an on-disk cache of export data and facts). There will be a temporary performance overhead due to double-type-checking, which we plan to mitigate before the next release. There also may be bugs. |
gopls version
v0.7.5 compiled with go1.18beta2
go version
go version go1.18beta2 darwin/amd64
What did you do?
gopls with fieldalignment analyzer enabled
"ui.diagnostic.analyses": { "fieldalignment": true }
What did you expect to see?
fieldalignment diagnostic message with correct struct size (40 according to
unsafe.Sizeof
).What did you see instead?
diagnostic message with incorrect size info
The text was updated successfully, but these errors were encountered: