[dev.typeparams] go/types: don't expose the TypeSet API for 1.18

The TypeSet API is very new and probably not necessary to expose outside
of go/types, at least for 1.18. Users can check whether a type is
contained within a type set via Implements, and can access the
representation of the type set via the embedded Unions.

Change-Id: Icc7355285785bee5aa7a8fe74052bcb0fedcd0a1
Reviewed-on: https://go-review.googlesource.com/c/go/+/341289
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Rob Findley 2021-08-04 10:45:16 -04:00 committed by Robert Findley
parent 40ba119e3f
commit 0f34a92df7
5 changed files with 19 additions and 19 deletions

View file

@ -844,7 +844,7 @@ func (check *Checker) applyTypeFunc(f func(Type) Type, x Type) Type {
ptyp := check.NewTypeParam(tpar, &emptyInterface) // assigns type to tpar as a side-effect ptyp := check.NewTypeParam(tpar, &emptyInterface) // assigns type to tpar as a side-effect
ptyp.index = tp.index ptyp.index = tp.index
tsum := newUnion(rtypes, tildes) tsum := newUnion(rtypes, tildes)
ptyp.bound = &Interface{complete: true, tset: &TypeSet{types: tsum}} ptyp.bound = &Interface{complete: true, tset: &_TypeSet{types: tsum}}
return ptyp return ptyp
} }

View file

@ -21,11 +21,11 @@ type Interface struct {
embedPos *[]token.Pos // positions of embedded elements; or nil (for error messages) - use pointer to save space embedPos *[]token.Pos // positions of embedded elements; or nil (for error messages) - use pointer to save space
complete bool // indicates that obj, methods, and embeddeds are set and type set can be computed complete bool // indicates that obj, methods, and embeddeds are set and type set can be computed
tset *TypeSet // type set described by this interface, computed lazily tset *_TypeSet // type set described by this interface, computed lazily
} }
// typeSet returns the type set for interface t. // typeSet returns the type set for interface t.
func (t *Interface) typeSet() *TypeSet { return computeTypeSet(nil, token.NoPos, t) } func (t *Interface) typeSet() *_TypeSet { return computeTypeSet(nil, token.NoPos, t) }
// is reports whether interface t represents types that all satisfy f. // is reports whether interface t represents types that all satisfy f.
func (t *Interface) is(f func(Type, bool) bool) bool { func (t *Interface) is(f func(Type, bool) bool) bool {

View file

@ -48,7 +48,7 @@ func TestSizeof(t *testing.T) {
// Misc // Misc
{Scope{}, 44, 88}, {Scope{}, 44, 88},
{Package{}, 40, 80}, {Package{}, 40, 80},
{TypeSet{}, 24, 48}, {_TypeSet{}, 24, 48},
} }
for _, test := range tests { for _, test := range tests {
got := reflect.TypeOf(test.val).Size() got := reflect.TypeOf(test.val).Size()

View file

@ -14,8 +14,8 @@ import (
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// API // API
// A TypeSet represents the type set of an interface. // A _TypeSet represents the type set of an interface.
type TypeSet struct { type _TypeSet struct {
comparable bool // if set, the interface is or embeds comparable comparable bool // if set, the interface is or embeds comparable
// TODO(gri) consider using a set for the methods for faster lookup // TODO(gri) consider using a set for the methods for faster lookup
methods []*Func // all methods of the interface; sorted by unique ID methods []*Func // all methods of the interface; sorted by unique ID
@ -23,14 +23,14 @@ type TypeSet struct {
} }
// IsTop reports whether type set s is the top type set (corresponding to the empty interface). // IsTop reports whether type set s is the top type set (corresponding to the empty interface).
func (s *TypeSet) IsTop() bool { return !s.comparable && len(s.methods) == 0 && s.types == nil } func (s *_TypeSet) IsTop() bool { return !s.comparable && len(s.methods) == 0 && s.types == nil }
// IsMethodSet reports whether the type set s is described by a single set of methods. // IsMethodSet reports whether the type set s is described by a single set of methods.
func (s *TypeSet) IsMethodSet() bool { return !s.comparable && s.types == nil } func (s *_TypeSet) IsMethodSet() bool { return !s.comparable && s.types == nil }
// IsComparable reports whether each type in the set is comparable. // IsComparable reports whether each type in the set is comparable.
// TODO(gri) this is not correct - there may be s.types values containing non-comparable types // TODO(gri) this is not correct - there may be s.types values containing non-comparable types
func (s *TypeSet) IsComparable() bool { func (s *_TypeSet) IsComparable() bool {
if s.types == nil { if s.types == nil {
return s.comparable return s.comparable
} }
@ -46,24 +46,24 @@ func (s *TypeSet) IsComparable() bool {
// TODO(gri) IsTypeSet is not a great name. Find a better one. // TODO(gri) IsTypeSet is not a great name. Find a better one.
// IsTypeSet reports whether the type set s is represented by a finite set of underlying types. // IsTypeSet reports whether the type set s is represented by a finite set of underlying types.
func (s *TypeSet) IsTypeSet() bool { func (s *_TypeSet) IsTypeSet() bool {
return !s.comparable && len(s.methods) == 0 return !s.comparable && len(s.methods) == 0
} }
// NumMethods returns the number of methods available. // NumMethods returns the number of methods available.
func (s *TypeSet) NumMethods() int { return len(s.methods) } func (s *_TypeSet) NumMethods() int { return len(s.methods) }
// Method returns the i'th method of type set s for 0 <= i < s.NumMethods(). // Method returns the i'th method of type set s for 0 <= i < s.NumMethods().
// The methods are ordered by their unique ID. // The methods are ordered by their unique ID.
func (s *TypeSet) Method(i int) *Func { return s.methods[i] } func (s *_TypeSet) Method(i int) *Func { return s.methods[i] }
// LookupMethod returns the index of and method with matching package and name, or (-1, nil). // LookupMethod returns the index of and method with matching package and name, or (-1, nil).
func (s *TypeSet) LookupMethod(pkg *Package, name string) (int, *Func) { func (s *_TypeSet) LookupMethod(pkg *Package, name string) (int, *Func) {
// TODO(gri) s.methods is sorted - consider binary search // TODO(gri) s.methods is sorted - consider binary search
return lookupMethod(s.methods, pkg, name) return lookupMethod(s.methods, pkg, name)
} }
func (s *TypeSet) String() string { func (s *_TypeSet) String() string {
if s.IsTop() { if s.IsTop() {
return "" return ""
} }
@ -102,7 +102,7 @@ func (s *TypeSet) String() string {
// enumerable types in the type set s. If the type set comprises all types // enumerable types in the type set s. If the type set comprises all types
// f is called once with the top type; if the type set is empty, the result // f is called once with the top type; if the type set is empty, the result
// is false. // is false.
func (s *TypeSet) underIs(f func(Type) bool) bool { func (s *_TypeSet) underIs(f func(Type) bool) bool {
switch t := s.types.(type) { switch t := s.types.(type) {
case nil: case nil:
return f(theTop) return f(theTop)
@ -114,10 +114,10 @@ func (s *TypeSet) underIs(f func(Type) bool) bool {
} }
// topTypeSet may be used as type set for the empty interface. // topTypeSet may be used as type set for the empty interface.
var topTypeSet TypeSet var topTypeSet _TypeSet
// computeTypeSet may be called with check == nil. // computeTypeSet may be called with check == nil.
func computeTypeSet(check *Checker, pos token.Pos, ityp *Interface) *TypeSet { func computeTypeSet(check *Checker, pos token.Pos, ityp *Interface) *_TypeSet {
if ityp.tset != nil { if ityp.tset != nil {
return ityp.tset return ityp.tset
} }
@ -157,7 +157,7 @@ func computeTypeSet(check *Checker, pos token.Pos, ityp *Interface) *TypeSet {
// have valid interfaces. Mark the interface as complete to avoid // have valid interfaces. Mark the interface as complete to avoid
// infinite recursion if the validType check occurs later for some // infinite recursion if the validType check occurs later for some
// reason. // reason.
ityp.tset = new(TypeSet) // TODO(gri) is this sufficient? ityp.tset = new(_TypeSet) // TODO(gri) is this sufficient?
// Methods of embedded interfaces are collected unchanged; i.e., the identity // Methods of embedded interfaces are collected unchanged; i.e., the identity
// of a method I.m's Func Object of an interface I is the same as that of // of a method I.m's Func Object of an interface I is the same as that of

View file

@ -100,7 +100,7 @@ func defPredeclaredTypes() {
{ {
obj := NewTypeName(token.NoPos, nil, "comparable", nil) obj := NewTypeName(token.NoPos, nil, "comparable", nil)
obj.setColor(black) obj.setColor(black)
ityp := &Interface{obj, nil, nil, nil, true, &TypeSet{true, nil, nil}} ityp := &Interface{obj, nil, nil, nil, true, &_TypeSet{true, nil, nil}}
NewNamed(obj, ityp, nil) NewNamed(obj, ityp, nil)
def(obj) def(obj)
} }