Skip to content

Commit 09852e7

Browse files
griesemergopherbot
authored andcommitted
go/types, types2: add predicate to missingMethod signature
This allows us to use missingMethod with different type comparers, such as the global Identical predicate, or a unifier. Preparation for the next CL. Change-Id: I237fd9dd7feb3708847ae6d9a112bcdd0aa1ecb4 Reviewed-on: https://go-review.googlesource.com/c/go/+/472297 Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]>
1 parent 052a36c commit 09852e7

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

src/cmd/compile/internal/types2/instantiate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func (check *Checker) implements(V, T Type, constraint bool, cause *string) bool
241241
}
242242

243243
// V must implement T's methods, if any.
244-
if m, wrong := check.missingMethod(V, Ti, true); m != nil /* !Implements(V, Ti) */ {
244+
if m, wrong := check.missingMethod(V, Ti, true, Identical); m != nil /* !Implements(V, Ti) */ {
245245
if cause != nil {
246246
*cause = check.sprintf("%s does not %s %s %s", V, verb, T, check.missingMethodCause(V, T, m, wrong))
247247
}

src/cmd/compile/internal/types2/lookup.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,20 +305,22 @@ func (l *instanceLookup) add(inst *Named) {
305305
// present in V have matching types (e.g., for a type assertion x.(T) where
306306
// x is of interface type V).
307307
func MissingMethod(V Type, T *Interface, static bool) (method *Func, wrongType bool) {
308-
m, alt := (*Checker)(nil).missingMethod(V, T, static)
308+
m, alt := (*Checker)(nil).missingMethod(V, T, static, Identical)
309309
// Only report a wrong type if the alternative method has the same name as m.
310310
return m, alt != nil && alt.name == m.name // alt != nil implies m != nil
311311
}
312312

313-
// missingMethod is like MissingMethod but accepts a *Checker as receiver.
313+
// missingMethod is like MissingMethod but accepts a *Checker as receiver
314+
// and comparator equivalent for type comparison.
314315
// The receiver may be nil if missingMethod is invoked through an exported
315316
// API call (such as MissingMethod), i.e., when all methods have been type-
316317
// checked.
318+
// The comparator is used to compare signatures.
317319
//
318320
// If a method is missing on T but is found on *T, or if a method is found
319321
// on T when looked up with case-folding, this alternative method is returned
320322
// as the second result.
321-
func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method, alt *Func) {
323+
func (check *Checker) missingMethod(V Type, T *Interface, static bool, equivalent func(x, y Type) bool) (method, alt *Func) {
322324
if T.NumMethods() == 0 {
323325
return
324326
}
@@ -336,7 +338,7 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
336338
return m, nil
337339
}
338340

339-
if !Identical(f.typ, m.typ) {
341+
if !equivalent(f.typ, m.typ) {
340342
return m, f
341343
}
342344
}
@@ -370,7 +372,7 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
370372
check.objDecl(f, nil)
371373
}
372374

373-
if !found || !Identical(f.typ, m.typ) {
375+
if !found || !equivalent(f.typ, m.typ) {
374376
return m, f
375377
}
376378
}
@@ -467,7 +469,7 @@ func (check *Checker) assertableTo(V *Interface, T Type) (method, wrongType *Fun
467469
return
468470
}
469471
// TODO(gri) fix this for generalized interfaces
470-
return check.missingMethod(T, V, false)
472+
return check.missingMethod(T, V, false, Identical)
471473
}
472474

473475
// newAssertableTo reports whether a value of type V can be asserted to have type T.

src/go/types/instantiate.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/go/types/lookup.go

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)