Skip to content

Commit

Permalink
fix: generalize previous to handle any untyped const expression
Browse files Browse the repository at this point in the history
Fixes also gnolang#1149.
  • Loading branch information
mvertes committed Sep 27, 2023
1 parent aabf7c2 commit 0432faf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
24 changes: 15 additions & 9 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,17 +1006,23 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
n.Args[1] = args1
}
}
// Another special case for append: adding nil to an array.
// Untyped nil must be converted to the array type for consistency.
// Another special case for append: adding untyped constants to an array.
// They must be converted to the array type for consistency.
for i, arg := range n.Args[1:] {
if cx, ok := arg.(*ConstExpr); ok && isNilExpr(cx.Source) {
// We append an untyped nil value. Get the array type from
// the first argument and convert nil to it.
s0 := evalStaticTypeOf(store, last, n.Args[0])
tx := constType(nil, s0.Elem())
var arg1 Expr = Call(tx, arg)
n.Args[i+1] = Preprocess(nil, last, arg1).(Expr)
if _, ok := arg.(*ConstExpr); !ok {
// Consider only constant expressions.
continue
}
t1 := evalStaticTypeOf(store, last, arg)
if t1 != nil && !isUntyped(t1) {
// Consider only untyped values.
continue
}
// Get the array type from the first argument and convert to it.
s0 := evalStaticTypeOf(store, last, n.Args[0])
tx := constType(arg, s0.Elem())
var arg1 Expr = 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 {
Expand Down
12 changes: 12 additions & 0 deletions gnovm/tests/files/append6.gno
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0432faf

Please sign in to comment.