Skip to content

Commit f0b71b2

Browse files
committed
go/analysis/passes/fieldalignment: fix data race
This CL is a quick band-aid over a data race spotted in passing. A principled solution can wait for another day. Fortunately fieldalignment isn't much used now. Updates golang/go#43233 Change-Id: I5643294e3429c5c0b152aab046c2f78065c755be Reviewed-on: https://go-review.googlesource.com/c/tools/+/716603 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Robert Findley <rfindley@google.com>
1 parent 9222b43 commit f0b71b2

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

go/analysis/passes/fieldalignment/fieldalignment.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"golang.org/x/tools/go/analysis"
1919
"golang.org/x/tools/go/analysis/passes/inspect"
2020
"golang.org/x/tools/go/ast/inspector"
21+
"golang.org/x/tools/internal/astutil"
2122
)
2223

2324
const Doc = `find structs that would use less memory if their fields were sorted
@@ -103,6 +104,11 @@ func fieldalignment(pass *analysis.Pass, node *ast.StructType, typ *types.Struct
103104
return
104105
}
105106

107+
// Analyzers borrow syntax tree; they do not own them and must modify them.
108+
// This Clone operation is a quick fix to the data race introduced
109+
// in CL 278872 by the clearing of the Comment and Doc fields below.
110+
node = astutil.CloneNode(node)
111+
106112
// Flatten the ast node since it could have multiple field names per list item while
107113
// *types.Struct only have one item per field.
108114
// TODO: Preserve multi-named fields instead of flattening.

0 commit comments

Comments
 (0)