mirror of
https://github.com/golang/go
synced 2024-11-24 07:40:17 -07:00
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/+/191417 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
parent
a80c5f0588
commit
82d6e2eab8
@ -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()}"},
|
||||
|
Loading…
Reference in New Issue
Block a user