go/types: move includeTypes function to a method of Interface (cleanup)

Change-Id: Ic3a4beaa279b6d6349136f5a9ff0447227cabb5a
This commit is contained in:
Robert Griesemer 2019-12-18 11:01:59 -08:00
parent 74d0d556d9
commit cef8f5c6a4
2 changed files with 12 additions and 13 deletions

View File

@ -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 {

View File

@ -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