Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle untyped constant expressions in append() #1177

Merged
merged 9 commits into from
Jan 10, 2024
17 changes: 17 additions & 0 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,23 @@
n.Args[1] = args1
}
}
mvertes marked this conversation as resolved.
Show resolved Hide resolved
// 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

Check warning on line 1014 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L1011-L1014

Added lines #L1011 - L1014 were not covered by tests
}
if t1 := evalStaticTypeOf(store, last, arg); t1 != nil && !isUntyped(t1) {
// Consider only untyped values (including nil).
continue

Check warning on line 1018 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L1016-L1018

Added lines #L1016 - L1018 were not covered by tests
}
// 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)
mvertes marked this conversation as resolved.
Show resolved Hide resolved
n.Args[i+1] = Preprocess(nil, last, arg1).(Expr)

Check warning on line 1024 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L1021-L1024

Added lines #L1021 - L1024 were not covered by tests
}
} else if fv.PkgPath == uversePkgPath && fv.Name == "copy" {
if len(n.Args) == 2 {
// If the second argument is a string,
Expand Down
10 changes: 10 additions & 0 deletions gnovm/tests/files/append5.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

func main() {
var errors []error
errors = append(errors, nil, nil)
println(len(errors))
}

// Output:
// 2
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
Loading