Skip to content

Commit

Permalink
nilness: add unit test for generic instantiation.
Browse files Browse the repository at this point in the history
Adds a unit test for generic instantiation to nilness.
Currently the expected behavior is to ignore the instantiation
as it is not added to builssa's SrcFuncs result.

Updates golang/go#52463
Updates golang/go#48525

Change-Id: I7b214aae88c8aa26605abb5019591178f76a7cbb
Reviewed-on: https://go-review.googlesource.com/c/tools/+/402054
Run-TryBot: Tim King <taking@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Tim King <taking@google.com>
Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
timothy-king authored and gopherbot committed Apr 25, 2022
1 parent 2548a8b commit 825b661
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions go/analysis/passes/nilness/nilness.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ var Analyzer = &analysis.Analyzer{

func run(pass *analysis.Pass) (interface{}, error) {
ssainput := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA)
// TODO(48525): ssainput.SrcFuncs is missing fn._Instances(). runFunc will be skipped.
for _, fn := range ssainput.SrcFuncs {
runFunc(pass, fn)
}
Expand Down
9 changes: 9 additions & 0 deletions go/analysis/passes/nilness/nilness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ import (

"golang.org/x/tools/go/analysis/analysistest"
"golang.org/x/tools/go/analysis/passes/nilness"
"golang.org/x/tools/internal/typeparams"
)

func Test(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, nilness.Analyzer, "a")
}

func TestInstantiated(t *testing.T) {
if !typeparams.Enabled {
t.Skip("TestInstantiated requires type parameters")
}
testdata := analysistest.TestData()
analysistest.Run(t, testdata, nilness.Analyzer, "c")
}
14 changes: 14 additions & 0 deletions go/analysis/passes/nilness/testdata/src/c/c.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package c

func instantiated[X any](x *X) int {
if x == nil {
print(*x) // not reported until _Instances are added to SrcFuncs
}
return 1
}

var g int

func init() {
g = instantiated[int](&g)
}

0 comments on commit 825b661

Please sign in to comment.