Skip to content

go/types: Interface.Method and ExplicitMethod are not ordered by Id, contra docs #81875

Closed

Description

@adonovan

(Found in passing.)

The docs for types.Interface.Method say "The methods are ordered by their unique Id"; so do those for ExplicitMethod. But since https://go.dev/cl/461677 they're actually sorted with exported names before unexported ones. They differ when an interface has unexported methods from more than one package, which happens when it embeds an interface from another package:

package q

type I interface{ a() }
package p

import "q"

type J interface {
	q.I
	b()
}

Program (type-checks both packages and prints the methods of J):

iface := J.Underlying().(*types.Interface)
for i := range iface.NumMethods() {
	fmt.Println(iface.Method(i).Id())
}
mset := types.NewMethodSet(J)
for i := range mset.Len() {
	fmt.Println(mset.At(i).Obj().Id())
}

Prints:

Interface.Method order:
	q.a
	p.b
MethodSet order:
	p.b
	q.a

Interface.Method yields q.a before p.b, although "p.b" < "q.a".
MethodSet, whose documentation also promises Id order, does sort by Id.

They can also differ for exported versus unexported names, when a package path
sorts before an exported name (e.g. path "M", methods Z and a: Id order is
M.a, Z, but Interface.Method yields Z first). That's rare in practice.

I'm not sure whether it's safe the order back; we may need to just document the actual order.

Activity

  1. dmitshur commented on Oct 5, 2026

    @dmitshur
    Member
  2. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    on Oct 5, 2026
  3. added this to the Backlog milestone on Oct 5, 2026
  4. gopherbot commented on Oct 8, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/847325 mentions this issue: go/types, types2: document actual Interface.Method order

  5. added
    FixPendingIssues that have a fix which has not yet been reviewed or submitted.
    and removed
    NeedsFixThe path to resolution is known, but the work has not been done.
    on Oct 9, 2026
  6. modified the milestones: Backlog, Go1.28 on Oct 9, 2026
  7. added a commit that references this issue on Oct 9, 2026
    e5f7ecd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FixPendingIssues that have a fix which has not yet been reviewed or submitted.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions