diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 7387746b3fb..d820aa6c5df 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -1297,6 +1297,11 @@ func preprocess1(store Store, ctx BlockNode, n Node) Node { return n, TRANS_CONTINUE case "iota": pd := lastDecl(ns) + valueDecl, ok := pd.(*ValueDecl) + if !ok || !valueDecl.Const { + panic("cannot use iota outside constant declaration") + } + io := pd.GetAttribute(ATTR_IOTA).(int) cx := constUntypedBigint(n, int64(io)) return cx, TRANS_CONTINUE diff --git a/gnovm/tests/files/iota_outside_const.gno b/gnovm/tests/files/iota_outside_const.gno new file mode 100644 index 00000000000..a86ef5b09c2 --- /dev/null +++ b/gnovm/tests/files/iota_outside_const.gno @@ -0,0 +1,17 @@ +package main + +const X = iota + +func f(x int) {} + +func main() { + f(X) + f(iota) // ERROR "iota" + f(X) +} + +// Error: +// main/iota_outside_const.gno:9:4-8: cannot use iota outside constant declaration + +// TypeCheckError: +// main/iota_outside_const.gno:9:4: cannot use iota outside constant declaration diff --git a/gnovm/tests/files/iota_outside_const_2.gno b/gnovm/tests/files/iota_outside_const_2.gno new file mode 100644 index 00000000000..e5ba16620bc --- /dev/null +++ b/gnovm/tests/files/iota_outside_const_2.gno @@ -0,0 +1,18 @@ +package main + +const X = iota + +func f(x int) {} + +func main() { + f(X) + var a = iota // ERROR "iota" + _ = a + f(X) +} + +// Error: +// main/iota_outside_const_2.gno:9:10-14: cannot use iota outside constant declaration + +// TypeCheckError: +// main/iota_outside_const_2.gno:9:10: cannot use iota outside constant declaration