Skip to content

Commit 8287d5d

Browse files
ldezstamblerre
authored andcommitted
go/analysis/passes/sigchanyzer: copy to avoid modifying the AST
The sigchanyzer pass suggests fixes, but it does that by altering the channel declaration argument. That causes the buildssa.Analyzer fail to fail, along with crashing other passes that depend on it. To fix this, we make a copy of the channel's declaration arguments, and modify the copy instead. Fixes golang/go#46129 Change-Id: I807d36abd49cd3ccc2cc9f907aa98349b2e3231f Reviewed-on: https://go-review.googlesource.com/c/tools/+/319211 Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
1 parent 9dfac01 commit 8287d5d

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

go/analysis/passes/sigchanyzer/sigchanyzer.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,19 @@ func run(pass *analysis.Pass) (interface{}, error) {
5959
if chanDecl == nil || len(chanDecl.Args) != 1 {
6060
return
6161
}
62-
chanDecl.Args = append(chanDecl.Args, &ast.BasicLit{
62+
63+
// Make a copy of the channel's declaration to avoid
64+
// mutating the AST. See https://golang.org/issue/46129.
65+
chanDeclCopy := &ast.CallExpr{}
66+
*chanDeclCopy = *chanDecl
67+
chanDeclCopy.Args = append([]ast.Expr(nil), chanDecl.Args...)
68+
chanDeclCopy.Args = append(chanDeclCopy.Args, &ast.BasicLit{
6369
Kind: token.INT,
6470
Value: "1",
6571
})
72+
6673
var buf bytes.Buffer
67-
if err := format.Node(&buf, token.NewFileSet(), chanDecl); err != nil {
74+
if err := format.Node(&buf, token.NewFileSet(), chanDeclCopy); err != nil {
6875
return
6976
}
7077
pass.Report(analysis.Diagnostic{

0 commit comments

Comments
 (0)