mirror of
https://github.com/golang/go.git
synced 2025-10-24 05:23:20 +00:00
[dev.typeparams] cmd/compile: fix MethodExpr handling with embedded fields
The recent refactoring of SelectorExpr code to helpers broke the handling of MethodExprs when there is an embedded field involved (e.g. test/method7.go, line 48). If there is an embedded field involved, the node op seen in DotMethod() is an ODOT rather than an OTYPE. Also, the receiver type of the result should be the original type, but the new code was using the last type after following the embedding path. Change-Id: I13f7ea6448b03d3e8f974103ee3a027219ca8388 Reviewed-on: https://go-review.googlesource.com/c/go/+/286176 Run-TryBot: Dan Scales <danscales@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Dan Scales <danscales@google.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
parent
cecc1dfcba
commit
08a598f8c1
4 changed files with 91 additions and 54 deletions
|
|
@ -140,6 +140,7 @@ func (g *irgen) selectorExpr(pos src.XPos, expr *syntax.SelectorExpr) ir.Node {
|
|||
embeds, last := index[:len(index)-1], index[len(index)-1]
|
||||
|
||||
x := g.expr(expr.X)
|
||||
origx := x
|
||||
for _, ix := range embeds {
|
||||
x = Implicit(DotField(pos, x, ix))
|
||||
}
|
||||
|
|
@ -155,27 +156,37 @@ func (g *irgen) selectorExpr(pos src.XPos, expr *syntax.SelectorExpr) ir.Node {
|
|||
// unexported methods from two different packages (due to cross-package
|
||||
// interface embedding).
|
||||
|
||||
var n ir.Node
|
||||
method := selinfo.Obj().(*types2.Func)
|
||||
|
||||
// Add implicit addr/deref for method values, if needed.
|
||||
if kind == types2.MethodVal && !x.Type().IsInterface() {
|
||||
recvTyp := method.Type().(*types2.Signature).Recv().Type()
|
||||
_, wantPtr := recvTyp.(*types2.Pointer)
|
||||
havePtr := x.Type().IsPtr()
|
||||
if kind == types2.MethodExpr {
|
||||
// OMETHEXPR is unusual in using directly the node and type of the
|
||||
// original OTYPE node (origx) before passing through embedded
|
||||
// fields, even though the method is selected from the type
|
||||
// (x.Type()) reached after following the embedded fields. We will
|
||||
// actually drop any ODOT nodes we created due to the embedded
|
||||
// fields.
|
||||
n = MethodExpr(pos, origx, x.Type(), last)
|
||||
} else {
|
||||
// Add implicit addr/deref for method values, if needed.
|
||||
if !x.Type().IsInterface() {
|
||||
recvTyp := method.Type().(*types2.Signature).Recv().Type()
|
||||
_, wantPtr := recvTyp.(*types2.Pointer)
|
||||
havePtr := x.Type().IsPtr()
|
||||
|
||||
if havePtr != wantPtr {
|
||||
if havePtr {
|
||||
x = Implicit(Deref(pos, x))
|
||||
} else {
|
||||
x = Implicit(Addr(pos, x))
|
||||
if havePtr != wantPtr {
|
||||
if havePtr {
|
||||
x = Implicit(Deref(pos, x))
|
||||
} else {
|
||||
x = Implicit(Addr(pos, x))
|
||||
}
|
||||
}
|
||||
if !g.match(x.Type(), recvTyp, false) {
|
||||
base.FatalfAt(pos, "expected %L to have type %v", x, recvTyp)
|
||||
}
|
||||
}
|
||||
if !g.match(x.Type(), recvTyp, false) {
|
||||
base.FatalfAt(pos, "expected %L to have type %v", x, recvTyp)
|
||||
}
|
||||
n = DotMethod(pos, x, last)
|
||||
}
|
||||
|
||||
n := DotMethod(pos, x, last)
|
||||
if have, want := n.Sym(), g.selector(method); have != want {
|
||||
base.FatalfAt(pos, "bad Sym: have %v, want %v", have, want)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue