Skip to content

Commit c7038e8

Browse files
committed
Fix type checker for float arguments
Fixes #474
1 parent 939aca1 commit c7038e8

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

checker/checker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ func (v *checker) checkArguments(name string, fn reflect.Type, method bool, argu
959959
in = fn.In(i + fnInOffset)
960960
}
961961

962-
if isFloat(in) {
962+
if isFloat(in) && isInteger(t) {
963963
traverseAndReplaceIntegerNodesWithFloatNodes(&arguments[i], in)
964964
continue
965965
}

expr_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,3 +2112,57 @@ func TestIssue462(t *testing.T) {
21122112
_, err := expr.Compile(`$env.unknown(int())`, expr.Env(env))
21132113
require.Error(t, err)
21142114
}
2115+
2116+
func TestIssue(t *testing.T) {
2117+
testCases := []struct {
2118+
code string
2119+
fail bool
2120+
}{
2121+
{
2122+
code: `func("invalid")`,
2123+
fail: true,
2124+
},
2125+
{
2126+
code: `func(true)`,
2127+
fail: true,
2128+
},
2129+
{
2130+
code: `func([])`,
2131+
fail: true,
2132+
},
2133+
{
2134+
code: `func({})`,
2135+
fail: true,
2136+
},
2137+
{
2138+
code: `func(1)`,
2139+
fail: false,
2140+
},
2141+
{
2142+
code: `func(1.5)`,
2143+
fail: false,
2144+
},
2145+
}
2146+
2147+
for _, tc := range testCases {
2148+
ltc := tc
2149+
t.Run(ltc.code, func(t *testing.T) {
2150+
t.Parallel()
2151+
function := expr.Function("func", func(params ...any) (any, error) {
2152+
return true, nil
2153+
}, new(func(float64) bool))
2154+
_, err := expr.Compile(ltc.code, function)
2155+
if ltc.fail {
2156+
if err == nil {
2157+
t.Error("expected an error, but it was nil")
2158+
t.FailNow()
2159+
}
2160+
} else {
2161+
if err != nil {
2162+
t.Errorf("expected nil, but it was %v", err)
2163+
t.FailNow()
2164+
}
2165+
}
2166+
})
2167+
}
2168+
}

test/fuzz/fuzz_corpus.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6680,7 +6680,6 @@ half(f32 ** f32)
66806680
half(f32 ** f64)
66816681
half(f32 + 0.5)
66826682
half(f32 + 1)
6683-
half(f32 + f32)
66846683
half(f32 + f64)
66856684
half(f32 + i)
66866685
half(f32 + i32)
@@ -12213,7 +12212,6 @@ ok ? score : foo.String
1221312212
ok ? score : foo?.String
1221412213
ok ? score : greet
1221512214
ok ? score : half
12216-
ok ? score : half("bar")
1221712215
ok ? score : i
1221812216
ok ? score : i32
1221912217
ok ? score : list

test/fuzz/fuzz_expr_seed_corpus.zip

-4 Bytes
Binary file not shown.

testdata/examples.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6680,7 +6680,6 @@ half(f32 ** f32)
66806680
half(f32 ** f64)
66816681
half(f32 + 0.5)
66826682
half(f32 + 1)
6683-
half(f32 + f32)
66846683
half(f32 + f64)
66856684
half(f32 + i)
66866685
half(f32 + i32)
@@ -12213,7 +12212,6 @@ ok ? score : foo.String
1221312212
ok ? score : foo?.String
1221412213
ok ? score : greet
1221512214
ok ? score : half
12216-
ok ? score : half("bar")
1221712215
ok ? score : i
1221812216
ok ? score : i32
1221912217
ok ? score : list

0 commit comments

Comments
 (0)