Skip to content

Commit 6d8ba77

Browse files
committed
cmd/compile: fix importing of method expressions
For OMETHEXPR, the Name in the Selection needs to be properly linked up to the method declaration. Use the same code we already have for ODOTMETH and OCALLPART to do that. Fixes #45503 Change-Id: I7d6f886d606bae6faad8c104f50c177f871d41c8 Reviewed-on: https://go-review.googlesource.com/c/go/+/309831 Trust: Keith Randall <[email protected]> Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> Reviewed-by: Dan Scales <[email protected]>
1 parent e7ab1a5 commit 6d8ba77

File tree

5 files changed

+43
-19
lines changed

5 files changed

+43
-19
lines changed

src/cmd/compile/internal/typecheck/iexport.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -1591,17 +1591,10 @@ func (w *exportWriter) expr(n ir.Node) {
15911591
w.exoticSelector(n.Sel)
15921592
if go117ExportTypes {
15931593
w.exoticType(n.Type())
1594-
if n.Op() == ir.ODOT || n.Op() == ir.ODOTPTR || n.Op() == ir.ODOTINTER || n.Op() == ir.OMETHEXPR {
1594+
if n.Op() == ir.ODOT || n.Op() == ir.ODOTPTR || n.Op() == ir.ODOTINTER {
15951595
w.exoticParam(n.Selection)
1596-
if n.Op() == ir.OMETHEXPR {
1597-
name := ir.MethodExprName(n)
1598-
w.bool(name != nil)
1599-
if name != nil {
1600-
w.exoticType(name.Type())
1601-
}
1602-
}
16031596
}
1604-
// n.Selection is not required for ODOTMETH and OCALLPART. It will
1597+
// n.Selection is not required for OMETHEXPR, ODOTMETH, and OCALLPART. It will
16051598
// be reconstructed during import.
16061599
}
16071600

src/cmd/compile/internal/typecheck/iimport.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -1197,23 +1197,17 @@ func (r *importReader) node() ir.Node {
11971197
pos := r.pos()
11981198
expr := r.expr()
11991199
sel := r.exoticSelector()
1200-
n := ir.NewSelectorExpr(pos, ir.OXDOT, expr, sel)
1201-
n.SetOp(op)
1200+
n := ir.NewSelectorExpr(pos, op, expr, sel)
12021201
n.SetType(r.exoticType())
12031202
switch op {
1204-
case ir.ODOT, ir.ODOTPTR, ir.ODOTINTER, ir.OMETHEXPR:
1203+
case ir.ODOT, ir.ODOTPTR, ir.ODOTINTER:
12051204
n.Selection = r.exoticParam()
1206-
if op == ir.OMETHEXPR {
1207-
if r.bool() { // has name
1208-
ir.MethodExprName(n).SetType(r.exoticType())
1209-
}
1210-
}
1211-
case ir.ODOTMETH, ir.OCALLPART:
1205+
case ir.ODOTMETH, ir.OCALLPART, ir.OMETHEXPR:
12121206
// These require a Lookup to link to the correct declaration.
12131207
rcvrType := expr.Type()
12141208
typ := n.Type()
12151209
n.Selection = Lookdot(n, rcvrType, 1)
1216-
if op == ir.OCALLPART {
1210+
if op == ir.OCALLPART || op == ir.OMETHEXPR {
12171211
// Lookdot clobbers the opcode and type, undo that.
12181212
n.SetOp(op)
12191213
n.SetType(typ)

test/fixedbugs/issue45503.dir/a.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package a
6+
7+
type S struct{}
8+
9+
func (s *S) M() {
10+
s.m((*S).N)
11+
}
12+
13+
func (s *S) N() {}
14+
15+
func (s *S) m(func(*S)) {}

test/fixedbugs/issue45503.dir/b.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package b
6+
7+
import "a"
8+
9+
func F() {
10+
s := a.S{}
11+
s.M()
12+
}

test/fixedbugs/issue45503.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// compiledir
2+
3+
// Copyright 2021 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+
// This test exercises exporting + importing method
8+
// expressions for use when inlining.
9+
10+
package ignored

0 commit comments

Comments
 (0)