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: missing length check in value declaration #2206

Merged
merged 19 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
29 changes: 26 additions & 3 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,10 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
// skip declarations already predefined
// (e.g. through recursion for a dependent)
} else {
d := n.(Decl)
checkAssignmentMismatch(d)
// recursively predefine dependencies.
d2, ppd := predefineNow(store, last, n.(Decl))
d2, ppd := predefineNow(store, last, d)
if ppd {
return d2, TRANS_SKIP
} else {
Expand Down Expand Up @@ -1865,8 +1867,9 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
// special case if `var a, b, c T? = f()` form.
cx := n.Values[0].(*CallExpr)
tt := evalStaticTypeOfRaw(store, last, cx).(*tupleType)
if len(tt.Elts) != numNames {
panic("should not happen")
rLen := len(tt.Elts)
if rLen != numNames {
panic(fmt.Sprintf("assignment mismatch: %d variable(s) but %s returns %d value(s)", numNames, cx.Func.String(), rLen))
omarsy marked this conversation as resolved.
Show resolved Hide resolved
}
if n.Type != nil {
// only a single type can be specified.
Expand All @@ -1887,6 +1890,14 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
} else if len(n.Values) != 0 && numNames != len(n.Values) {
panic("should not happen")
ltzmaxwell marked this conversation as resolved.
Show resolved Hide resolved
} else { // general case
for _, v := range n.Values {
if cx, ok := v.(*CallExpr); ok {
tt, ok := evalStaticTypeOfRaw(store, last, cx).(*tupleType)
if ok && len(tt.Elts) != 1 {
panic(fmt.Sprintf("multiple-value %s (value of type %s) in single-value context", cx.Func.String(), tt.Elts))
}
}
}
// evaluate types and convert consts.
if n.Type != nil {
// only a single type can be specified.
Expand Down Expand Up @@ -3039,6 +3050,18 @@ func checkIntegerType(xt Type) {
}
}

func checkAssignmentMismatch(d Decl) {
if cd, ok := d.(*ValueDecl); ok {
ltzmaxwell marked this conversation as resolved.
Show resolved Hide resolved
numNames := len(cd.NameExprs)
numValues := len(cd.Values)
if numValues > 0 && numValues != numNames {
ltzmaxwell marked this conversation as resolved.
Show resolved Hide resolved
if _, ok := cd.Values[0].(*CallExpr); !ok {
panic(fmt.Sprintf("assignment mismatch: %d variable(s) but %d value(s)", numNames, numValues))
}
}
}
}

// predefineNow() pre-defines (with empty placeholders) all
// declaration names, and then preprocesses all type/value decls, and
// partially processes func decls.
Expand Down
4 changes: 2 additions & 2 deletions gnovm/tests/files/var18.gno
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

func main() {
a, b, c := 1, 2
var a,b,c = 1,2
}

// Error:
// main/files/var18.gno:4: should not happen
// main/files/var18.gno:4: assignment mismatch: 3 variable(s) but 2 value(s)
11 changes: 11 additions & 0 deletions gnovm/tests/files/var19.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

func main() {
var a, b, c = 1, a+1
println(a)
println(b)
println(c)
}

// Error:
// main/files/var19.gno:4: assignment mismatch: 3 variable(s) but 2 value(s)
15 changes: 15 additions & 0 deletions gnovm/tests/files/var20.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

func r() int {
return 1
}

func main() {
var a, b, c = r()
}

// Error:
// main/files/var20.gno:8: assignment mismatch: 3 variable(s) but r<VPBlock(3,0)> returns 1 value(s):
// --- preprocess stack ---
// stack 0: func main() { var a<VPBlock(1,0)>, b<VPBlock(1,1)>, c<VPBlock(1,2)> = r<VPBlock(3,0)>() }
// ------------------------
17 changes: 17 additions & 0 deletions gnovm/tests/files/var21.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

func foo() (int, bool) {
return 1, true
}

func main() {
var a, b = 2, foo()
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved

println(a, b)
}

// Error:
// main/files/var21.gno:8: multiple-value foo<VPBlock(3,0)> (value of type [int bool]) in single-value context:
// --- preprocess stack ---
// stack 0: func main() { var a<VPBlock(1,0)>, b<VPBlock(1,1)> = (const (2 <untyped> bigint)), foo<VPBlock(3,0)>(); println<VPUverse(0)>(a<VPUverse(0)>, b<VPUverse(0)>) }
// ------------------------
Loading