Skip to content

Commit

Permalink
feat: add checkAssignmentMismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
omarsy committed Jun 2, 2024
1 parent b436f3a commit bd4b924
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
23 changes: 17 additions & 6 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1857,13 +1857,11 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
tvs := make([]TypedValue, numNames)
if numNames > 1 && len(n.Values) == 1 {
// special case if `var a, b, c T? = f()` form.
cx, ok := n.Values[0].(*CallExpr)
if !ok {
panic("should not happen")
}
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))
}
if n.Type != nil {
// only a single type can be specified.
Expand Down Expand Up @@ -2984,6 +2982,18 @@ func checkIntegerType(xt Type) {
}
}

func checkAssignmentMismatch(d Decl) {

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2985

Added line #L2985 was not covered by tests
if cd, ok := d.(*ValueDecl); ok {
numNames := len(cd.NameExprs)
numValues := len(cd.Values)
if numValues > 0 && numValues != numNames {
if _, ok := cd.Values[0].(*CallExpr); !ok {

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2990

Added line #L2990 was not covered by tests
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 Expand Up @@ -3016,6 +3026,7 @@ func predefineNow(store Store, last BlockNode, d Decl) (Decl, bool) {
}
}
}()
checkAssignmentMismatch(d)
m := make(map[Name]struct{})
return predefineNow2(store, last, d, m)
}
Expand Down
5 changes: 1 addition & 4 deletions gnovm/tests/files/var18.gno
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@ func main() {
}

// Error:
// main/files/var18.gno:3: should not happen:
// --- preprocess stack ---
// stack 0: func main() { var a<VPBlock(1,0)>, b<VPBlock(1,1)> = (const (1 <untyped> bigint)) }
// ------------------------
// main/files/var18.gno:3: assignment mismatch: 2 variable(s) but 1 value(s)
2 changes: 1 addition & 1 deletion gnovm/tests/files/var19.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ func main() {
}

// Error:
// main/files/var19.gno:3: constant definition loop with a
// main/files/var19.gno:3: assignment mismatch: 3 variable(s) but 2 value(s)
2 changes: 1 addition & 1 deletion gnovm/tests/files/var20.gno
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func main() {
}

// Error:
// main/files/var20.gno:7: should not happen:
// main/files/var20.gno:7: 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)>() }
// ------------------------

0 comments on commit bd4b924

Please sign in to comment.