Skip to content

Commit

Permalink
fix #1198
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed May 19, 2022
1 parent d64cb6a commit 29d9d38
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
44 changes: 44 additions & 0 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,50 @@ func main() {
`)
}

func TestInterfaceBugNilUnderlying_Issue1198(t *testing.T) {
gopClTest(t, `
import "runtime"
type Outer interface{ Inner }
type impl struct{}
func New() Outer { return &impl{} }
type Inner interface {
DoStuff() error
}
func (a *impl) DoStuff() error {
return nil
}
func main() {
var outer Outer = New()
}
`, `package main
type Inner interface {
DoStuff() error
}
type Outer interface {
Inner
}
type impl struct {
}
func (a *impl) DoStuff() error {
return nil
}
func New() Outer {
return &impl{}
}
func main() {
var outer Outer = New()
}
`)
}

func TestInterfaceBugNilUnderlying_Issue1196(t *testing.T) {
gopClTest(t, `
func main() {
Expand Down
8 changes: 6 additions & 2 deletions cl/func_type_and_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func toStructType(ctx *blockCtx, v *ast.StructType) *types.Struct {
if chkRedecl(name, field.Type.Pos()) {
continue
}
if t, ok := typ.(*types.Named); ok { // NOTE: embedded type should ensure loaded
if t, ok := typ.(*types.Named); ok { // #1196: embedded type should ensure loaded
ctx.loadNamed(ctx.pkg, t)
}
fld := types.NewField(token.NoPos, pkg, name, typ, true)
Expand Down Expand Up @@ -319,7 +319,11 @@ func toInterfaceType(ctx *blockCtx, v *ast.InterfaceType) types.Type {
var embeddeds []types.Type
for _, m := range methodsList {
if m.Names == nil { // embedded
embeddeds = append(embeddeds, toType(ctx, m.Type))
typ := toType(ctx, m.Type)
if t, ok := typ.(*types.Named); ok { // #1198: embedded type should ensure loaded
ctx.loadNamed(ctx.pkg, t)
}
embeddeds = append(embeddeds, typ)
continue
}
name := m.Names[0].Name
Expand Down
1 change: 1 addition & 0 deletions cmd/goptestgo/goptestgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ var (
"issue22662.go": {},
"issue27201.go": {},
"issue46903.go": {},
"issue50190.go": {}, // interesting, should be fixed
"nilptr_aix.go": {},
"inline_literal.go": {},
"returntype.go": {}, // not a problem
Expand Down

0 comments on commit 29d9d38

Please sign in to comment.