1
0
mirror of https://github.com/golang/go synced 2024-09-28 21:24:29 -06:00

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 <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
Matthew Dempsky 2022-08-06 12:02:03 -07:00
parent d42b520b03
commit d02d5fda65

View File

@ -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") }