diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 33a6beea581..c485fd43346 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -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 { @@ -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 { diff --git a/gnovm/tests/files/assign22.gno b/gnovm/tests/files/assign22.gno new file mode 100644 index 00000000000..bedb8b4adb8 --- /dev/null +++ b/gnovm/tests/files/assign22.gno @@ -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 diff --git a/gnovm/tests/files/assign23.gno b/gnovm/tests/files/assign23.gno new file mode 100644 index 00000000000..ab648774028 --- /dev/null +++ b/gnovm/tests/files/assign23.gno @@ -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