From 82d6e2eab8ea12962f2ad5f1d446350a7572d57c Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 22 Aug 2019 14:47:01 -0700 Subject: [PATCH] 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 --- src/go/types/type.go | 5 ++--- src/go/types/typestring_test.go | 12 ++++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/go/types/type.go b/src/go/types/type.go index 4807911e354..23ae6e33b70 100644 --- a/src/go/types/type.go +++ b/src/go/types/type.go @@ -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) diff --git a/src/go/types/typestring_test.go b/src/go/types/typestring_test.go index 3cae4f134a8..5d9db39bfc0 100644 --- a/src/go/types/typestring_test.go +++ b/src/go/types/typestring_test.go @@ -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()}"},