Skip to content

Commit c7543e5

Browse files
committed
go/types: fixes for pointer receivers of instantiated methods
Backported changes from CL 349998 that were not already in go/types. Change-Id: I0341f76c080b4e73567b3e917a4cbbe2e82d3703 Reviewed-on: https://go-review.googlesource.com/c/go/+/351149 Trust: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 986f8ea commit c7543e5

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/go/types/instantiate_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ var X T[int]
8686
}{
8787
{"func (r T[P]) m() P", "func (T[int]).m() int"},
8888
{"func (r T[P]) m(P)", "func (T[int]).m(int)"},
89-
{"func (r T[P]) m() func() P", "func (T[int]).m() func() int"},
89+
{"func (r *T[P]) m(P)", "func (*T[int]).m(int)"},
9090
{"func (r T[P]) m() T[P]", "func (T[int]).m() T[int]"},
9191
{"func (r T[P]) m(T[P])", "func (T[int]).m(T[int])"},
9292
{"func (r T[P]) m(T[P], P, string)", "func (T[int]).m(T[int], int, string)"},
@@ -99,7 +99,7 @@ var X T[int]
9999
if err != nil {
100100
t.Fatal(err)
101101
}
102-
typ := pkg.Scope().Lookup("X").Type().(*Named)
102+
typ := NewPointer(pkg.Scope().Lookup("X").Type())
103103
obj, _, _ := LookupFieldOrMethod(typ, false, pkg, "m")
104104
m, _ := obj.(*Func)
105105
if m == nil {

src/go/types/named.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func expandNamed(env *Environment, n *Named, instPos token.Pos) (tparams *TypePa
257257
// During type checking origm may not have a fully set up type, so defer
258258
// instantiation of its signature until later.
259259
m := NewFunc(origm.pos, origm.pkg, origm.name, nil)
260-
m.hasPtrRecv = origm.hasPtrRecv
260+
m.hasPtrRecv = ptrRecv(origm)
261261
// Setting instRecv here allows us to complete later (we need the
262262
// instRecv to get targs and the original method).
263263
m.instRecv = n
@@ -289,32 +289,38 @@ func expandNamed(env *Environment, n *Named, instPos token.Pos) (tparams *TypePa
289289

290290
func (check *Checker) completeMethod(env *Environment, m *Func) {
291291
assert(m.instRecv != nil)
292-
rtyp := m.instRecv
292+
rbase := m.instRecv
293293
m.instRecv = nil
294294
m.setColor(black)
295295

296-
assert(rtyp.TypeArgs().Len() > 0)
296+
assert(rbase.TypeArgs().Len() > 0)
297297

298298
// Look up the original method.
299-
_, orig := lookupMethod(rtyp.orig.methods, rtyp.obj.pkg, m.name)
299+
_, orig := lookupMethod(rbase.orig.methods, rbase.obj.pkg, m.name)
300300
assert(orig != nil)
301301
if check != nil {
302302
check.objDecl(orig, nil)
303303
}
304304
origSig := orig.typ.(*Signature)
305-
if origSig.RecvTypeParams().Len() != rtyp.targs.Len() {
305+
if origSig.RecvTypeParams().Len() != rbase.targs.Len() {
306306
m.typ = origSig // or new(Signature), but we can't use Typ[Invalid]: Funcs must have Signature type
307307
return // error reported elsewhere
308308
}
309309

310-
smap := makeSubstMap(origSig.RecvTypeParams().list(), rtyp.targs.list())
310+
smap := makeSubstMap(origSig.RecvTypeParams().list(), rbase.targs.list())
311311
sig := check.subst(orig.pos, origSig, smap, env).(*Signature)
312312
if sig == origSig {
313-
// No substitution occurred, but we still need to create a copy to hold the
314-
// instantiated receiver.
313+
// No substitution occurred, but we still need to create a new signature to
314+
// hold the instantiated receiver.
315315
copy := *origSig
316316
sig = &copy
317317
}
318+
var rtyp Type
319+
if ptrRecv(m) {
320+
rtyp = NewPointer(rbase)
321+
} else {
322+
rtyp = rbase
323+
}
318324
sig.recv = NewParam(origSig.recv.pos, origSig.recv.pkg, origSig.recv.name, rtyp)
319325

320326
m.typ = sig

0 commit comments

Comments
 (0)