]> Cypherpunks repositories - gostls13.git/commitdiff
test: add test case for type parameter method indexing
authorMatthew Dempsky <mdempsky@google.com>
Sat, 6 Aug 2022 19:02:03 +0000 (12:02 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Mon, 8 Aug 2022 17:39:25 +0000 (17:39 +0000)
When types2 type checks a method expression or method value that
selects a type parameter method, the Selection.Index is indexed based
on the method's index within the type parameter's constraint
interface.

However, with a fully-stenciled implementation, naively using the
index would result in picking a method from the corresponding type
argument's full method set, which could select a different method.

Unified IR currently avoids this because it selects methods based on
name, not index; but experimenting with index-based selection revealed
that there are no test cases that would have caught this failure case.

Change-Id: Idbc39e1ee741714203d4749e47f5bc015af25020
Reviewed-on: https://go-review.googlesource.com/c/go/+/421815
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
test/typeparam/mdempsky/19.go [new file with mode: 0644]

diff --git a/test/typeparam/mdempsky/19.go b/test/typeparam/mdempsky/19.go
new file mode 100644 (file)
index 0000000..53d979a
--- /dev/null
@@ -0,0 +1,32 @@
+// run
+
+// Copyright 2022 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.
+
+// Test that type parameter methods are handled correctly, even when
+// the instantiating type argument has additional methods.
+
+package main
+
+func main() {
+       F(X(0))
+}
+
+type I interface{ B() }
+
+func F[T I](t T) {
+       CallMethod(t)
+       MethodExpr[T]()(t)
+       MethodVal(t)()
+}
+
+func CallMethod[T I](t T)       { t.B() }
+func MethodExpr[T I]() func(T)  { return T.B }
+func MethodVal[T I](t T) func() { return t.B }
+
+type X int
+
+func (X) A() { panic("FAIL") }
+func (X) B() {}
+func (X) C() { panic("FAIL") }