Skip to content

Commit

Permalink
Adding methods to an interface that uses internal types is also minor.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobg committed Sep 13, 2024
1 parent 62de330 commit c85f9c4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- mode: go -*-

// {{ define "older" }}
package addmethod
package addmethod1

type X interface {
A() int
Expand All @@ -10,7 +10,7 @@ type X interface {
// {{ end }}

// {{ define "newer" }}
package addmethod
package addmethod1

type X interface {
A() int
Expand Down
52 changes: 50 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go/types"
"reflect"
"regexp"
"strings"

"golang.org/x/tools/go/packages"
)
Expand Down Expand Up @@ -237,9 +238,12 @@ func (c *comparer) compareInterfaces(older, newer *types.Interface) Result {

if c.implements(newer, older) {
if !c.implements(older, newer) {
if anyUnexportedMethods(older) {
switch {
case anyUnexportedMethods(older):
res = rwrapf(Minor, "new interface %s is a superset of older, with unexported methods", newer)
} else {
case anyInternalTypes(older):
res = rwrapf(Minor, "new interface %s is a superset of older, using internal types", newer)
default:
res = rwrapf(Major, "new interface %s is a superset of older", newer)
}
}
Expand Down Expand Up @@ -315,6 +319,50 @@ func anyUnexportedMethods(intf *types.Interface) bool {
return false
}

// Do any of the types in the method args or results have "internal" in their pkgpaths?
func anyInternalTypes(intf *types.Interface) bool {
for i := 0; i < intf.NumMethods(); i++ {
sig, ok := intf.Method(i).Type().(*types.Signature)
if !ok {
// Should be impossible.
continue
}
if anyInternalTypesInTuple(sig.Params()) || anyInternalTypesInTuple(sig.Results()) {
return true
}
if recv := sig.Recv(); recv != nil && isInternalType(recv.Type()) {
return true
}
}
return false
}

func anyInternalTypesInTuple(tup *types.Tuple) bool {
for i := 0; i < tup.Len(); i++ {
if isInternalType(tup.At(i).Type()) {
return true
}
}
return false
}

func isInternalType(typ types.Type) bool {
s := types.TypeString(typ, nil)
if strings.HasPrefix(s, "internal.") {
return true
}
if strings.Contains(s, "/internal.") {
return true
}
if strings.Contains(s, "/internal/") {
return true
}
if strings.HasPrefix(s, "main.") {
return true
}
return strings.Contains(s, "/main.")
}

// This takes an interface and flattens its typelists by traversing embeds.
func termsOf(typ types.Type) []*types.Term {
var res []*types.Term
Expand Down

0 comments on commit c85f9c4

Please sign in to comment.