Fix type checker for float arguments

Fixes #474
This commit is contained in:
Anton Medvedev
2023-11-24 16:50:09 +01:00
parent e817dfb8a8
commit 885c769c65
5 changed files with 55 additions and 5 deletions
+1 -1
View File
@@ -959,7 +959,7 @@ func (v *checker) checkArguments(name string, fn reflect.Type, method bool, argu
in = fn.In(i + fnInOffset)
}
if isFloat(in) {
if isFloat(in) && isInteger(t) {
traverseAndReplaceIntegerNodesWithFloatNodes(&arguments[i], in)
continue
}
+54
View File
@@ -2112,3 +2112,57 @@ func TestIssue462(t *testing.T) {
_, err := expr.Compile(`$env.unknown(int())`, expr.Env(env))
require.Error(t, err)
}
func TestIssue(t *testing.T) {
testCases := []struct {
code string
fail bool
}{
{
code: `func("invalid")`,
fail: true,
},
{
code: `func(true)`,
fail: true,
},
{
code: `func([])`,
fail: true,
},
{
code: `func({})`,
fail: true,
},
{
code: `func(1)`,
fail: false,
},
{
code: `func(1.5)`,
fail: false,
},
}
for _, tc := range testCases {
ltc := tc
t.Run(ltc.code, func(t *testing.T) {
t.Parallel()
function := expr.Function("func", func(params ...any) (any, error) {
return true, nil
}, new(func(float64) bool))
_, err := expr.Compile(ltc.code, function)
if ltc.fail {
if err == nil {
t.Error("expected an error, but it was nil")
t.FailNow()
}
} else {
if err != nil {
t.Errorf("expected nil, but it was %v", err)
t.FailNow()
}
}
})
}
}
-2
View File
@@ -6680,7 +6680,6 @@ half(f32 ** f32)
half(f32 ** f64)
half(f32 + 0.5)
half(f32 + 1)
half(f32 + f32)
half(f32 + f64)
half(f32 + i)
half(f32 + i32)
@@ -12213,7 +12212,6 @@ ok ? score : foo.String
ok ? score : foo?.String
ok ? score : greet
ok ? score : half
ok ? score : half("bar")
ok ? score : i
ok ? score : i32
ok ? score : list
Binary file not shown.
-2
View File
@@ -6680,7 +6680,6 @@ half(f32 ** f32)
half(f32 ** f64)
half(f32 + 0.5)
half(f32 + 1)
half(f32 + f32)
half(f32 + f64)
half(f32 + i)
half(f32 + i32)
@@ -12213,7 +12212,6 @@ ok ? score : foo.String
ok ? score : foo?.String
ok ? score : greet
ok ? score : half
ok ? score : half("bar")
ok ? score : i
ok ? score : i32
ok ? score : list