Skip to content

Commit

Permalink
pattern: make sure custom Matchers also benefit from automatic unnesting
Browse files Browse the repository at this point in the history
(cherry picked from commit 154241a)
  • Loading branch information
dominikh committed Feb 19, 2021
1 parent 5d0f4f0 commit b720550
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions pattern/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,22 +446,24 @@ func (Nil) Match(m *Matcher, node interface{}) (interface{}, bool) {
}

func (builtin Builtin) Match(m *Matcher, node interface{}) (interface{}, bool) {
ident, ok := node.(*ast.Ident)
r, ok := match(m, Ident(builtin), node)
if !ok {
return nil, false
}
ident := r.(*ast.Ident)
obj := m.TypesInfo.ObjectOf(ident)
if obj != types.Universe.Lookup(ident.Name) {
return nil, false
}
return match(m, builtin.Name, ident.Name)
return ident.Name, true
}

func (obj Object) Match(m *Matcher, node interface{}) (interface{}, bool) {
ident, ok := node.(*ast.Ident)
r, ok := match(m, Ident(obj), node)
if !ok {
return nil, false
}
ident := r.(*ast.Ident)

id := m.TypesInfo.ObjectOf(ident)
_, ok = match(m, obj.Name, ident.Name)
Expand All @@ -471,9 +473,15 @@ func (obj Object) Match(m *Matcher, node interface{}) (interface{}, bool) {
func (fn Function) Match(m *Matcher, node interface{}) (interface{}, bool) {
var name string
var obj types.Object
switch node := node.(type) {

r, ok := match(m, Or{Nodes: []Node{Ident{Any{}}, SelectorExpr{Any{}, Any{}}}}, node)
if !ok {
return nil, false
}

switch r := r.(type) {
case *ast.Ident:
obj = m.TypesInfo.ObjectOf(node)
obj = m.TypesInfo.ObjectOf(r)
switch obj := obj.(type) {
case *types.Func:
// OPT(dh): optimize this similar to code.FuncName
Expand All @@ -485,16 +493,16 @@ func (fn Function) Match(m *Matcher, node interface{}) (interface{}, bool) {
}
case *ast.SelectorExpr:
var ok bool
obj, ok = m.TypesInfo.ObjectOf(node.Sel).(*types.Func)
obj, ok = m.TypesInfo.ObjectOf(r.Sel).(*types.Func)
if !ok {
return nil, false
}
// OPT(dh): optimize this similar to code.FuncName
name = obj.(*types.Func).FullName()
default:
return nil, false
panic("unreachable")
}
_, ok := match(m, fn.Name, name)
_, ok = match(m, fn.Name, name)
return obj, ok
}

Expand Down

0 comments on commit b720550

Please sign in to comment.