Skip to content

Commit 16c2b36

Browse files
cuonglmcagedmantis
authored andcommitted
[release-branch.go1.19] cmd/compile: fix wrong typeparams for selector expr with embedded generic type
For selector expression "x.M" where "M" is a promoted method, irgen is using the type of receiver "x" for determining the typeparams for instantiation. However, because M is a promoted method, so its associated receiver is not "x", but "x.T" where "T" is the embedded field of "x". That casues a mismatch when converting non-shape types arguments. Fixing it by using the actual receiver which has the method, instead of using the base receiver. Fixes #54243 Change-Id: I1836fc422d734df14e9e6664d4bd014503960bfc Reviewed-on: https://go-review.googlesource.com/c/go/+/419294 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/423114 Reviewed-by: Carlos Amedee <[email protected]>
1 parent 3c200d6 commit 16c2b36

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/cmd/compile/internal/noder/stencil.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,6 @@ func (g *genInst) buildClosure(outer *ir.Func, x ir.Node) ir.Node {
334334
} else { // ir.OMETHEXPR or ir.METHVALUE
335335
// Method expression T.M where T is a generic type.
336336
se := x.(*ir.SelectorExpr)
337-
targs := deref(se.X.Type()).RParams()
338-
if len(targs) == 0 {
339-
panic("bad")
340-
}
341337
if x.Op() == ir.OMETHVALUE {
342338
rcvrValue = se.X
343339
}
@@ -348,7 +344,8 @@ func (g *genInst) buildClosure(outer *ir.Func, x ir.Node) ir.Node {
348344
// of se.Selection, since that will be the type that actually has
349345
// the method.
350346
recv := deref(se.Selection.Type.Recv().Type)
351-
if len(recv.RParams()) == 0 {
347+
targs := recv.RParams()
348+
if len(targs) == 0 {
352349
// The embedded type that actually has the method is not
353350
// actually generic, so no need to build a closure.
354351
return x

test/fixedbugs/issue53982.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// build
2+
3+
// Copyright 2022 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
type S[K, V any] struct {
10+
E[V]
11+
}
12+
13+
type E[K any] struct{}
14+
15+
func (e E[K]) M() E[K] {
16+
return e
17+
}
18+
19+
func G[K, V any](V) {
20+
_ = (*S[K, V]).M
21+
}
22+
23+
func main() {
24+
G[*int](new(int))
25+
}

0 commit comments

Comments
 (0)