From d02d5fda653f9e7ca2bb6036ccddc61a869abad5 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Sat, 6 Aug 2022 12:02:03 -0700 Subject: [PATCH] test: add test case for type parameter method indexing 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 TryBot-Result: Gopher Robot Reviewed-by: David Chase --- test/typeparam/mdempsky/19.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 test/typeparam/mdempsky/19.go diff --git a/test/typeparam/mdempsky/19.go b/test/typeparam/mdempsky/19.go new file mode 100644 index 0000000000..53d979a1f2 --- /dev/null +++ b/test/typeparam/mdempsky/19.go @@ -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") } -- 2.50.0