Skip to content

Commit fd4392b

Browse files
committed
go/types: don't over-eagerly verify embedded interfaces
In https://go-review.googlesource.com/c/go/+/114317 (fix for #25301) the constructor types.NewInterface was replaced with NewInterface2. The new constructor aggressively verified that embedded interfaces had an underlying type of interface type; the old code didn't do any verification. During importing, defined types may be not yet fully set up, and testing their underlying types will fail in those cases. This change only verifies embedded types that are not defined types and thus restores behavior for defined types to how it was before the fix for #25301. Fixes #25596. Fixes #25615. Change-Id: Ifd694413656ec0b780fe4f37acaa9e6ba6077271 Reviewed-on: https://go-review.googlesource.com/115155 Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent f5cf72d commit fd4392b

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/go/internal/gcimporter/gcimporter_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,27 @@ func TestIssue25301(t *testing.T) {
530530
importPkg(t, "./testdata/issue25301")
531531
}
532532

533+
func TestIssue25596(t *testing.T) {
534+
skipSpecialPlatforms(t)
535+
536+
// This package only handles gc export data.
537+
if runtime.Compiler != "gc" {
538+
t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
539+
}
540+
541+
// On windows, we have to set the -D option for the compiler to avoid having a drive
542+
// letter and an illegal ':' in the import path - just skip it (see also issue #3483).
543+
if runtime.GOOS == "windows" {
544+
t.Skip("avoid dealing with relative paths/drive letters on windows")
545+
}
546+
547+
if f := compile(t, "testdata", "issue25596.go"); f != "" {
548+
defer os.Remove(f)
549+
}
550+
551+
importPkg(t, "./testdata/issue25596")
552+
}
553+
533554
func importPkg(t *testing.T, path string) *types.Package {
534555
pkg, err := Import(make(map[string]*types.Package), path, ".", nil)
535556
if err != nil {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package issue25596
6+
7+
type E interface {
8+
M() T
9+
}
10+
11+
type T interface {
12+
E
13+
}

src/go/types/type.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ func NewInterface(methods []*Func, embeddeds []*Named) *Interface {
275275
}
276276

277277
// NewInterface2 returns a new (incomplete) interface for the given methods and embedded types.
278-
// Each embedded type must have an underlying type of interface type.
278+
// Each embedded type must have an underlying type of interface type (this property is not
279+
// verified for defined types, which may be in the process of being set up and which don't
280+
// have a valid underlying type yet).
279281
// NewInterface2 takes ownership of the provided methods and may modify their types by setting
280282
// missing receivers. To compute the method set of the interface, Complete must be called.
281283
func NewInterface2(methods []*Func, embeddeds []Type) *Interface {
@@ -298,8 +300,12 @@ func NewInterface2(methods []*Func, embeddeds []Type) *Interface {
298300
sort.Sort(byUniqueMethodName(methods))
299301

300302
if len(embeddeds) > 0 {
303+
// All embedded types should be interfaces; however, defined types
304+
// may not yet be fully resolved. Only verify that non-defined types
305+
// are interfaces. This matches the behavior of the code before the
306+
// fix for #25301 (issue #25596).
301307
for _, t := range embeddeds {
302-
if !IsInterface(t) {
308+
if _, ok := t.(*Named); !ok && !IsInterface(t) {
303309
panic("embedded type is not an interface")
304310
}
305311
}

0 commit comments

Comments
 (0)