Skip to content

Commit

Permalink
go/internal/gcimporter: rewrite interface receiver parameters
Browse files Browse the repository at this point in the history
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 <drchase@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
  • Loading branch information
dr2chase committed Aug 4, 2022
1 parent b5fd088 commit fc3b24a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
12 changes: 2 additions & 10 deletions go/internal/gcimporter/gcimporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,6 @@ func verifyInterfaceMethodRecvs(t *testing.T, named *types.Named, level int) {
return // not an interface
}

// The unified IR importer always sets interface method receiver
// parameters to point to the Interface type, rather than the Named.
// See #49906.
var want types.Type = named
if unifiedIR {
want = iface
}

// check explicitly declared methods
for i := 0; i < iface.NumExplicitMethods(); i++ {
m := iface.ExplicitMethod(i)
Expand All @@ -388,8 +380,8 @@ func verifyInterfaceMethodRecvs(t *testing.T, named *types.Named, level int) {
t.Errorf("%s: missing receiver type", m)
continue
}
if recv.Type() != want {
t.Errorf("%s: got recv type %s; want %s", m, recv.Type(), want)
if recv.Type() != named {
t.Errorf("%s: got recv type %s; want %s", m, recv.Type(), named)
}
}

Expand Down
26 changes: 22 additions & 4 deletions go/internal/gcimporter/ureader_yes.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,6 @@ func (pr *pkgReader) objIdx(idx pkgbits.Index) (*types.Package, string) {

named.SetTypeParams(r.typeParamNames())

// TODO(mdempsky): Rewrite receiver types to underlying is an
// Interface? The go/types importer does this (I think because
// unit tests expected that), but cmd/compile doesn't care
// about it, so maybe we can avoid worrying about that here.
rhs := r.typ()
pk := r.p
pk.laterFor(named, func() {
Expand All @@ -527,6 +523,28 @@ func (pr *pkgReader) objIdx(idx pkgbits.Index) (*types.Package, string) {
f() // initialize RHS
}
underlying := rhs.Underlying()

// If the underlying type is an interface, we need to
// duplicate its methods so we can replace the receiver
// parameter's type (#49906).
if iface, ok := underlying.(*types.Interface); ok && iface.NumExplicitMethods() != 0 {
methods := make([]*types.Func, iface.NumExplicitMethods())
for i := range methods {
fn := iface.ExplicitMethod(i)
sig := fn.Type().(*types.Signature)

recv := types.NewVar(fn.Pos(), fn.Pkg(), "", named)
methods[i] = types.NewFunc(fn.Pos(), fn.Pkg(), fn.Name(), types.NewSignature(recv, sig.Params(), sig.Results(), sig.Variadic()))
}

embeds := make([]types.Type, iface.NumEmbeddeds())
for i := range embeds {
embeds[i] = iface.EmbeddedType(i)
}

underlying = types.NewInterfaceType(methods, embeds)
}

named.SetUnderlying(underlying)
})

Expand Down

0 comments on commit fc3b24a

Please sign in to comment.