1
0
mirror of https://github.com/golang/go synced 2024-11-23 23:20:08 -07:00

cmd/compile: unified IR support for implicit interfaces

Change-Id: Ibdaa0750f7bc47b513c047fdf4b7145ebba9e870
Reviewed-on: https://go-review.googlesource.com/c/go/+/386001
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Trust: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Matthew Dempsky 2022-02-01 17:41:46 -08:00
parent 7c151f3280
commit 21998413ad
3 changed files with 17 additions and 1 deletions

View File

@ -449,6 +449,8 @@ func (r *reader) interfaceType() *types.Type {
tpkg := types.LocalPkg // TODO(mdempsky): Remove after iexport is gone.
nmethods, nembeddeds := r.Len(), r.Len()
implicit := nmethods == 0 && nembeddeds == 1 && r.Bool()
assert(!implicit) // implicit interfaces only appear in constraints
fields := make([]*types.Field, nmethods+nembeddeds)
methods, embeddeds := fields[:nmethods], fields[nmethods:]

View File

@ -295,6 +295,7 @@ func (r *reader2) unionType() *types2.Union {
func (r *reader2) interfaceType() *types2.Interface {
methods := make([]*types2.Func, r.Len())
embeddeds := make([]types2.Type, r.Len())
implicit := len(methods) == 0 && len(embeddeds) == 1 && r.Bool()
for i := range methods {
pos := r.pos()
@ -307,7 +308,11 @@ func (r *reader2) interfaceType() *types2.Interface {
embeddeds[i] = r.typ()
}
return types2.NewInterfaceType(methods, embeddeds)
iface := types2.NewInterfaceType(methods, embeddeds)
if implicit {
iface.MarkImplicit()
}
return iface
}
func (r *reader2) signature(recv *types2.Var, rtparams, tparams []*types2.TypeParam) *types2.Signature {

View File

@ -396,6 +396,15 @@ func (w *writer) interfaceType(typ *types2.Interface) {
w.Len(typ.NumExplicitMethods())
w.Len(typ.NumEmbeddeds())
if typ.NumExplicitMethods() == 0 && typ.NumEmbeddeds() == 1 {
w.Bool(typ.IsImplicit())
} else {
// Implicit interfaces always have 0 explicit methods and 1
// embedded type, so we skip writing out the implicit flag
// otherwise as a space optimization.
assert(!typ.IsImplicit())
}
for i := 0; i < typ.NumExplicitMethods(); i++ {
m := typ.ExplicitMethod(i)
sig := m.Type().(*types2.Signature)