Skip to content

Commit 15ab3c0

Browse files
committed
compiler: Fix panic due to type aliases.
The tests of math/bits define a type alias, a new Go 1.9 language feature: type entry = struct { nlz, ntz, pop int } This caused compiler.Compile to panic on named := o.Type().(*types.Named), because the type of o.Type() could be *types.Struct in such cases. Fix the panic, and add a TODO comment to properly handle/support type aliases. Fixes: $ gopherjs test --short math/bits panic: interface conversion: types.Type is *types.Struct, not *types.Named goroutine 1 [running]: github.com/gopherjs/gopherjs/compiler.Compile.func10.2() /Users/Dmitri/Dropbox/Work/2013/GoLand/src/github.com/gopherjs/gopherjs/compiler/package.go:467 +0xa08 github.com/gopherjs/gopherjs/compiler.(*funcContext).CatchOutput(0xc4200ccd10, 0x0, 0xc4223023d8, 0xc420915680, 0x128, 0x140) /Users/Dmitri/Dropbox/Work/2013/GoLand/src/github.com/gopherjs/gopherjs/compiler/utils.go:65 +0x96 github.com/gopherjs/gopherjs/compiler.Compile.func10() /Users/Dmitri/Dropbox/Work/2013/GoLand/src/github.com/gopherjs/gopherjs/compiler/package.go:465 +0xea github.com/gopherjs/gopherjs/compiler.Compile.func4(0xc420672540, 0xc420672540, 0xc4216b5630, 0xc42021db30) /Users/Dmitri/Dropbox/Work/2013/GoLand/src/github.com/gopherjs/gopherjs/compiler/package.go:283 +0xa5 github.com/gopherjs/gopherjs/compiler.Compile(0x7fff5fbffb76, 0x9, 0xc420259a40, 0x3, 0x4, 0xc4201c3b40, 0xc42025a490, 0x0, 0x2d, 0x0, ...) /Users/Dmitri/Dropbox/Work/2013/GoLand/src/github.com/gopherjs/gopherjs/compiler/package.go:436 +0x311a github.com/gopherjs/gopherjs/build.(*Session).BuildPackage(0xc420134b00, 0xc4201245f0, 0x0, 0x0, 0x0) /Users/Dmitri/Dropbox/Work/2013/GoLand/src/github.com/gopherjs/gopherjs/build/build.go:627 +0x638 main.main.func5.1.1(0xc4201245f0, 0x157356b, 0x5, 0xc420156719, 0xc420134a60, 0x2) /Users/Dmitri/Dropbox/Work/2013/GoLand/src/github.com/gopherjs/gopherjs/tool.go:343 +0x1e0 main.main.func5.1(0xc4201222c0, 0xc420133b50, 0xc42012a09f, 0xc420126310, 0xc4201262c0, 0xc4201262d0, 0xc4201262f0, 0xc420126300, 0xc42012a09d, 0xc42012a09e, ...) /Users/Dmitri/Dropbox/Work/2013/GoLand/src/github.com/gopherjs/gopherjs/tool.go:350 +0x8cb main.main.func5(0xc420158b40, 0xc4201263e0, 0x1, 0x1) /Users/Dmitri/Dropbox/Work/2013/GoLand/src/github.com/gopherjs/gopherjs/tool.go:463 +0x14f github.com/spf13/cobra.(*Command).execute(0xc420158b40, 0xc420134360, 0x2, 0x2, 0xc420158b40, 0xc420134360) /Users/Dmitri/Dropbox/Work/2013/GoLanding/src/github.com/spf13/cobra/command.go:651 +0x23d github.com/spf13/cobra.(*Command).ExecuteC(0xc420159200, 0x156fe20, 0xc420159340, 0xc420133f68) /Users/Dmitri/Dropbox/Work/2013/GoLanding/src/github.com/spf13/cobra/command.go:726 +0x2fe github.com/spf13/cobra.(*Command).Execute(0xc420159200, 0xc420133f30, 0x8) /Users/Dmitri/Dropbox/Work/2013/GoLanding/src/github.com/spf13/cobra/command.go:685 +0x2b main.main() /Users/Dmitri/Dropbox/Work/2013/GoLand/src/github.com/gopherjs/gopherjs/tool.go:530 +0x12c4
1 parent 44d151d commit 15ab3c0

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

compiler/package.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,15 @@ func Compile(importPath string, files []*ast.File, fileSet *token.FileSet, impor
463463
c.Printf(`%s = $newType(%d, %s, "%s.%s", %t, "%s", %t, %s);`, lhs, size, typeKind(o.Type()), o.Pkg().Name(), o.Name(), o.Name() != "", o.Pkg().Path(), o.Exported(), constructor)
464464
})
465465
d.MethodListCode = c.CatchOutput(0, func() {
466-
if _, isInterface := o.Type().Underlying().(*types.Interface); !isInterface {
467-
named := o.Type().(*types.Named)
466+
if _, isInterface := o.Type().Underlying().(*types.Interface); isInterface {
467+
return
468+
}
469+
switch t := o.Type().(type) {
470+
case *types.Named:
468471
var methods []string
469472
var ptrMethods []string
470-
for i := 0; i < named.NumMethods(); i++ {
471-
method := named.Method(i)
473+
for i := 0; i < t.NumMethods(); i++ {
474+
method := t.Method(i)
472475
name := method.Name()
473476
if reservedKeywords[name] {
474477
name += "$"
@@ -491,6 +494,8 @@ func Compile(importPath string, files []*ast.File, fileSet *token.FileSet, impor
491494
if len(ptrMethods) > 0 {
492495
c.Printf("%s.methods = [%s];", c.typeName(types.NewPointer(o.Type())), strings.Join(ptrMethods, ", "))
493496
}
497+
case *types.Struct:
498+
// TODO: Support type aliases.
494499
}
495500
})
496501
switch t := o.Type().Underlying().(type) {

0 commit comments

Comments
 (0)