From cef8f5c6a47abfc571a04ddfd00e8b7822e19a7a Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 18 Dec 2019 11:01:59 -0800 Subject: [PATCH] go/types: move includeTypes function to a method of Interface (cleanup) Change-Id: Ic3a4beaa279b6d6349136f5a9ff0447227cabb5a --- src/go/types/subst.go | 15 ++------------- src/go/types/type.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/go/types/subst.go b/src/go/types/subst.go index 5b03a23251..fe28931caa 100644 --- a/src/go/types/subst.go +++ b/src/go/types/subst.go @@ -104,7 +104,7 @@ func (check *Checker) instantiate(pos token.Pos, typ Type, targs []Type, poslist // list of iface types (i.e., the targ type list must be a subset of the iface types) if targ, _ := targ.Underlying().(*TypeParam); targ != nil { for _, t := range targ.Interface().types { - if !includesType(t, iface) { + if !iface.includes(t.Underlying()) { // TODO(gri) match this error message with the one below (or vice versa) check.softErrorf(pos, "%s does not satisfy %s (missing type %s)", targ, tpar.bound, t) break @@ -116,7 +116,7 @@ func (check *Checker) instantiate(pos token.Pos, typ Type, targs []Type, poslist // otherwise, targ's underlying type must also be one of the interface types listed, if any if len(iface.types) > 0 { // TODO(gri) must it be the underlying type, or should it just be the type? (spec question) - if !includesType(targ.Underlying(), iface) { + if !iface.includes(targ.Underlying()) { check.softErrorf(pos, "%s does not satisfy %s (%s not found in %s)", targ, tpar.bound, targ, iface) break } @@ -126,17 +126,6 @@ func (check *Checker) instantiate(pos token.Pos, typ Type, targs []Type, poslist return check.subst(pos, typ, tparams, targs) } -// includesType reports whether iface includes typ -// TODO(gri) make this a method of *Interface -func includesType(typ Type, iface *Interface) bool { - for _, t := range iface.types { - if Identical(typ.Underlying(), t) { - return true - } - } - return false -} - // subst returns the type typ with its type parameters tparams replaced by // the corresponding type arguments targs, recursively. func (check *Checker) subst(pos token.Pos, typ Type, tpars []*TypeName, targs []Type) Type { diff --git a/src/go/types/type.go b/src/go/types/type.go index 2105b52b09..19fde623a0 100644 --- a/src/go/types/type.go +++ b/src/go/types/type.go @@ -362,6 +362,16 @@ func (t *Interface) Method(i int) *Func { t.assertCompleteness(); return t.allMe // The interface must have been completed. func (t *Interface) Empty() bool { t.assertCompleteness(); return len(t.allMethods) == 0 } +// includes reports whether the interface t includes the type typ. +func (t *Interface) includes(typ Type) bool { + for _, t := range t.types { + if Identical(t, typ) { + return true + } + } + return false +} + // Complete computes the interface's method set. It must be called by users of // NewInterfaceType and NewInterface after the interface's embedded types are // fully defined and before using the interface type in any way other than to