package types
import (
+ "errors"
"fmt"
"go/token"
)
// Avoid duplicate errors; instantiate will have complained if tparams
// and targs do not have the same length.
if len(tparams) == len(targs) {
- check.verify(pos, tparams, targs, posList)
+ if i, err := check.verify(pos, tparams, targs); err != nil {
+ // best position for error reporting
+ pos := pos
+ if i < len(posList) {
+ pos = posList[i]
+ }
+ check.softErrorf(atPos(pos), _Todo, err.Error())
+ }
}
})
}
return named
}
-func (check *Checker) verify(pos token.Pos, tparams []*TypeName, targs []Type, posList []token.Pos) {
+func (check *Checker) verify(pos token.Pos, tparams []*TypeName, targs []Type) (int, error) {
smap := makeSubstMap(tparams, targs)
for i, tname := range tparams {
- // best position for error reporting
- pos := pos
- if i < len(posList) {
- pos = posList[i]
- }
-
// stop checking bounds after the first failure
- if !check.satisfies(pos, targs[i], tname.typ.(*TypeParam), smap) {
- break
+ if err := check.satisfies(pos, targs[i], tname.typ.(*TypeParam), smap); err != nil {
+ return i, err
}
}
+ return -1, nil
}
// satisfies reports whether the type argument targ satisfies the constraint of type parameter
// parameter tpar (after any of its type parameters have been substituted through smap).
// A suitable error is reported if the result is false.
// TODO(gri) This should be a method of interfaces or type sets.
-func (check *Checker) satisfies(pos token.Pos, targ Type, tpar *TypeParam, smap substMap) bool {
+func (check *Checker) satisfies(pos token.Pos, targ Type, tpar *TypeParam, smap substMap) error {
iface := tpar.iface()
if iface.Empty() {
- return true // no type bound
+ return nil // no type bound
+ }
+
+ // TODO(rfindley): it would be great if users could pass in a qualifier here,
+ // rather than falling back to verbose qualification. Maybe this can be part
+ // of a the shared environment.
+ var qf Qualifier
+ if check != nil {
+ qf = check.qualifier
+ }
+ errorf := func(format string, args ...interface{}) error {
+ return errors.New(sprintf(nil, qf, format, args...))
}
// The type parameter bound is parameterized with the same type parameters
// TODO(gri) the error messages needs to be better, here
if iface.IsComparable() && !Comparable(targ) {
if tpar := asTypeParam(targ); tpar != nil && tpar.iface().typeSet().IsAll() {
- check.softErrorf(atPos(pos), _Todo, "%s has no constraints", targ)
- return false
+ return errorf("%s has no constraints", targ)
}
- check.softErrorf(atPos(pos), _Todo, "%s does not satisfy comparable", targ)
- return false
+ return errorf("%s does not satisfy comparable", targ)
}
// targ must implement iface (methods)
// method set is empty.
// TODO(gri) is this what we want? (spec question)
if base, isPtr := deref(targ); isPtr && asTypeParam(base) != nil {
- check.errorf(atPos(pos), 0, "%s has no methods", targ)
- return false
+ return errorf("%s has no methods", targ)
}
if m, wrong := check.missingMethod(targ, iface, true); m != nil {
// TODO(gri) needs to print updated name to avoid major confusion in error message!
// TODO(gri) This can still report uninstantiated types which makes the error message
// more difficult to read then necessary.
// TODO(rFindley) should this use parentheses rather than ':' for qualification?
- check.softErrorf(atPos(pos), _Todo,
- "%s does not satisfy %s: wrong method signature\n\tgot %s\n\twant %s",
+ return errorf("%s does not satisfy %s: wrong method signature\n\tgot %s\n\twant %s",
targ, tpar.bound, wrong, m,
)
- } else {
- check.softErrorf(atPos(pos), 0, "%s does not satisfy %s (missing method %s)", targ, tpar.bound, m.name)
}
- return false
+ return errorf("%s does not satisfy %s (missing method %s)", targ, tpar.bound, m.name)
}
}
// targ's underlying type must also be one of the interface types listed, if any
if !iface.typeSet().hasTerms() {
- return true // nothing to do
+ return nil // nothing to do
}
// If targ is itself a type parameter, each of its possible types, but at least one, must be in the
if targ := asTypeParam(targ); targ != nil {
targBound := targ.iface()
if !targBound.typeSet().hasTerms() {
- check.softErrorf(atPos(pos), _Todo, "%s does not satisfy %s (%s has no type constraints)", targ, tpar.bound, targ)
- return false
+ return errorf("%s does not satisfy %s (%s has no type constraints)", targ, tpar.bound, targ)
}
if !targBound.typeSet().subsetOf(iface.typeSet()) {
// TODO(gri) need better error message
- check.softErrorf(atPos(pos), _Todo, "%s does not satisfy %s", targ, tpar.bound)
- return false
+ return errorf("%s does not satisfy %s", targ, tpar.bound)
}
- return true
+ return nil
}
// Otherwise, targ's type or underlying type must also be one of the interface types listed, if any.
if !iface.typeSet().includes(targ) {
// TODO(gri) better error message
- check.softErrorf(atPos(pos), _Todo, "%s does not satisfy %s", targ, tpar.bound)
- return false
+ return errorf("%s does not satisfy %s", targ, tpar.bound)
}
- return true
+ return nil
}