Skip to content

Is it possible to match implementors of generic interfaces? #476

@ian-h-chamberlain

Description

@ian-h-chamberlain

Here's a small example of what I'm trying to do. Given this code:

type CustomCompare[T] interface {
  comparable
  GetT() T
}

type impl struct{}

func (impl) GetT() int {
  return 0
}

func main() {
  x := impl{}
  y := impl{}
  fmt.Println(x == y)
}

I'd like to write a rule kinda like this:

func CustomComparer(m dsl.Matcher) {
	m.Match(`$x == $y`).
		Where(m["x"].Type.Implements("CustomCompare"))
}

I'd imagine this would require some support from the DSL, since I'm not specifying T in my rule and in practice impl only implements CustomCompare[int]... There might be multiple possible use cases, although for my purposes I think either of the first two would be fine:

  1. Implements("CustomCompare") meaning the type implements CustomCompare[T] for at least one T. Maybe could also use syntax like "CustomCompare[$_]" or something
  2. Implements("CustomCompare[$t]") meaning the type implements CustomCompare[T] for a specific T based on some other matcher, e.g. maybe some syntax like:
    m.Match(`$x == $y`).Where(m["x"].Type.Implements("CustomCompare[$y.Type]"))
  3. Implements("CustomCompare[int]") - implements a concrete interface

It might also be useful to have HasMethod work in a similar way, although I'm not sure of the technical differences between how those are implemented.

Is something like this possible? Or if not currently, is there any workaround I could use for a less-precise way of getting similar functionality?

If not, where in the code would I look if I were to try adding this feature?

Thanks in advance! go-ruleguard has been a big help in my organization so I appreciate the work you've done here!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions