Skip to content

Commit

Permalink
Fix type checker for float arguments
Browse files Browse the repository at this point in the history
Fixes #474
  • Loading branch information
antonmedv committed Nov 24, 2023
1 parent 939aca1 commit c7038e8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
54 changes: 54 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 0 additions & 2 deletions test/fuzz/fuzz_corpus.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Binary file modified test/fuzz/fuzz_expr_seed_corpus.zip
Binary file not shown.
2 changes: 0 additions & 2 deletions testdata/examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c7038e8

Please sign in to comment.