Skip to content

Commit 1028e30

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/settings: remove fieldalignment analyzer
Since it doesn't find likely mistakes, it is a poor fit for the gopls analyzer suite: even when off-by-default, its diagnostics can be confusing. Instead, its docs now advise users who come across it to run it using a standalone singlechecker as desired. + release note Also, we issue a deprecated warning if the user's configuration enables the deleted analyzer, with a reference to the 0.17 release so that users can find the release note. Fixes golang/go#67762 Change-Id: I7e2eafc3216df84eb62de132ac2f04e0bf444f92 Reviewed-on: https://go-review.googlesource.com/c/tools/+/590375 Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 5fefc65 commit 1028e30

File tree

6 files changed

+25
-50
lines changed

6 files changed

+25
-50
lines changed

go/analysis/passes/fieldalignment/fieldalignment.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ Be aware that the most compact order is not always the most efficient.
4646
In rare cases it may cause two variables each updated by its own goroutine
4747
to occupy the same CPU cache line, inducing a form of memory contention
4848
known as "false sharing" that slows down both goroutines.
49+
50+
Unlike most analyzers, which report likely mistakes, the diagnostics
51+
produced by fieldanalyzer very rarely indicate a significant problem,
52+
so the analyzer is not included in typical suites such as vet or
53+
gopls. Use this standalone command to run it on your code:
54+
55+
$ go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest
56+
$ go fieldalignment [packages]
57+
4958
`
5059

5160
var Analyzer = &analysis.Analyzer{

gopls/doc/analyzers.md

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -257,40 +257,6 @@ Default: on.
257257

258258
Package documentation: [errorsas](https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/errorsas)
259259

260-
<a id='fieldalignment'></a>
261-
## `fieldalignment`: find structs that would use less memory if their fields were sorted
262-
263-
264-
This analyzer find structs that can be rearranged to use less memory, and provides
265-
a suggested edit with the most compact order.
266-
267-
Note that there are two different diagnostics reported. One checks struct size,
268-
and the other reports "pointer bytes" used. Pointer bytes is how many bytes of the
269-
object that the garbage collector has to potentially scan for pointers, for example:
270-
271-
struct { uint32; string }
272-
273-
have 16 pointer bytes because the garbage collector has to scan up through the string's
274-
inner pointer.
275-
276-
struct { string; *uint32 }
277-
278-
has 24 pointer bytes because it has to scan further through the *uint32.
279-
280-
struct { string; uint32 }
281-
282-
has 8 because it can stop immediately after the string pointer.
283-
284-
Be aware that the most compact order is not always the most efficient.
285-
In rare cases it may cause two variables each updated by its own goroutine
286-
to occupy the same CPU cache line, inducing a form of memory contention
287-
known as "false sharing" that slows down both goroutines.
288-
289-
290-
Default: off. Enable by setting `"analyses": {"fieldalignment": true}`.
291-
292-
Package documentation: [fieldalignment](https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/fieldalignment)
293-
294260
<a id='fillreturns'></a>
295261
## `fillreturns`: suggest fixes for errors due to an incorrect number of return values
296262

gopls/doc/release/v0.17.0.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
# Configuration Changes
4+
5+
The `fieldalignment` analyzer, previously disabled by default, has
6+
been removed: it is redundant with the hover size/offset information
7+
displayed by v0.16.0 and its diagnostics were confusing.

gopls/internal/doc/api.json

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -447,11 +447,6 @@
447447
"Doc": "report passing non-pointer or non-error values to errors.As\n\nThe errorsas analysis reports calls to errors.As where the type\nof the second argument is not a pointer to a type implementing error.",
448448
"Default": "true"
449449
},
450-
{
451-
"Name": "\"fieldalignment\"",
452-
"Doc": "find structs that would use less memory if their fields were sorted\n\nThis analyzer find structs that can be rearranged to use less memory, and provides\na suggested edit with the most compact order.\n\nNote that there are two different diagnostics reported. One checks struct size,\nand the other reports \"pointer bytes\" used. Pointer bytes is how many bytes of the\nobject that the garbage collector has to potentially scan for pointers, for example:\n\n\tstruct { uint32; string }\n\nhave 16 pointer bytes because the garbage collector has to scan up through the string's\ninner pointer.\n\n\tstruct { string; *uint32 }\n\nhas 24 pointer bytes because it has to scan further through the *uint32.\n\n\tstruct { string; uint32 }\n\nhas 8 because it can stop immediately after the string pointer.\n\nBe aware that the most compact order is not always the most efficient.\nIn rare cases it may cause two variables each updated by its own goroutine\nto occupy the same CPU cache line, inducing a form of memory contention\nknown as \"false sharing\" that slows down both goroutines.\n",
453-
"Default": "false"
454-
},
455450
{
456451
"Name": "\"fillreturns\"",
457452
"Doc": "suggest fixes for errors due to an incorrect number of return values\n\nThis checker provides suggested fixes for type errors of the\ntype \"wrong number of return values (want %d, got %d)\". For example:\n\n\tfunc m() (int, string, *bool, error) {\n\t\treturn\n\t}\n\nwill turn into\n\n\tfunc m() (int, string, *bool, error) {\n\t\treturn 0, \"\", nil, nil\n\t}\n\nThis functionality is similar to https://github.com/sqs/goreturns.",
@@ -1360,12 +1355,6 @@
13601355
"URL": "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/errorsas",
13611356
"Default": true
13621357
},
1363-
{
1364-
"Name": "fieldalignment",
1365-
"Doc": "find structs that would use less memory if their fields were sorted\n\nThis analyzer find structs that can be rearranged to use less memory, and provides\na suggested edit with the most compact order.\n\nNote that there are two different diagnostics reported. One checks struct size,\nand the other reports \"pointer bytes\" used. Pointer bytes is how many bytes of the\nobject that the garbage collector has to potentially scan for pointers, for example:\n\n\tstruct { uint32; string }\n\nhave 16 pointer bytes because the garbage collector has to scan up through the string's\ninner pointer.\n\n\tstruct { string; *uint32 }\n\nhas 24 pointer bytes because it has to scan further through the *uint32.\n\n\tstruct { string; uint32 }\n\nhas 8 because it can stop immediately after the string pointer.\n\nBe aware that the most compact order is not always the most efficient.\nIn rare cases it may cause two variables each updated by its own goroutine\nto occupy the same CPU cache line, inducing a form of memory contention\nknown as \"false sharing\" that slows down both goroutines.\n",
1366-
"URL": "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/fieldalignment",
1367-
"Default": false
1368-
},
13691358
{
13701359
"Name": "fillreturns",
13711360
"Doc": "suggest fixes for errors due to an incorrect number of return values\n\nThis checker provides suggested fixes for type errors of the\ntype \"wrong number of return values (want %d, got %d)\". For example:\n\n\tfunc m() (int, string, *bool, error) {\n\t\treturn\n\t}\n\nwill turn into\n\n\tfunc m() (int, string, *bool, error) {\n\t\treturn 0, \"\", nil, nil\n\t}\n\nThis functionality is similar to https://github.com/sqs/goreturns.",

gopls/internal/settings/analysis.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"golang.org/x/tools/go/analysis/passes/defers"
2121
"golang.org/x/tools/go/analysis/passes/directive"
2222
"golang.org/x/tools/go/analysis/passes/errorsas"
23-
"golang.org/x/tools/go/analysis/passes/fieldalignment"
2423
"golang.org/x/tools/go/analysis/passes/framepointer"
2524
"golang.org/x/tools/go/analysis/passes/httpresponse"
2625
"golang.org/x/tools/go/analysis/passes/ifaceassert"
@@ -184,9 +183,9 @@ func init() {
184183
{analyzer: embeddirective.Analyzer, enabled: true},
185184

186185
// disabled due to high false positives
187-
{analyzer: fieldalignment.Analyzer, enabled: false}, // never a bug
188-
{analyzer: shadow.Analyzer, enabled: false}, // very noisy
189-
{analyzer: useany.Analyzer, enabled: false}, // never a bug
186+
{analyzer: shadow.Analyzer, enabled: false}, // very noisy
187+
{analyzer: useany.Analyzer, enabled: false}, // never a bug
188+
// fieldalignment is not even off-by-default; see #67762.
190189

191190
// "simplifiers": analyzers that offer mere style fixes
192191
// gofmt -s suite:

gopls/internal/settings/settings.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,12 @@ func (o *Options) set(name string, value any, seen map[string]struct{}) error {
10101010
DefinitionShortcut)
10111011

10121012
case "analyses":
1013-
return setBoolMap(&o.Analyses, value)
1013+
if err := setBoolMap(&o.Analyses, value); err != nil {
1014+
return err
1015+
}
1016+
if o.Analyses["fieldalignment"] {
1017+
return deprecatedError("the 'fieldalignment' analyzer was removed in gopls/v0.17.0; instead, hover over struct fields to see size/offset information (https://go.dev/issue/66861)")
1018+
}
10141019

10151020
case "hints":
10161021
return setBoolMap(&o.Hints, value)

0 commit comments

Comments
 (0)