@@ -192,17 +192,7 @@ func (check *Checker) implements(V, T Type, qf Qualifier) error {
192192 return errorf ("cannot implement %s (empty type set)" , T )
193193 }
194194
195- // If T is comparable, V must be comparable.
196- // TODO(gri) the error messages could be better, here
197- if Ti .IsComparable () && ! Comparable (V ) {
198- if Vi != nil && Vi .Empty () {
199- return errorf ("empty interface %s does not implement %s" , V , T )
200- }
201- return errorf ("%s does not implement comparable" , V )
202- }
203-
204- // V must implement T (methods)
205- // - check only if we have methods
195+ // V must implement T's methods, if any.
206196 if Ti .NumMethods () > 0 {
207197 if m , wrong := check .missingMethod (V , Ti , true ); m != nil {
208198 // TODO(gri) needs to print updated name to avoid major confusion in error message!
@@ -220,10 +210,17 @@ func (check *Checker) implements(V, T Type, qf Qualifier) error {
220210 }
221211 }
222212
213+ // If T is comparable, V must be comparable.
214+ // Remember as a pending error and report only if we don't have a more specific error.
215+ var pending error
216+ if Ti .IsComparable () && ! Comparable (V ) {
217+ pending = errorf ("%s does not implement comparable" , V )
218+ }
219+
223220 // V must also be in the set of types of T, if any.
224221 // Constraints with empty type sets were already excluded above.
225222 if ! Ti .typeSet ().hasTerms () {
226- return nil // nothing to do
223+ return pending // nothing to do
227224 }
228225
229226 // If V is itself an interface, each of its possible types must be in the set
@@ -234,7 +231,7 @@ func (check *Checker) implements(V, T Type, qf Qualifier) error {
234231 // TODO(gri) report which type is missing
235232 return errorf ("%s does not implement %s" , V , T )
236233 }
237- return nil
234+ return pending
238235 }
239236
240237 // Otherwise, V's type must be included in the iface type set.
@@ -262,5 +259,5 @@ func (check *Checker) implements(V, T Type, qf Qualifier) error {
262259 }
263260 }
264261
265- return nil
262+ return pending
266263}
0 commit comments