go/analysis/passes/cgocall: add typeparams test

This CL adds a test for the cgocall pass that involves use of generics.

Updates golang/go#48704

Change-Id: I521708d607c5f32ca24fe370b7d6436147bae6a5
Reviewed-on: https://go-review.googlesource.com/c/tools/+/358695
Run-TryBot: Zvonimir Pavlinovic <zpavlinovic@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Trust: Zvonimir Pavlinovic <zpavlinovic@google.com>
This commit is contained in:
Zvonimir Pavlinovic 2021-10-25 17:19:38 -07:00 committed by Emmanuel Odeke
parent 26dbf47d51
commit 591e12a7fb
2 changed files with 44 additions and 1 deletions

View File

@ -9,9 +9,15 @@ import (
"golang.org/x/tools/go/analysis/analysistest"
"golang.org/x/tools/go/analysis/passes/cgocall"
"golang.org/x/tools/internal/typeparams"
)
func Test(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, cgocall.Analyzer, "a", "b", "c")
tests := []string{"a", "b", "c"}
if typeparams.Enabled {
// and testdata/src/typeparams/typeparams.go when possible
tests = append(tests, "typeparams")
}
analysistest.Run(t, testdata, cgocall.Analyzer, tests...)
}

View File

@ -0,0 +1,37 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file contains tests for the cgo checker.
package a
// void f(void *ptr) {}
import "C"
import "unsafe"
func CgoTest[T any]() {
var c chan bool
C.f(*(*unsafe.Pointer)(unsafe.Pointer(&c))) // want "embedded pointer"
C.f(unsafe.Pointer(&c)) // want "embedded pointer"
var schan S[chan bool]
C.f(*(*unsafe.Pointer)(unsafe.Pointer(&schan))) // want "embedded pointer"
C.f(unsafe.Pointer(&schan)) // want "embedded pointer"
var x T
C.f(*(*unsafe.Pointer)(unsafe.Pointer(&x))) // no findings as T is not known compile-time
C.f(unsafe.Pointer(&x))
// instantiating CgoTest should not yield any warnings
CgoTest[chan bool]()
var sint S[int]
C.f(*(*unsafe.Pointer)(unsafe.Pointer(&sint)))
C.f(unsafe.Pointer(&sint))
}
type S[X any] struct {
val X
}