Skip to content

Commit 950a568

Browse files
committed
cmd/compile: fix method expressions with anonymous receivers
Method expressions with anonymous receiver types like "struct { T }.m" require wrapper functions, which we weren't always creating. This in turn resulted in linker errors. This CL ensures that we generate wrapper functions for any anonymous receiver types used in a method expression. Fixes #22444. Change-Id: Ia8ac27f238c2898965e57b82a91d959792d2ddd4 Reviewed-on: https://go-review.googlesource.com/105044 Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 84b784a commit 950a568

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/cmd/compile/internal/gc/reflect.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,7 @@ func itabsym(it *obj.LSym, offset int64) *obj.LSym {
14741474
return syms[methodnum]
14751475
}
14761476

1477+
// addsignat ensures that a runtime type descriptor is emitted for t.
14771478
func addsignat(t *types.Type) {
14781479
signatset[t] = struct{}{}
14791480
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,6 +2378,16 @@ func looktypedot(n *Node, t *types.Type, dostrcmp int) bool {
23782378
return false
23792379
}
23802380

2381+
// The method expression T.m requires a wrapper when T is
2382+
// different from m's declared receiver type. We normally
2383+
// generate these wrappers while writing out runtime type
2384+
// descriptors, which is always done for types declared at
2385+
// package scope. However, we need to make sure to generate
2386+
// wrappers for anonymous receiver types too.
2387+
if mt.Sym == nil {
2388+
addsignat(t)
2389+
}
2390+
23812391
n.Sym = methodSym(t, n.Sym)
23822392
n.Xoffset = f2.Offset
23832393
n.Type = f2.Type

test/method7.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ func main() {
4545
interface{ m1(string) }.m1(x, "d")
4646
want += " m1(d)"
4747

48-
// cannot link the call below - see #22444
49-
// g := struct{ T }.m2
50-
// g(struct{T}{})
51-
// want += " m2()"
48+
g := struct{ T }.m2
49+
g(struct{ T }{})
50+
want += " m2()"
5251

5352
if got != want {
5453
panic("got" + got + ", want" + want)

0 commit comments

Comments
 (0)