Skip to content

proposal: go/types: export API for unification #63982

@dominikh

Description

@dominikh

go/types should offer API for type unification, to allow clients to unify types that the type-checker hasn't already unified while type-checking code.

Staticcheck has a use for this as part of the unused check, to see if types implement possible instantiations of generic interfaces. For example, given

type Iface[T any] interface {
    foo() []T
    bar() T
}

type T1 struct{}
func (T1) foo() []int
func (T1) bar() int

type T2 struct{}
func (T2) foo() []int
func (T2) bar() string

we want to find out that T1 implements Iface[int] and that T2 doesn't implement any possible instantiation of Iface. For that, we would try to unify T1.foo and T1.bar with Iface.foo and Iface.bar, under a shared mapping of type parameters, and similarly do the same for T2.

As such, the API should allow unifying multiple pairs of types using the same mapping of type parameters to types, report success/failure, and allow querying the mapping.

/cc @findleyr

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions