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(gnovm): handle non call expression valuedecl values #2647

Merged
merged 4 commits into from
Aug 19, 2024
Merged
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
41 changes: 35 additions & 6 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -2163,13 +2163,42 @@
numNames := len(n.NameExprs)
sts := make([]Type, numNames) // static types
tvs := make([]TypedValue, numNames)

if numNames > 1 && len(n.Values) == 1 {
// special case if `var a, b, c T? = f()` form.
cx := n.Values[0].(*CallExpr)
tt := evalStaticTypeOfRaw(store, last, cx).(*tupleType)
if rLen := len(tt.Elts); rLen != numNames {
panic(fmt.Sprintf("assignment mismatch: %d variable(s) but %s returns %d value(s)", numNames, cx.Func.String(), rLen))
// Special cases if one of the following:
// - `var a, b, c T = f()`
// - `var a, b = n.(T)`
// - `var a, b = n[i], where n is a map`

var tuple *tupleType
valueExpr := n.Values[0]
valueType := evalStaticTypeOfRaw(store, last, valueExpr)

switch expr := valueExpr.(type) {
case *CallExpr:
tuple = valueType.(*tupleType)
case *TypeAssertExpr, *IndexExpr:
tuple = &tupleType{Elts: []Type{valueType, BoolType}}
if ex, ok := expr.(*TypeAssertExpr); ok {
ex.HasOK = true
break
}
expr.(*IndexExpr).HasOK = true
default:
panic(fmt.Sprintf("unexpected ValueDecl value expression type %T", expr))

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2187-L2188

Added lines #L2187 - L2188 were not covered by tests
}

if rLen := len(tuple.Elts); rLen != numNames {
panic(
fmt.Sprintf(
"assignment mismatch: %d variable(s) but %s returns %d value(s)",
numNames,
valueExpr.String(),
rLen,
),
)
}

if n.Type != nil {
// only a single type can be specified.
nt := evalStaticType(store, last, n.Type)
Expand All @@ -2181,7 +2210,7 @@
} else {
// set types as return types.
for i := 0; i < numNames; i++ {
et := tt.Elts[i]
et := tuple.Elts[i]
sts[i] = et
tvs[i] = anyValue(et)
}
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/var20.gno
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ func main() {
}

// Error:
// main/files/var20.gno:8:6: assignment mismatch: 3 variable(s) but r<VPBlock(3,0)> returns 1 value(s)
// main/files/var20.gno:8:6: assignment mismatch: 3 variable(s) but r<VPBlock(3,0)>() returns 1 value(s)
23 changes: 23 additions & 0 deletions gnovm/tests/files/vardecl.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

func main() {
var i interface{} = 1
var a, ok = i.(int)
println(a, ok)

var b, c = doSomething()
println(b, c)

m := map[string]int{"a": 1, "b": 2}
var d, okk = m["d"]
println(d, okk)
}

func doSomething() (int, string) {
return 4, "hi"
}

// Output:
// 1 true
// 4 hi
// 0 false
Loading