Skip to content

Commit 9009d4d

Browse files
committed
Fix CallFastTyped detection
1 parent 5366e57 commit 9009d4d

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

checker/checker.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -583,11 +583,11 @@ func (v *visitor) checkFunc(fn reflect.Type, method bool, node *ast.CallNode, na
583583
continue funcTypes
584584
}
585585
}
586-
if typed.NumIn() != len(arguments) {
586+
if typed.NumIn() != fn.NumIn() {
587587
continue
588588
}
589-
for j, arg := range arguments {
590-
if typed.In(j) != arg.Type() {
589+
for j := 0; j < typed.NumIn(); j++ {
590+
if typed.In(j) != fn.In(j) {
591591
continue funcTypes
592592
}
593593
}

checker/checker_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -703,3 +703,22 @@ func TestCheck_TypeWeights(t *testing.T) {
703703
}
704704
}
705705
}
706+
707+
func TestCheck_CallFastTyped(t *testing.T) {
708+
env := map[string]interface{}{
709+
"fn": func([]interface{}, string) string {
710+
return "foo"
711+
},
712+
}
713+
714+
tree, err := parser.Parse("fn([1, 2], 'bar')")
715+
require.NoError(t, err)
716+
717+
config := conf.New(env)
718+
719+
_, err = checker.Check(tree, config)
720+
require.NoError(t, err)
721+
722+
require.False(t, tree.Node.(*ast.CallNode).Fast)
723+
require.Equal(t, 22, tree.Node.(*ast.CallNode).Typed)
724+
}

expr_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,24 @@ func TestCompile_allow_to_use_interface_to_get_an_element_from_map(t *testing.T)
16301630
})
16311631
}
16321632

1633+
func TestFastCall(t *testing.T) {
1634+
env := map[string]interface{}{
1635+
"func": func(in interface{}) float64 {
1636+
return 8
1637+
},
1638+
}
1639+
code := `func("8")`
1640+
1641+
program, err := expr.Compile(code, expr.Env(env))
1642+
assert.NoError(t, err)
1643+
1644+
out, err := expr.Run(program, env)
1645+
assert.NoError(t, err)
1646+
assert.Equal(t, float64(8), out)
1647+
}
1648+
16331649
// Mock types
1650+
16341651
type mockEnv struct {
16351652
Any interface{}
16361653
Int, One, Two, Three int

0 commit comments

Comments
 (0)