From 4b4cb23035ba67ba08b6fa20b3bb2be7e6246388 Mon Sep 17 00:00:00 2001 From: deelawn Date: Thu, 2 May 2024 12:32:25 -0700 Subject: [PATCH 1/4] default untyped to uint when necessary --- gnovm/pkg/gnolang/preprocess.go | 2 +- gnovm/pkg/gnolang/types.go | 19 ++++++++++++++----- gnovm/pkg/gnolang/values.go | 2 +- gnovm/pkg/gnolang/values_conversions.go | 2 +- gnovm/tests/file_test.go | 2 +- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 64fe1fbff45..25eea40b446 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -2437,7 +2437,7 @@ func checkOrConvertType(store Store, last BlockNode, x *Expr, t Type, autoNative } if isUntyped(xt) { if t == nil { - t = defaultTypeOf(xt) + t = defaultTypeOf(xt, nil) } // Push type into expr if qualifying binary expr. if bx, ok := (*x).(*BinaryExpr); ok { diff --git a/gnovm/pkg/gnolang/types.go b/gnovm/pkg/gnolang/types.go index e1814e8f243..5cfc1590c87 100644 --- a/gnovm/pkg/gnolang/types.go +++ b/gnovm/pkg/gnolang/types.go @@ -1166,8 +1166,8 @@ func (ft *FuncType) Specify(store Store, argTVs []TypedValue, isVarg bool) *Func continue } else if vargt == nil { vargt = varg.T - } else if isUntyped(varg.T) && vargt.TypeID() == defaultTypeOf(varg.T).TypeID() { - vargt = defaultTypeOf(varg.T) + } else if isUntyped(varg.T) && vargt.TypeID() == defaultTypeOf(varg.T, varg.V).TypeID() { + vargt = defaultTypeOf(varg.T, varg.V) } else if vargt.TypeID() != varg.T.TypeID() { panic(fmt.Sprintf( "incompatible varg types: expected %v, got %s", @@ -2204,14 +2204,23 @@ func isDataByte(t Type) bool { // TODO move untyped const stuff to preprocess.go. // TODO associate with ConvertTo() in documentation. -func defaultTypeOf(t Type) Type { +func defaultTypeOf(t Type, v Value) Type { switch t { case UntypedBoolType: return BoolType case UntypedRuneType: return Int32Type case UntypedBigintType: - return IntType + typeVal := IntType + if bigintValue, ok := v.(BigintValue); ok { + if bigintValue.V.Sign() == 1 && !bigintValue.V.IsInt64() { + // Use an unsigned type if the value is positive and we know + // it won't fit in an int64. + typeVal = Uint64Type + } + } + + return typeVal case UntypedBigdecType: return Float64Type case UntypedStringType: @@ -2437,7 +2446,7 @@ func specifyType(store Store, lookup map[Name]Type, tmpl Type, spec Type, specTy return // ok } else { if isUntyped(spec) { - spec = defaultTypeOf(spec) + spec = defaultTypeOf(spec, nil) } lookup[ct.Generic] = spec return // ok diff --git a/gnovm/pkg/gnolang/values.go b/gnovm/pkg/gnolang/values.go index ae5ac7fd40b..26e25cd13a6 100644 --- a/gnovm/pkg/gnolang/values.go +++ b/gnovm/pkg/gnolang/values.go @@ -1594,7 +1594,7 @@ func (tv *TypedValue) Assign(alloc *Allocator, tv2 TypedValue, cu bool) { } *tv = tv2.Copy(alloc) if cu && isUntyped(tv.T) { - ConvertUntypedTo(tv, defaultTypeOf(tv.T)) + ConvertUntypedTo(tv, defaultTypeOf(tv.T, tv.V)) } } diff --git a/gnovm/pkg/gnolang/values_conversions.go b/gnovm/pkg/gnolang/values_conversions.go index b4888878c7a..edaa8883819 100644 --- a/gnovm/pkg/gnolang/values_conversions.go +++ b/gnovm/pkg/gnolang/values_conversions.go @@ -926,7 +926,7 @@ func ConvertUntypedTo(tv *TypedValue, t Type) { } // general case if t == nil { - t = defaultTypeOf(tv.T) + t = defaultTypeOf(tv.T, tv.V) } switch tv.T { case UntypedBoolType: diff --git a/gnovm/tests/file_test.go b/gnovm/tests/file_test.go index f070546ab74..19c747923c4 100644 --- a/gnovm/tests/file_test.go +++ b/gnovm/tests/file_test.go @@ -15,7 +15,7 @@ import ( var withSync = flag.Bool("update-golden-tests", false, "rewrite tests updating Realm: and Output: with new values where changed") func TestFileStr(t *testing.T) { - filePath := filepath.Join(".", "files", "str.gno") + filePath := filepath.Join(".", "dylan", "c.gno") runFileTest(t, filePath, WithNativeLibs()) } From 22115fa3c16b0ce1e121edbb84a802e7965ae9f3 Mon Sep 17 00:00:00 2001 From: deelawn Date: Thu, 2 May 2024 12:38:41 -0700 Subject: [PATCH 2/4] revert accidental change --- gnovm/tests/file_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gnovm/tests/file_test.go b/gnovm/tests/file_test.go index 19c747923c4..f070546ab74 100644 --- a/gnovm/tests/file_test.go +++ b/gnovm/tests/file_test.go @@ -15,7 +15,7 @@ import ( var withSync = flag.Bool("update-golden-tests", false, "rewrite tests updating Realm: and Output: with new values where changed") func TestFileStr(t *testing.T) { - filePath := filepath.Join(".", "dylan", "c.gno") + filePath := filepath.Join(".", "files", "str.gno") runFileTest(t, filePath, WithNativeLibs()) } From 7aa15f30c6e3e0725cd995b98eaba3a0b040fd0a Mon Sep 17 00:00:00 2001 From: deelawn Date: Thu, 2 May 2024 12:39:07 -0700 Subject: [PATCH 3/4] add untyped bigint test --- gnovm/tests/files/bigint1.gno | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 gnovm/tests/files/bigint1.gno diff --git a/gnovm/tests/files/bigint1.gno b/gnovm/tests/files/bigint1.gno new file mode 100644 index 00000000000..01032e27b3f --- /dev/null +++ b/gnovm/tests/files/bigint1.gno @@ -0,0 +1,14 @@ +package main + +import ( + "math" +) + +func main() { + println(float64(math.MaxUint32)) + println(float64(math.MaxUint64)) +} + +// Output: +// 4.294967295e+09 +// 1.8446744073709552e+19 From e3a6b034f5301b2498d9a0a62bdc64e205f4cf56 Mon Sep 17 00:00:00 2001 From: deelawn Date: Thu, 2 May 2024 12:43:40 -0700 Subject: [PATCH 4/4] check for nil --- gnovm/pkg/gnolang/types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gnovm/pkg/gnolang/types.go b/gnovm/pkg/gnolang/types.go index 5cfc1590c87..7ea82bfebce 100644 --- a/gnovm/pkg/gnolang/types.go +++ b/gnovm/pkg/gnolang/types.go @@ -2213,7 +2213,7 @@ func defaultTypeOf(t Type, v Value) Type { case UntypedBigintType: typeVal := IntType if bigintValue, ok := v.(BigintValue); ok { - if bigintValue.V.Sign() == 1 && !bigintValue.V.IsInt64() { + if bigintValue.V != nil && bigintValue.V.Sign() == 1 && !bigintValue.V.IsInt64() { // Use an unsigned type if the value is positive and we know // it won't fit in an int64. typeVal = Uint64Type