Skip to content

Commit 591e12a

Browse files
zpavlinovicodeke-em
authored andcommitted
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 <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Emmanuel Odeke <[email protected]> Reviewed-by: Robert Findley <[email protected]> Trust: Zvonimir Pavlinovic <[email protected]>
1 parent 26dbf47 commit 591e12a

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

go/analysis/passes/cgocall/cgocall_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ import (
99

1010
"golang.org/x/tools/go/analysis/analysistest"
1111
"golang.org/x/tools/go/analysis/passes/cgocall"
12+
"golang.org/x/tools/internal/typeparams"
1213
)
1314

1415
func Test(t *testing.T) {
1516
testdata := analysistest.TestData()
16-
analysistest.Run(t, testdata, cgocall.Analyzer, "a", "b", "c")
17+
tests := []string{"a", "b", "c"}
18+
if typeparams.Enabled {
19+
// and testdata/src/typeparams/typeparams.go when possible
20+
tests = append(tests, "typeparams")
21+
}
22+
analysistest.Run(t, testdata, cgocall.Analyzer, tests...)
1723
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// This file contains tests for the cgo checker.
6+
7+
package a
8+
9+
// void f(void *ptr) {}
10+
import "C"
11+
12+
import "unsafe"
13+
14+
func CgoTest[T any]() {
15+
var c chan bool
16+
C.f(*(*unsafe.Pointer)(unsafe.Pointer(&c))) // want "embedded pointer"
17+
C.f(unsafe.Pointer(&c)) // want "embedded pointer"
18+
19+
var schan S[chan bool]
20+
C.f(*(*unsafe.Pointer)(unsafe.Pointer(&schan))) // want "embedded pointer"
21+
C.f(unsafe.Pointer(&schan)) // want "embedded pointer"
22+
23+
var x T
24+
C.f(*(*unsafe.Pointer)(unsafe.Pointer(&x))) // no findings as T is not known compile-time
25+
C.f(unsafe.Pointer(&x))
26+
27+
// instantiating CgoTest should not yield any warnings
28+
CgoTest[chan bool]()
29+
30+
var sint S[int]
31+
C.f(*(*unsafe.Pointer)(unsafe.Pointer(&sint)))
32+
C.f(unsafe.Pointer(&sint))
33+
}
34+
35+
type S[X any] struct {
36+
val X
37+
}

0 commit comments

Comments
 (0)