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): assignment operators require 1 expression on both sides #1943

Merged
merged 3 commits into from
Apr 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
21 changes: 13 additions & 8 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1607,9 +1607,20 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
}
}
}
} else { // ASSIGN.
} else { // ASSIGN, or assignment operation (+=, -=, <<=, etc.)
// If this is an assignment operation, ensure there's only 1
// expr on lhs/rhs.
if n.Op != ASSIGN &&
(len(n.Lhs) != 1 || len(n.Rhs) != 1) {
panic("assignment operator " + n.Op.TokenString() +
" requires only one expression on lhs and rhs")
}

// NOTE: Keep in sync with DEFINE above.
if len(n.Lhs) > len(n.Rhs) {
if n.Op == SHL_ASSIGN || n.Op == SHR_ASSIGN {
// Special case if shift assign <<= or >>=.
checkOrConvertType(store, last, &n.Rhs[0], UintType, false)
} else if len(n.Lhs) > len(n.Rhs) {
// TODO dry code w/ above.
// Unpack n.Rhs[0] to n.Lhs[:]
if len(n.Rhs) != 1 {
Expand Down Expand Up @@ -1641,12 +1652,6 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
default:
panic("should not happen")
}
} else if n.Op == SHL_ASSIGN || n.Op == SHR_ASSIGN {
if len(n.Lhs) != 1 || len(n.Rhs) != 1 {
panic("should not happen")
}
// Special case if shift assign <<= or >>=.
checkOrConvertType(store, last, &n.Rhs[0], UintType, false)
} else {
// General case: a, b = x, y.
for i, lx := range n.Lhs {
Expand Down
12 changes: 12 additions & 0 deletions gnovm/tests/files/assign22.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

func main() {
m := map[string]int{"a": 1}
var s int
var ok bool
s, ok <<= m["a"]
println(s, ok)
}

// Error:
// main/files/assign22.gno:7: assignment operator <<= requires only one expression on lhs and rhs
12 changes: 12 additions & 0 deletions gnovm/tests/files/assign23.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

func main() {
m := map[string]int{"a": 1}
var s int
var ok bool
s, ok += m["a"]
println(s, ok)
}

// Error:
// main/files/assign23.gno:7: assignment operator += requires only one expression on lhs and rhs
Loading