Skip to content

Commit

Permalink
unused: implement all known instances of generic interface types
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikh committed Jun 30, 2024
1 parent f4ee291 commit 663bb6d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,18 @@ type S8_1 struct{} //@ used("S8_1", true)
func (s *S8_1) m8() []io.Reader { //@ quiet("s"), used("m8", false)
return nil
}

type S8 struct{} //@ used("S8", true)
type I9[T any] interface { //@ used("I9", true), used("T", true)
make() *T //@ used("make", true)
}

type S9 struct{} //@ used("S9", true)

func (S9) make() *S8 { return nil } //@ used("make", true)

func i9use(i I9[S8]) { i.make() } //@ used("i9use", true), used("i", true)

func init() { //@ used("init", true)
i9use(S9{})
}
15 changes: 14 additions & 1 deletion unused/unused.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,19 @@ func (g *graph) entry() {
}
}

// We use a normal map instead of a typeutil.Map because we deduplicate
// these on a best effort basis, as an optimization.
allInterfaces := make(map[*types.Interface]struct{})
for _, typ := range g.interfaceTypes {
allInterfaces[typ] = struct{}{}
}
for _, ins := range g.info.Instances {
if typ, ok := ins.Type.(*types.Named); ok && typ.Obj().Pkg() == g.pkg {
if iface, ok := typ.Underlying().(*types.Interface); ok {
allInterfaces[iface] = struct{}{}
}
}
}
processMethodSet := func(named *types.TypeName, ms *types.MethodSet) {
if g.opts.ExportedIsUsed {
for i := 0; i < ms.Len(); i++ {
Expand All @@ -552,7 +565,7 @@ func (g *graph) entry() {
// (8.0) handle interfaces
//
// We don't care about interfaces implementing interfaces; all their methods are already used, anyway
for _, iface := range g.interfaceTypes {
for iface := range allInterfaces {
if sels, ok := implements(named.Type(), iface, ms); ok {
for _, sel := range sels {
// (8.2) any concrete type implements all known interfaces
Expand Down

0 comments on commit 663bb6d

Please sign in to comment.