Skip to content

Commit 154241a

Browse files
committed
pattern: make sure custom Matchers also benefit from automatic unnesting
1 parent 5774049 commit 154241a

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

pattern/match.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -446,22 +446,24 @@ func (Nil) Match(m *Matcher, node interface{}) (interface{}, bool) {
446446
}
447447

448448
func (builtin Builtin) Match(m *Matcher, node interface{}) (interface{}, bool) {
449-
ident, ok := node.(*ast.Ident)
449+
r, ok := match(m, Ident(builtin), node)
450450
if !ok {
451451
return nil, false
452452
}
453+
ident := r.(*ast.Ident)
453454
obj := m.TypesInfo.ObjectOf(ident)
454455
if obj != types.Universe.Lookup(ident.Name) {
455456
return nil, false
456457
}
457-
return match(m, builtin.Name, ident.Name)
458+
return ident.Name, true
458459
}
459460

460461
func (obj Object) Match(m *Matcher, node interface{}) (interface{}, bool) {
461-
ident, ok := node.(*ast.Ident)
462+
r, ok := match(m, Ident(obj), node)
462463
if !ok {
463464
return nil, false
464465
}
466+
ident := r.(*ast.Ident)
465467

466468
id := m.TypesInfo.ObjectOf(ident)
467469
_, ok = match(m, obj.Name, ident.Name)
@@ -471,9 +473,15 @@ func (obj Object) Match(m *Matcher, node interface{}) (interface{}, bool) {
471473
func (fn Function) Match(m *Matcher, node interface{}) (interface{}, bool) {
472474
var name string
473475
var obj types.Object
474-
switch node := node.(type) {
476+
477+
r, ok := match(m, Or{Nodes: []Node{Ident{Any{}}, SelectorExpr{Any{}, Any{}}}}, node)
478+
if !ok {
479+
return nil, false
480+
}
481+
482+
switch r := r.(type) {
475483
case *ast.Ident:
476-
obj = m.TypesInfo.ObjectOf(node)
484+
obj = m.TypesInfo.ObjectOf(r)
477485
switch obj := obj.(type) {
478486
case *types.Func:
479487
// OPT(dh): optimize this similar to code.FuncName
@@ -485,16 +493,16 @@ func (fn Function) Match(m *Matcher, node interface{}) (interface{}, bool) {
485493
}
486494
case *ast.SelectorExpr:
487495
var ok bool
488-
obj, ok = m.TypesInfo.ObjectOf(node.Sel).(*types.Func)
496+
obj, ok = m.TypesInfo.ObjectOf(r.Sel).(*types.Func)
489497
if !ok {
490498
return nil, false
491499
}
492500
// OPT(dh): optimize this similar to code.FuncName
493501
name = obj.(*types.Func).FullName()
494502
default:
495-
return nil, false
503+
panic("unreachable")
496504
}
497-
_, ok := match(m, fn.Name, name)
505+
_, ok = match(m, fn.Name, name)
498506
return obj, ok
499507
}
500508

0 commit comments

Comments
 (0)