Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/compile/internal/types2: use correct recv for parameterized embed…
…ded methods Methods of generic types are instantiated lazily (upon use). Thus, when we encounter a method of such a type, we need to instantiate the method signature with the receiver type arguments. We infer those type arguments from the method receiver. If the method is embedded, we must use the actual embedded receiver type, otherwise the receiver type declared with the method doesn't match up and inference will fail. (Note that there's no type inference in the source code here, it's only the implementation which uses the existing inference mechanism to easily identify the actual type arguments. If the implementation is correct, the inference will always succeed.) Updates #44688. Change-Id: Ie35b62bebaeaf42037f2ca00cf8bd34fec2ddd9c Reviewed-on: https://go-review.googlesource.com/c/go/+/298129 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
- Loading branch information
Showing
3 changed files
with
106 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// 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 P | ||
|
||
type A1[T any] struct{} | ||
|
||
func (*A1[T]) m1(T) {} | ||
|
||
type A2[T any] interface { | ||
m2(T) | ||
} | ||
|
||
type B1[T any] struct { | ||
filler int | ||
*A1[T] | ||
A2[T] | ||
} | ||
|
||
type B2[T any] interface { | ||
A2[T] | ||
} | ||
|
||
type C[T any] struct { | ||
filler1 int | ||
filler2 int | ||
B1[T] | ||
} | ||
|
||
type D[T any] struct { | ||
filler1 int | ||
filler2 int | ||
filler3 int | ||
C[T] | ||
} | ||
|
||
func _() { | ||
// calling embedded methods | ||
var b1 B1[string] | ||
|
||
b1.A1.m1("") | ||
b1.m1("") | ||
|
||
b1.A2.m2("") | ||
b1.m2("") | ||
|
||
var b2 B2[string] | ||
b2.m2("") | ||
|
||
// a deeper nesting | ||
var d D[string] | ||
d.m1("") | ||
d.m2("") | ||
|
||
// calling method expressions | ||
m1x := B1[string].m1 | ||
m1x(b1, "") | ||
m2x := B2[string].m2 | ||
m2x(b2, "") | ||
|
||
// calling method values | ||
m1v := b1.m1 | ||
m1v("") | ||
m2v := b1.m2 | ||
m2v("") | ||
b2v := b2.m2 | ||
b2v("") | ||
} | ||
|
||
// actual test case from issue | ||
|
||
type A[T any] struct{} | ||
|
||
func (*A[T]) f(T) {} | ||
|
||
type B[T any] struct{ A[T] } | ||
|
||
func _() { | ||
var b B[string] | ||
b.A.f("") | ||
b.f("") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters