Skip to content

Commit

Permalink
Fix param type overwriting in type propagation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Mar 21, 2023
1 parent b3fb3b9 commit fb81647
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ func (v *visitor) checkFunc(name string, fn reflect.Type, method bool, node *ast
in = fn.In(i + fnInOffset)
}

if isIntegerOrArithmeticOperation(arg) {
if isIntegerOrArithmeticOperation(arg) && (isInteger(in) || isFloat(in)) {
t = in
setTypeForIntegers(arg, t)
}
Expand Down
40 changes: 40 additions & 0 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@ cannot use int to get an element from map[string]interface {} (1:10)
invalid operation: + (mismatched types int and string) (1:13)
| 1 /* one */ + "2"
| ............^
FuncTyped(42)
cannot use int as argument (type string) to call FuncTyped (1:11)
| FuncTyped(42)
| ..........^
`

func TestCheck_error(t *testing.T) {
Expand Down Expand Up @@ -893,3 +898,38 @@ func TestCheck_dont_panic_on_nil_arguments_for_builtins(t *testing.T) {
})
}
}

func TestCheck_do_not_override_params_for_functions(t *testing.T) {
env := map[string]interface{}{
"foo": func(p string) string {
return "foo"
},
}
config := conf.New(env)
expr.Function(
"bar",
func(p ...interface{}) (interface{}, error) {
return p[0].(string), nil
},
new(func(string) string),
)(config)
config.Check()

t.Run("func from env", func(t *testing.T) {
tree, err := parser.Parse("foo(1)")
require.NoError(t, err)

_, err = checker.Check(tree, config)
require.Error(t, err)
require.Contains(t, err.Error(), "cannot use int as argument")
})

t.Run("func from function", func(t *testing.T) {
tree, err := parser.Parse("bar(1)")
require.NoError(t, err)

_, err = checker.Check(tree, config)
require.Error(t, err)
require.Contains(t, err.Error(), "cannot use int as argument")
})
}

0 comments on commit fb81647

Please sign in to comment.