Commit 82d6e2ea authored by Robert Griesemer's avatar Robert Griesemer

go/types: NewInterface/NewInterfaceType complete empty interfaces

When creating a new interface via the exported API calls, a shared
empty and completed Interface value is returned if there are no
methods or embedded interfaces. This is a minor optimization and
matches the internal behavior when creating empty interfaces.

Since calling Interface.Complete is idempotent, and since there
are no other legitimate ways to create Interface values externally
but via NewInterface/NewInterfaceType calls, and completed Interfaces
are considered "immutable", this change is not expected to affect
clients. The only observable behavior that changed is the string
value for empty interfaces created via the above API calls; those
empty interfaces now don't show "incomplete" anymore even before
Interface.Complete is called. Except in special test cases, this
behavior is unlikely to affect clients.

Change-Id: Idf7f2cd112241c5b81a43b4544bbe3f2e003d8d8
Reviewed-on: https://go-review.googlesource.com/c/go/+/191417Reviewed-by: default avatarMatthew Dempsky <mdempsky@google.com>
parent a80c5f05
......@@ -278,13 +278,12 @@ func NewInterface(methods []*Func, embeddeds []*Named) *Interface {
// NewInterfaceType takes ownership of the provided methods and may modify their types by setting
// missing receivers. To compute the method set of the interface, Complete must be called.
func NewInterfaceType(methods []*Func, embeddeds []Type) *Interface {
typ := new(Interface)
if len(methods) == 0 && len(embeddeds) == 0 {
return typ
return &emptyInterface
}
// set method receivers if necessary
typ := new(Interface)
for _, m := range methods {
if sig := m.typ.(*Signature); sig.recv == nil {
sig.recv = NewVar(m.pos, m.pkg, "", typ)
......
......@@ -148,11 +148,11 @@ func TestIncompleteInterfaces(t *testing.T) {
{new(Interface), "interface{/* incomplete */}"},
{new(Interface).Complete(), "interface{}"},
{NewInterface(nil, nil), "interface{/* incomplete */}"},
{NewInterface(nil, nil), "interface{}"},
{NewInterface(nil, nil).Complete(), "interface{}"},
{NewInterface([]*Func{}, nil), "interface{/* incomplete */}"},
{NewInterface([]*Func{}, nil), "interface{}"},
{NewInterface([]*Func{}, nil).Complete(), "interface{}"},
{NewInterface(nil, []*Named{}), "interface{/* incomplete */}"},
{NewInterface(nil, []*Named{}), "interface{}"},
{NewInterface(nil, []*Named{}).Complete(), "interface{}"},
{NewInterface([]*Func{m}, nil), "interface{m() /* incomplete */}"},
{NewInterface([]*Func{m}, nil).Complete(), "interface{m()}"},
......@@ -162,11 +162,11 @@ func TestIncompleteInterfaces(t *testing.T) {
{NewInterface(nil, []*Named{newDefined(NewInterface([]*Func{m}, nil).Complete())}), "interface{T /* incomplete */}"},
{NewInterface(nil, []*Named{newDefined(NewInterface([]*Func{m}, nil).Complete())}).Complete(), "interface{T}"},
{NewInterfaceType(nil, nil), "interface{/* incomplete */}"},
{NewInterfaceType(nil, nil), "interface{}"},
{NewInterfaceType(nil, nil).Complete(), "interface{}"},
{NewInterfaceType([]*Func{}, nil), "interface{/* incomplete */}"},
{NewInterfaceType([]*Func{}, nil), "interface{}"},
{NewInterfaceType([]*Func{}, nil).Complete(), "interface{}"},
{NewInterfaceType(nil, []Type{}), "interface{/* incomplete */}"},
{NewInterfaceType(nil, []Type{}), "interface{}"},
{NewInterfaceType(nil, []Type{}).Complete(), "interface{}"},
{NewInterfaceType([]*Func{m}, nil), "interface{m() /* incomplete */}"},
{NewInterfaceType([]*Func{m}, nil).Complete(), "interface{m()}"},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment