Skip to content

Commit fb81647

Browse files
committed
Fix param type overwriting in type propagation logic
1 parent b3fb3b9 commit fb81647

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

checker/checker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ func (v *visitor) checkFunc(name string, fn reflect.Type, method bool, node *ast
639639
in = fn.In(i + fnInOffset)
640640
}
641641

642-
if isIntegerOrArithmeticOperation(arg) {
642+
if isIntegerOrArithmeticOperation(arg) && (isInteger(in) || isFloat(in)) {
643643
t = in
644644
setTypeForIntegers(arg, t)
645645
}

checker/checker_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,11 @@ cannot use int to get an element from map[string]interface {} (1:10)
484484
invalid operation: + (mismatched types int and string) (1:13)
485485
| 1 /* one */ + "2"
486486
| ............^
487+
488+
FuncTyped(42)
489+
cannot use int as argument (type string) to call FuncTyped (1:11)
490+
| FuncTyped(42)
491+
| ..........^
487492
`
488493

489494
func TestCheck_error(t *testing.T) {
@@ -893,3 +898,38 @@ func TestCheck_dont_panic_on_nil_arguments_for_builtins(t *testing.T) {
893898
})
894899
}
895900
}
901+
902+
func TestCheck_do_not_override_params_for_functions(t *testing.T) {
903+
env := map[string]interface{}{
904+
"foo": func(p string) string {
905+
return "foo"
906+
},
907+
}
908+
config := conf.New(env)
909+
expr.Function(
910+
"bar",
911+
func(p ...interface{}) (interface{}, error) {
912+
return p[0].(string), nil
913+
},
914+
new(func(string) string),
915+
)(config)
916+
config.Check()
917+
918+
t.Run("func from env", func(t *testing.T) {
919+
tree, err := parser.Parse("foo(1)")
920+
require.NoError(t, err)
921+
922+
_, err = checker.Check(tree, config)
923+
require.Error(t, err)
924+
require.Contains(t, err.Error(), "cannot use int as argument")
925+
})
926+
927+
t.Run("func from function", func(t *testing.T) {
928+
tree, err := parser.Parse("bar(1)")
929+
require.NoError(t, err)
930+
931+
_, err = checker.Check(tree, config)
932+
require.Error(t, err)
933+
require.Contains(t, err.Error(), "cannot use int as argument")
934+
})
935+
}

0 commit comments

Comments
 (0)