Skip to content

Commit fc3b24a

Browse files
committed
go/internal/gcimporter: rewrite interface receiver parameters
Tracking changes in go repo for unified IR. See CL 421355. Change-Id: Idc0d2afeb6f2241f3608cbdb0fbc128f8755ec55 Reviewed-on: https://go-review.googlesource.com/c/tools/+/421255 Run-TryBot: David Chase <[email protected]> TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent b5fd088 commit fc3b24a

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

go/internal/gcimporter/gcimporter_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,14 +372,6 @@ func verifyInterfaceMethodRecvs(t *testing.T, named *types.Named, level int) {
372372
return // not an interface
373373
}
374374

375-
// The unified IR importer always sets interface method receiver
376-
// parameters to point to the Interface type, rather than the Named.
377-
// See #49906.
378-
var want types.Type = named
379-
if unifiedIR {
380-
want = iface
381-
}
382-
383375
// check explicitly declared methods
384376
for i := 0; i < iface.NumExplicitMethods(); i++ {
385377
m := iface.ExplicitMethod(i)
@@ -388,8 +380,8 @@ func verifyInterfaceMethodRecvs(t *testing.T, named *types.Named, level int) {
388380
t.Errorf("%s: missing receiver type", m)
389381
continue
390382
}
391-
if recv.Type() != want {
392-
t.Errorf("%s: got recv type %s; want %s", m, recv.Type(), want)
383+
if recv.Type() != named {
384+
t.Errorf("%s: got recv type %s; want %s", m, recv.Type(), named)
393385
}
394386
}
395387

go/internal/gcimporter/ureader_yes.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,6 @@ func (pr *pkgReader) objIdx(idx pkgbits.Index) (*types.Package, string) {
512512

513513
named.SetTypeParams(r.typeParamNames())
514514

515-
// TODO(mdempsky): Rewrite receiver types to underlying is an
516-
// Interface? The go/types importer does this (I think because
517-
// unit tests expected that), but cmd/compile doesn't care
518-
// about it, so maybe we can avoid worrying about that here.
519515
rhs := r.typ()
520516
pk := r.p
521517
pk.laterFor(named, func() {
@@ -527,6 +523,28 @@ func (pr *pkgReader) objIdx(idx pkgbits.Index) (*types.Package, string) {
527523
f() // initialize RHS
528524
}
529525
underlying := rhs.Underlying()
526+
527+
// If the underlying type is an interface, we need to
528+
// duplicate its methods so we can replace the receiver
529+
// parameter's type (#49906).
530+
if iface, ok := underlying.(*types.Interface); ok && iface.NumExplicitMethods() != 0 {
531+
methods := make([]*types.Func, iface.NumExplicitMethods())
532+
for i := range methods {
533+
fn := iface.ExplicitMethod(i)
534+
sig := fn.Type().(*types.Signature)
535+
536+
recv := types.NewVar(fn.Pos(), fn.Pkg(), "", named)
537+
methods[i] = types.NewFunc(fn.Pos(), fn.Pkg(), fn.Name(), types.NewSignature(recv, sig.Params(), sig.Results(), sig.Variadic()))
538+
}
539+
540+
embeds := make([]types.Type, iface.NumEmbeddeds())
541+
for i := range embeds {
542+
embeds[i] = iface.EmbeddedType(i)
543+
}
544+
545+
underlying = types.NewInterfaceType(methods, embeds)
546+
}
547+
530548
named.SetUnderlying(underlying)
531549
})
532550

0 commit comments

Comments
 (0)