Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ func TestExpr(t *testing.T) {
},
{
`Int32 + Int64`,
0,
int64(0),
},
{
`Float64 + 0`,
Expand Down
58 changes: 58 additions & 0 deletions test/issues/804/issue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package issue_test

import (
"math"
"testing"

"github.com/expr-lang/expr"
"github.com/expr-lang/expr/internal/testify/require"
)

// TestIssue804 verifies that arithmetic operations on int64 and uint64
// operands preserve 64-bit precision instead of truncating to int
// (which is 32-bit on 32-bit architectures).
func TestIssue804(t *testing.T) {
env := map[string]any{
"a": int64(math.MaxInt32),
"b": int64(1),
}

// int64 + int64 must produce int64, not int.
out, err := expr.Eval("a + b", env)
require.NoError(t, err)
require.Equal(t, int64(math.MaxInt32)+1, out)

// int64 - int64
out, err = expr.Eval("a - b", env)
require.NoError(t, err)
require.Equal(t, int64(math.MaxInt32)-1, out)

// int64 * int64
out, err = expr.Eval("a * b", env)
require.NoError(t, err)
require.Equal(t, int64(math.MaxInt32), out)
}

func TestIssue804_uint64(t *testing.T) {
env := map[string]any{
"a": uint64(math.MaxUint32),
"b": uint64(1),
}

// uint64 + uint64 must produce uint64, not int.
out, err := expr.Eval("a + b", env)
require.NoError(t, err)
require.Equal(t, uint64(math.MaxUint32)+1, out)
}

func TestIssue804_mixed(t *testing.T) {
env := map[string]any{
"a": int32(100),
"b": int64(math.MaxInt32),
}

// int32 + int64 must produce int64.
out, err := expr.Eval("a + b", env)
require.NoError(t, err)
require.Equal(t, int64(100)+int64(math.MaxInt32), out)
}
24 changes: 23 additions & 1 deletion vm/runtime/helpers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ func cases(op string, xs ...[]string) string {
echo(`switch y := b.(type) {`)
for _, b := range types {
t := "int"
if (isInt64(a) || isInt64(b)) && isSigned(a) && isSigned(b) {
t = "int64"
}
if (isUint64(a) || isUint64(b)) && isUnsigned(a) && isUnsigned(b) {
t = "uint64"
}
if isDuration(a) || isDuration(b) {
t = "time.Duration"
}
Expand Down Expand Up @@ -133,6 +139,22 @@ func isFloat(t string) bool {
return strings.HasPrefix(t, "float")
}

func isInt64(t string) bool {
return t == "int64"
}

func isUint64(t string) bool {
return t == "uint64"
}

func isSigned(t string) bool {
return t == "int" || t == "int8" || t == "int16" || t == "int32" || t == "int64"
}

func isUnsigned(t string) bool {
return t == "uint" || t == "uint8" || t == "uint16" || t == "uint32" || t == "uint64"
}

func isDuration(t string) bool {
return t == "time.Duration"
}
Expand Down Expand Up @@ -323,7 +345,7 @@ func Divide(a, b interface{}) float64 {
panic(fmt.Sprintf("invalid operation: %T / %T", a, b))
}

func Modulo(a, b interface{}) int {
func Modulo(a, b interface{}) interface{} {
switch x := a.(type) {
{{ cases_int_only "%" }}
}
Expand Down
Loading