go/analysis/passes/ifaceassert: add a typeparams test
For golang/go#48704
Change-Id: I94cc071e6e38f5b60c7b62c41cb65120bdc9720a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/358619
Trust: Robert Findley <rfindley@google.com>
Trust: Zvonimir Pavlinovic <zpavlinovic@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com>
diff --git a/go/analysis/passes/ifaceassert/ifaceassert_test.go b/go/analysis/passes/ifaceassert/ifaceassert_test.go
index 4607338..b07c276 100644
--- a/go/analysis/passes/ifaceassert/ifaceassert_test.go
+++ b/go/analysis/passes/ifaceassert/ifaceassert_test.go
@@ -9,9 +9,14 @@
"golang.org/x/tools/go/analysis/analysistest"
"golang.org/x/tools/go/analysis/passes/ifaceassert"
+ "golang.org/x/tools/internal/typeparams"
)
func Test(t *testing.T) {
testdata := analysistest.TestData()
- analysistest.Run(t, testdata, ifaceassert.Analyzer, "a")
+ pkgs := []string{"a"}
+ if typeparams.Enabled {
+ pkgs = append(pkgs, "typeparams")
+ }
+ analysistest.Run(t, testdata, ifaceassert.Analyzer, pkgs...)
}
diff --git a/go/analysis/passes/ifaceassert/testdata/src/typeparams/typeparams.go b/go/analysis/passes/ifaceassert/testdata/src/typeparams/typeparams.go
new file mode 100644
index 0000000..dd0c9b2
--- /dev/null
+++ b/go/analysis/passes/ifaceassert/testdata/src/typeparams/typeparams.go
@@ -0,0 +1,36 @@
+// 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.
+
+package typeparams
+
+import "io"
+
+type SourceReader[Source any] interface {
+ Read(p Source) (n int, err error)
+}
+
+func GenericInterfaceAssertionTest[T io.Reader]() {
+ var (
+ a SourceReader[[]byte]
+ b SourceReader[[]int]
+ r io.Reader
+ )
+ _ = a.(io.Reader)
+ _ = b.(io.Reader) // want `^impossible type assertion: no type can implement both typeparams.SourceReader\[\[\]int\] and io.Reader \(conflicting types for Read method\)$`
+
+ _ = r.(SourceReader[[]byte])
+ _ = r.(SourceReader[[]int]) // want `^impossible type assertion: no type can implement both io.Reader and typeparams.SourceReader\[\[\]int\] \(conflicting types for Read method\)$`
+ _ = r.(T) // not actually an iface assertion, so checked by the type checker.
+
+ switch a.(type) {
+ case io.Reader:
+ default:
+ }
+
+ switch b.(type) {
+ case io.Reader: // want `^impossible type assertion: no type can implement both typeparams.SourceReader\[\[\]int\] and io.Reader \(conflicting types for Read method\)$`
+
+ default:
+ }
+}