Skip to content

Commit 61693e9

Browse files
committed
compiler: Simplify type alias handling.
There's no need to consider type aliases when looking to compute method sets. Type aliases cannot have a different method set than the aliased type. Even methods defined on the type alias are still associated with the aliased type. This is a refactor that shouldn't change behavior, just clean up and better organize the code.
1 parent be67827 commit 61693e9

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

compiler/package.go

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -468,39 +468,38 @@ func Compile(importPath string, files []*ast.File, fileSet *token.FileSet, impor
468468
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)
469469
})
470470
d.MethodListCode = c.CatchOutput(0, func() {
471-
if _, isInterface := o.Type().Underlying().(*types.Interface); isInterface {
471+
if o.IsAlias() {
472472
return
473473
}
474-
switch t := o.Type().(type) {
475-
case *types.Named:
476-
var methods []string
477-
var ptrMethods []string
478-
for i := 0; i < t.NumMethods(); i++ {
479-
method := t.Method(i)
480-
name := method.Name()
481-
if reservedKeywords[name] {
482-
name += "$"
483-
}
484-
pkgPath := ""
485-
if !method.Exported() {
486-
pkgPath = method.Pkg().Path()
487-
}
488-
t := method.Type().(*types.Signature)
489-
entry := fmt.Sprintf(`{prop: "%s", name: "%s", pkg: "%s", typ: $funcType(%s)}`, name, method.Name(), pkgPath, c.initArgs(t))
490-
if _, isPtr := t.Recv().Type().(*types.Pointer); isPtr {
491-
ptrMethods = append(ptrMethods, entry)
492-
continue
493-
}
494-
methods = append(methods, entry)
474+
named := o.Type().(*types.Named)
475+
if _, ok := named.Underlying().(*types.Interface); ok {
476+
return
477+
}
478+
var methods []string
479+
var ptrMethods []string
480+
for i := 0; i < named.NumMethods(); i++ {
481+
method := named.Method(i)
482+
name := method.Name()
483+
if reservedKeywords[name] {
484+
name += "$"
495485
}
496-
if len(methods) > 0 {
497-
c.Printf("%s.methods = [%s];", c.typeName(o.Type()), strings.Join(methods, ", "))
486+
pkgPath := ""
487+
if !method.Exported() {
488+
pkgPath = method.Pkg().Path()
498489
}
499-
if len(ptrMethods) > 0 {
500-
c.Printf("%s.methods = [%s];", c.typeName(types.NewPointer(o.Type())), strings.Join(ptrMethods, ", "))
490+
t := method.Type().(*types.Signature)
491+
entry := fmt.Sprintf(`{prop: "%s", name: "%s", pkg: "%s", typ: $funcType(%s)}`, name, method.Name(), pkgPath, c.initArgs(t))
492+
if _, isPtr := t.Recv().Type().(*types.Pointer); isPtr {
493+
ptrMethods = append(ptrMethods, entry)
494+
continue
501495
}
502-
case *types.Struct:
503-
// TODO: Support type aliases.
496+
methods = append(methods, entry)
497+
}
498+
if len(methods) > 0 {
499+
c.Printf("%s.methods = [%s];", c.typeName(named), strings.Join(methods, ", "))
500+
}
501+
if len(ptrMethods) > 0 {
502+
c.Printf("%s.methods = [%s];", c.typeName(types.NewPointer(named)), strings.Join(ptrMethods, ", "))
504503
}
505504
})
506505
switch t := o.Type().Underlying().(type) {

0 commit comments

Comments
 (0)