Skip to content

Commit 3c7e491

Browse files
committed
go/types: add debugging code to detect use of incomplete interfaces
The comment for phase 2 of checker.interfaceType (typexpr.go:517) requires that embedded interfaces be complete for correctness of the algorithm. Yet, the very next comment (typexpr.go:530) states that underlying embedded interfaces may in fact be incomplete. This is in fact the case and the underlying bug in issue #18395. This change makes sure that new interface types are marked complete when finished (per the implicit definition in Interface.Complete, type.go:302). It also adds a check, enabled in debug mode only, to detect the use of incomplete embedded interfaces during construction of a new interface. In debug mode, this check fails for the testcase in the issue (and several others). This change has no noticeable impact with debug mode disabled. For #18395. Change-Id: Ibb81e47257651282fb3755a80a36ab5d392e636d Reviewed-on: https://go-review.googlesource.com/78955 Reviewed-by: Alan Donovan <[email protected]>
1 parent 4aac23c commit 3c7e491

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/go/types/typexpr.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,10 @@ func (check *Checker) interfaceType(iface *Interface, ityp *ast.InterfaceType, d
540540
}
541541
iface.embeddeds = append(iface.embeddeds, named)
542542
// collect embedded methods
543+
if debug && embed.allMethods == nil {
544+
check.dump("%s: incomplete embedded interface %s", pos, named)
545+
unreachable()
546+
}
543547
for _, m := range embed.allMethods {
544548
if check.declareInSet(&mset, pos, m) {
545549
iface.allMethods = append(iface.allMethods, m)
@@ -579,7 +583,11 @@ func (check *Checker) interfaceType(iface *Interface, ityp *ast.InterfaceType, d
579583
// claim source order in the future. Revisit.
580584
sort.Sort(byUniqueTypeName(iface.embeddeds))
581585

582-
sort.Sort(byUniqueMethodName(iface.allMethods))
586+
if iface.allMethods == nil {
587+
iface.allMethods = make([]*Func, 0) // mark interface as complete
588+
} else {
589+
sort.Sort(byUniqueMethodName(iface.allMethods))
590+
}
583591
}
584592

585593
// byUniqueTypeName named type lists can be sorted by their unique type names.

0 commit comments

Comments
 (0)