Skip to content

Commit

Permalink
go/analysis/passes/sigchanyzer: copy to avoid modifying the AST
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Cuong Manh Le <[email protected]>
Run-TryBot: Emmanuel Odeke <[email protected]>
gopls-CI: kokoro <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Trust: Cuong Manh Le <[email protected]>
  • Loading branch information
ldez authored and stamblerre committed May 12, 2021
1 parent 9dfac01 commit 8287d5d
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions go/analysis/passes/sigchanyzer/sigchanyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,19 @@ func run(pass *analysis.Pass) (interface{}, error) {
if chanDecl == nil || len(chanDecl.Args) != 1 {
return
}
chanDecl.Args = append(chanDecl.Args, &ast.BasicLit{

// Make a copy of the channel's declaration to avoid
// mutating the AST. See https://golang.org/issue/46129.
chanDeclCopy := &ast.CallExpr{}
*chanDeclCopy = *chanDecl
chanDeclCopy.Args = append([]ast.Expr(nil), chanDecl.Args...)
chanDeclCopy.Args = append(chanDeclCopy.Args, &ast.BasicLit{
Kind: token.INT,
Value: "1",
})

var buf bytes.Buffer
if err := format.Node(&buf, token.NewFileSet(), chanDecl); err != nil {
if err := format.Node(&buf, token.NewFileSet(), chanDeclCopy); err != nil {
return
}
pass.Report(analysis.Diagnostic{
Expand Down

0 comments on commit 8287d5d

Please sign in to comment.