diff --git a/examples/gno.land/r/gnoland/faucet/z2_filetest.gno b/examples/gno.land/r/gnoland/faucet/z2_filetest.gno index 1791cd91989..9b59d7c07d3 100644 --- a/examples/gno.land/r/gnoland/faucet/z2_filetest.gno +++ b/examples/gno.land/r/gnoland/faucet/z2_filetest.gno @@ -16,11 +16,11 @@ func main() { ) std.TestSetOrigCaller(adminaddr) err := faucet.AdminAddController(controlleraddr1) - if err != nil { + if err != "" { panic(err) } err = faucet.AdminAddController(controlleraddr2) - if err != nil { + if err != "" { panic(err) } println(faucet.Render("")) diff --git a/examples/gno.land/r/gnoland/faucet/z3_filetest.gno b/examples/gno.land/r/gnoland/faucet/z3_filetest.gno index 1dca56811a9..682a619256a 100644 --- a/examples/gno.land/r/gnoland/faucet/z3_filetest.gno +++ b/examples/gno.land/r/gnoland/faucet/z3_filetest.gno @@ -18,21 +18,21 @@ func main() { ) std.TestSetOrigCaller(adminaddr) err := faucet.AdminAddController(controlleraddr1) - if err != nil { + if err != "" { panic(err) } err = faucet.AdminAddController(controlleraddr2) - if err != nil { + if err != "" { panic(err) } std.TestSetOrigCaller(controlleraddr1) err = faucet.Transfer(testaddr1, 1000000) - if err != nil { + if err != "" { panic(err) } std.TestSetOrigCaller(controlleraddr2) err = faucet.Transfer(testaddr1, 2000000) - if err != nil { + if err != "" { panic(err) } println(faucet.Render("")) diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index a4576fa6f48..6b77f5562a0 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -1012,6 +1012,29 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { args1 = Preprocess(nil, last, args1).(Expr) n.Args[1] = args1 } + } else { + var tx *constTypeExpr // array type expr, lazily initialized + // Another special case for append: adding untyped constants. + // They must be converted to the array type for consistency. + for i, arg := range n.Args[1:] { + if _, ok := arg.(*ConstExpr); !ok { + // Consider only constant expressions. + continue + } + if t1 := evalStaticTypeOf(store, last, arg); t1 != nil && !isUntyped(t1) { + // Consider only untyped values (including nil). + continue + } + + if tx == nil { + // Get the array type from the first argument. + s0 := evalStaticTypeOf(store, last, n.Args[0]) + tx = constType(arg, s0.Elem()) + } + // Convert to the array type. + arg1 := Call(tx, arg) + n.Args[i+1] = Preprocess(nil, last, arg1).(Expr) + } } } else if fv.PkgPath == uversePkgPath && fv.Name == "copy" { if len(n.Args) == 2 { diff --git a/gnovm/pkg/gnolang/values_conversions.go b/gnovm/pkg/gnolang/values_conversions.go index e2000defdde..66d8bcbf233 100644 --- a/gnovm/pkg/gnolang/values_conversions.go +++ b/gnovm/pkg/gnolang/values_conversions.go @@ -77,6 +77,10 @@ GNO_CASE: } // special case for undefined/nil source if tv.IsUndefined() { + switch t.Kind() { + case BoolKind, StringKind, IntKind, Int8Kind, Int16Kind, Int32Kind, Int64Kind, UintKind, Uint8Kind, Uint16Kind, Uint32Kind, Uint64Kind, Float32Kind, Float64Kind, BigintKind, BigdecKind: + panic(fmt.Sprintf("cannot convert %v to %v", tv, t)) + } tv.T = t return } diff --git a/gnovm/tests/files/append5.gno b/gnovm/tests/files/append5.gno index b1fdae852b1..0eba5a46463 100644 --- a/gnovm/tests/files/append5.gno +++ b/gnovm/tests/files/append5.gno @@ -7,4 +7,4 @@ func main() { } // Output: -// X +// X \ No newline at end of file diff --git a/gnovm/tests/files/append6.gno b/gnovm/tests/files/append6.gno new file mode 100644 index 00000000000..523c57bf044 --- /dev/null +++ b/gnovm/tests/files/append6.gno @@ -0,0 +1,12 @@ +package main + +func main() { + const x = 118999 + y := 11 + p := []int{} + p = append(p, x, y) + println(p[0] + p[1]) +} + +// Output: +// 119010 diff --git a/gnovm/tests/files/append7.gno b/gnovm/tests/files/append7.gno new file mode 100644 index 00000000000..85783724205 --- /dev/null +++ b/gnovm/tests/files/append7.gno @@ -0,0 +1,10 @@ +package main + +func main() { + var errors []error + errors = append(errors, nil, nil) + println(len(errors)) +} + +// Output: +// 2 diff --git a/gnovm/tests/files/convert4.gno b/gnovm/tests/files/convert4.gno new file mode 100644 index 00000000000..6ad748499ed --- /dev/null +++ b/gnovm/tests/files/convert4.gno @@ -0,0 +1,7 @@ +package main + +func main() { + println(int(nil)) +} + +// error: cannot convert (undefined) to int diff --git a/gnovm/tests/files/convert5.gno b/gnovm/tests/files/convert5.gno new file mode 100644 index 00000000000..acd33889ff0 --- /dev/null +++ b/gnovm/tests/files/convert5.gno @@ -0,0 +1,9 @@ +package main + +func main() { + var ints []int + ints = append(ints, nil, nil) + println(ints) +} + +// error: cannot convert (undefined) to int