Skip to content
Open
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
5 changes: 5 additions & 0 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not directly related. Declaring a variable name iota := 0 is legal in Go (https://go.dev/play/p/FP9F1VlxwAt), and is now returning "cannot use iota outside constant declaration".

Either we should copy Go behavior, or set builtin identifiers cannot be shadowed: iota. like it is done for const iota = 5

}

io := pd.GetAttribute(ATTR_IOTA).(int)
cx := constUntypedBigint(n, int64(io))
return cx, TRANS_CONTINUE
Expand Down
17 changes: 17 additions & 0 deletions gnovm/tests/files/iota_outside_const.gno
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions gnovm/tests/files/iota_outside_const_2.gno
Original file line number Diff line number Diff line change
@@ -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