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
4 changes: 4 additions & 0 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -992,6 +993,9 @@ var Builtins = []*Function{
}

size := v.Len()
if strconv.IntSize == 32 {
return uniq32(v, size)
}
ret := []any{}
// Scan small sets linearly and index them once they grow.
var index *uniqIndex
Expand Down
145 changes: 144 additions & 1 deletion builtin/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ func containsEqual(values []any, value any) bool {
// strings and numbers. Strings only equal strings. Integers equal integers
// via int(x) and floats via float64(x), so integers are also keyed as
// float64 for float lookups. Everything else falls back to a scan.
//
// amd64 uses this index. On 386, signed int64 equality is wider than int, so
// uniq uses uniqIndex32 instead.
type uniqIndex struct {
strings map[string]struct{}
ints map[int]struct{}
Expand Down Expand Up @@ -159,7 +162,8 @@ func (u *uniqIndex) add(value any) {
}
}

// intKeys mirrors the conversions runtime.Equal applies to integers.
// intKeys mirrors the conversions runtime.Equal applies to integers when int
// is 64 bits. On 386, signed pairs that include an int64 compare as int64.
func intKeys(value any) (int, float64, bool) {
switch x := value.(type) {
case int:
Expand All @@ -186,6 +190,145 @@ func intKeys(value any) (int, float64, bool) {
return 0, 0, false
}

// uniqIndex32 matches runtime.Equal on GOARCH=386, where signed integers that
// include an int64 compare as int64 and mixed signedness still compares as int.
type uniqIndex32 struct {
strings map[string]struct{}
signed map[int64]struct{}
signedAsInt map[int]struct{}
unsigned map[int]struct{}
intFloats map[float64]struct{}
floats map[float64]struct{}
other []any
}

func newUniqIndex32(values []any) *uniqIndex32 {
u := &uniqIndex32{
strings: map[string]struct{}{},
signed: map[int64]struct{}{},
signedAsInt: map[int]struct{}{},
unsigned: map[int]struct{}{},
intFloats: map[float64]struct{}{},
floats: map[float64]struct{}{},
}
for _, v := range values {
u.add(v)
}
return u
}

func (u *uniqIndex32) has(value any) bool {
if s, ok := value.(string); ok {
_, found := u.strings[s]
return found
}
if k, ok := numberKey32(value); ok {
if k.signed {
if _, found := u.signed[k.s]; found {
return true
}
if _, found := u.unsigned[k.trunc]; found {
return true
}
} else {
if _, found := u.unsigned[k.trunc]; found {
return true
}
if _, found := u.signedAsInt[k.trunc]; found {
return true
}
}
_, found := u.floats[k.float]
return found
}
if f, ok := floatKey(value); ok {
_, found := u.floats[f]
if !found {
_, found = u.intFloats[f]
}
return found
}
return containsEqual(u.other, value)
}

func (u *uniqIndex32) add(value any) {
if s, ok := value.(string); ok {
u.strings[s] = struct{}{}
return
}
if k, ok := numberKey32(value); ok {
u.intFloats[k.float] = struct{}{}
if k.signed {
u.signed[k.s] = struct{}{}
u.signedAsInt[k.trunc] = struct{}{}
return
}
u.unsigned[k.trunc] = struct{}{}
return
}
if f, ok := floatKey(value); ok {
u.floats[f] = struct{}{}
return
}
u.other = append(u.other, value)
}

type integerKey32 struct {
signed bool
s int64
trunc int
float float64
}

func numberKey32(value any) (integerKey32, bool) {
switch x := value.(type) {
case int:
return integerKey32{signed: true, s: int64(x), trunc: x, float: float64(x)}, true
case int8:
return integerKey32{signed: true, s: int64(x), trunc: int(x), float: float64(x)}, true
case int16:
return integerKey32{signed: true, s: int64(x), trunc: int(x), float: float64(x)}, true
case int32:
return integerKey32{signed: true, s: int64(x), trunc: int(x), float: float64(x)}, true
case int64:
return integerKey32{signed: true, s: x, trunc: int(x), float: float64(x)}, true
case uint:
return integerKey32{trunc: int(x), float: float64(x)}, true
case uint8:
return integerKey32{trunc: int(x), float: float64(x)}, true
case uint16:
return integerKey32{trunc: int(x), float: float64(x)}, true
case uint32:
return integerKey32{trunc: int(x), float: float64(x)}, true
case uint64:
return integerKey32{trunc: int(x), float: float64(x)}, true
}
return integerKey32{}, false
}

// uniq32 is uniq on GOARCH=386, where signed int64 values must not share an
// int key. strconv.IntSize == 32 is constant, so amd64 does not call this.
func uniq32(v reflect.Value, size int) (any, error) {
ret := []any{}
var index *uniqIndex32
for i := 0; i < size; i++ {
value := v.Index(i).Interface()
if index == nil && len(ret) >= 16 {
index = newUniqIndex32(ret)
}
if index != nil {
if index.has(value) {
continue
}
index.add(value)
} else if containsEqual(ret, value) {
continue
}
ret = append(ret, value)
}
return ret, nil
}

func floatKey(value any) (float64, bool) {
switch x := value.(type) {
case float32:
Expand Down
25 changes: 21 additions & 4 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"reflect"
"regexp"
"strconv"
"time"

"github.com/expr-lang/expr/ast"
Expand Down Expand Up @@ -454,9 +455,14 @@ func (v *Checker) binaryNode(node *ast.BinaryNode) Nature {

case "%":
if l.IsInteger && r.IsInteger {
return v.config.NtCache.FromType(intType)
return l.PromoteNumericNature(&v.config.NtCache, r)
}
if l.MaybeCompatible(&v.config.NtCache, r, IntegerCheck) {
// One operand is any. On 32-bit the runtime value may be int64,
// so claiming int would make `==` emit OpEqualInt and panic.
if strconv.IntSize == 32 {
return Nature{}
}
return v.config.NtCache.FromType(intType)
}

Expand Down Expand Up @@ -741,7 +747,7 @@ func (v *Checker) callNode(node *ast.CallNode) Nature {
}

if nt.Kind == reflect.Func {
outType, err := v.checkArguments(fnName, nt, node.Arguments, node)
outType, err := v.checkArguments(fnName, nt, node.Arguments, node, true)
if err != nil {
if v.err == nil {
v.err = err
Expand Down Expand Up @@ -1071,7 +1077,7 @@ func (v *Checker) checkFunction(f *builtin.Function, node ast.Node, arguments []
}
return v.config.NtCache.FromType(t)
} else if len(f.Types) == 0 {
nt, err := v.checkArguments(f.Name, v.config.NtCache.FromType(f.Type()), arguments, node)
nt, err := v.checkArguments(f.Name, v.config.NtCache.FromType(f.Type()), arguments, node, false)
if err != nil {
if v.err == nil {
v.err = err
Expand All @@ -1087,7 +1093,7 @@ func (v *Checker) checkFunction(f *builtin.Function, node ast.Node, arguments []
}
var lastErr *file.Error
for _, t := range f.Types {
outNature, err := v.checkArguments(f.Name, v.config.NtCache.FromType(t), arguments, node)
outNature, err := v.checkArguments(f.Name, v.config.NtCache.FromType(t), arguments, node, false)
if err != nil {
lastErr = err
continue
Expand Down Expand Up @@ -1117,6 +1123,7 @@ func (v *Checker) checkArguments(
fn Nature,
arguments []ast.Node,
node ast.Node,
reflectCall bool,
) (Nature, *file.Error) {
if fn.IsUnknown(&v.config.NtCache) {
return Nature{}, nil
Expand Down Expand Up @@ -1200,6 +1207,16 @@ func (v *Checker) checkArguments(

if in.IsInteger && argNature.IsInteger && argNature.Kind != in.Kind {
traverseAndReplaceIntegerNodesWithIntegerNodes(&arguments[i], in)
// Only real Go funcs (reflect.Call or OpCallTyped) assert .(int).
// Builtins and expr.Function take ...any and convert with ToInt.
if reflectCall && strconv.IntSize == 32 {
if got := arguments[i].Type(); got != nil && in.Type != nil && got.Kind() == reflect.Int64 && !got.AssignableTo(in.Type) {
return Nature{}, &file.Error{
Location: arguments[i].Location(),
Message: fmt.Sprintf("cannot use %s as argument (type %s) to call %v ", got, in.String(), name),
}
}
}
continue
}

Expand Down
22 changes: 21 additions & 1 deletion checker/nature/nature.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nature
import (
"fmt"
"reflect"
"strconv"
"time"

"github.com/expr-lang/expr/builtin"
Expand All @@ -11,6 +12,7 @@ import (

var (
intType = reflect.TypeOf(0)
int64Type = reflect.TypeOf(int64(0))
floatType = reflect.TypeOf(float64(0))
arrayType = reflect.TypeOf([]any{})
byteSliceType = reflect.TypeOf([]byte{})
Expand Down Expand Up @@ -484,7 +486,25 @@ func (n *Nature) PromoteNumericNature(c *Cache, rhs Nature) Nature {
if n.IsFloat || rhs.IsFloat {
return c.FromType(floatType)
}
return c.FromType(intType)
return c.FromType(integerArithType(n.Kind, rhs.Kind))
}

// integerArithType matches the runtime helpers. int64 arithmetic stays int64
// only when int is 32 bits; otherwise the result is int, as on amd64 master.
func integerArithType(a, b reflect.Kind) reflect.Type {
if strconv.IntSize == 32 && signedKind(a) && signedKind(b) && (a == reflect.Int64 || b == reflect.Int64) {
return int64Type
}
return intType
}

func signedKind(k reflect.Kind) bool {
switch k {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return true
default:
return false
}
}

func (n *Nature) IsTime() bool {
Expand Down
12 changes: 11 additions & 1 deletion expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"reflect"
"strconv"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -712,6 +713,15 @@ func TestExpr_readme_example(t *testing.T) {
require.Equal(t, "Hello, world!", output)
}

// int32PlusInt64Result is int on 64-bit and int64 on 32-bit, matching
// signed arithmetic that includes an int64.
func int32PlusInt64Result() any {
if strconv.IntSize == 32 {
return int64(0)
}
return 0
}

func TestExpr(t *testing.T) {
date := time.Date(2017, time.October, 23, 18, 30, 0, 0, time.UTC)
oneDay, _ := time.ParseDuration("24h")
Expand Down Expand Up @@ -813,7 +823,7 @@ func TestExpr(t *testing.T) {
},
{
`Int32 + Int64`,
0,
int32PlusInt64Result(),
},
{
`Float64 + 0`,
Expand Down
Loading