Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When an interface has unexported methods, adding a method is minor, not major #29

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions testdata/minor/addmethod1.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// -*- mode: go -*-

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

type X interface {
A() int
unexported()
}
// {{ end }}

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

type X interface {
A() int
B() string
unexported()
}
// {{ end }}
34 changes: 34 additions & 0 deletions testdata/minor/addmethod2.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// -*- mode: go -*-

// {{ define "older/internal/q.go" }}
package internal

type Q int
// {{ end }}

// {{ define "newer/internal/q.go" }}
package internal

type Q int
// {{ end }}

// {{ define "older" }}
package addmethod2

import "addmethod2/internal"

type X interface {
A() internal.Q
}
// {{ end }}

// {{ define "newer" }}
package addmethod2

import "addmethod2/internal"

type X interface {
A() internal.Q
B() string
}
// {{ end }}
63 changes: 62 additions & 1 deletion 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,7 +238,14 @@ func (c *comparer) compareInterfaces(older, newer *types.Interface) Result {

if c.implements(newer, older) {
if !c.implements(older, newer) {
res = rwrapf(Major, "new interface %s is a superset of older", newer)
switch {
case anyUnexportedMethods(older):
res = rwrapf(Minor, "new interface %s is a superset of older, with unexported methods", newer)
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)
}
}
} else {
return rwrapf(Major, "new interface %s does not implement old", newer)
Expand Down Expand Up @@ -302,6 +310,59 @@ func (c *comparer) compareInterfaces(older, newer *types.Interface) Result {
return rwrapf(Major, "constraint type unions differ")
}

func anyUnexportedMethods(intf *types.Interface) bool {
for i := 0; i < intf.NumMethods(); i++ {
if !ast.IsExported(intf.Method(i).Name()) {
return true
}
}
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
Loading