-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There was a bug where the TypeParams weren't being properly copied but rather continuing the use the source value. The code: ``` cp := *x if typeParams := typeparams.ForFuncType(x); typeParams != nil { *typeparams.ForFuncType(&cp) = *FieldList(typeParams) } ``` cp.TypeParams which typeparams.ForFyncType returns is still x.TypeParams. Then the `*typeparams.ForFuncType(&cp)` overwrites it a copy thus modifying the original slice. This issue causes a data race which showed up in golangci-lint and often caused a panic. The only way around this I found was to have copies of the function for different versions of Go since the typeparam package doesn't have a way to set the TypeParams field.
- Loading branch information
Showing
3 changed files
with
66 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//go:build !go1.18 | ||
// +build !go1.18 | ||
|
||
package astcopy | ||
|
||
// FuncType returns x deep copy. | ||
// Copy of nil argument is nil. | ||
func FuncType(x *ast.FuncType) *ast.FuncType { | ||
if x == nil { | ||
return nil | ||
} | ||
cp := *x | ||
cp.Params = FieldList(x.Params) | ||
cp.Results = FieldList(x.Results) | ||
return &cp | ||
} | ||
|
||
// TypeSpec returns x deep copy. | ||
// Copy of nil argument is nil. | ||
func TypeSpec(x *ast.TypeSpec) *ast.TypeSpec { | ||
if x == nil { | ||
return nil | ||
} | ||
cp := *x | ||
cp.Name = Ident(x.Name) | ||
cp.Type = copyExpr(x.Type) | ||
cp.Doc = CommentGroup(x.Doc) | ||
cp.Comment = CommentGroup(x.Comment) | ||
return &cp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
package astcopy | ||
|
||
import ( | ||
"go/ast" | ||
) | ||
|
||
// FuncType returns x deep copy. | ||
// Copy of nil argument is nil. | ||
func FuncType(x *ast.FuncType) *ast.FuncType { | ||
if x == nil { | ||
return nil | ||
} | ||
cp := *x | ||
cp.Params = FieldList(x.Params) | ||
cp.Results = FieldList(x.Results) | ||
cp.TypeParams = FieldList(x.TypeParams) | ||
return &cp | ||
} | ||
|
||
// TypeSpec returns x deep copy. | ||
// Copy of nil argument is nil. | ||
func TypeSpec(x *ast.TypeSpec) *ast.TypeSpec { | ||
if x == nil { | ||
return nil | ||
} | ||
cp := *x | ||
cp.Name = Ident(x.Name) | ||
cp.Type = copyExpr(x.Type) | ||
cp.Doc = CommentGroup(x.Doc) | ||
cp.Comment = CommentGroup(x.Comment) | ||
cp.TypeParams = FieldList(x.TypeParams) | ||
return &cp | ||
} |