diff options
author | Cuong Manh Le <cuong.manhle.vn@gmail.com> | 2022-07-23 22:31:35 +0700 |
---|---|---|
committer | Carlos Amedee <carlos@golang.org> | 2022-08-17 15:12:12 +0000 |
commit | 16c2b3615ae7b565947ff7d0cff06d4809236c2e (patch) | |
tree | fd6d9211a350488707b7e98e690186af7be20c05 | |
parent | 3c200d6c81e75348e583297b1295f8e34b07b61c (diff) | |
download | go-16c2b3615ae7b565947ff7d0cff06d4809236c2e.tar.gz go-16c2b3615ae7b565947ff7d0cff06d4809236c2e.zip |
[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 <gobot@golang.org>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/423114
Reviewed-by: Carlos Amedee <carlos@golang.org>
-rw-r--r-- | src/cmd/compile/internal/noder/stencil.go | 7 | ||||
-rw-r--r-- | test/fixedbugs/issue53982.go | 25 |
2 files changed, 27 insertions, 5 deletions
diff --git a/src/cmd/compile/internal/noder/stencil.go b/src/cmd/compile/internal/noder/stencil.go index d3f51e00cd..6fcb31b472 100644 --- a/src/cmd/compile/internal/noder/stencil.go +++ b/src/cmd/compile/internal/noder/stencil.go @@ -334,10 +334,6 @@ func (g *genInst) buildClosure(outer *ir.Func, x ir.Node) ir.Node { } else { // ir.OMETHEXPR or ir.METHVALUE // Method expression T.M where T is a generic type. se := x.(*ir.SelectorExpr) - targs := deref(se.X.Type()).RParams() - if len(targs) == 0 { - panic("bad") - } if x.Op() == ir.OMETHVALUE { rcvrValue = se.X } @@ -348,7 +344,8 @@ func (g *genInst) buildClosure(outer *ir.Func, x ir.Node) ir.Node { // of se.Selection, since that will be the type that actually has // the method. recv := deref(se.Selection.Type.Recv().Type) - if len(recv.RParams()) == 0 { + targs := recv.RParams() + if len(targs) == 0 { // The embedded type that actually has the method is not // actually generic, so no need to build a closure. return x diff --git a/test/fixedbugs/issue53982.go b/test/fixedbugs/issue53982.go new file mode 100644 index 0000000000..512b1af2bc --- /dev/null +++ b/test/fixedbugs/issue53982.go @@ -0,0 +1,25 @@ +// build + +// 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. + +package main + +type S[K, V any] struct { + E[V] +} + +type E[K any] struct{} + +func (e E[K]) M() E[K] { + return e +} + +func G[K, V any](V) { + _ = (*S[K, V]).M +} + +func main() { + G[*int](new(int)) +} |