Skip to content
Draft
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
11 changes: 11 additions & 0 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,17 @@ func preprocess1(store Store, ctx BlockNode, n Node) Node {
n.Path = NewValuePathBlock(0, 0, blankIdentifier)
return n, TRANS_CONTINUE
case "iota":
// iota is a non-shadowable builtin; reject declaring a new
// identifier named "iota" (`iota := 5` or `for iota := range x`)
// instead of falling through to the constant-only check below.
isDefine := ftype == TRANS_RANGE_KEY || ftype == TRANS_RANGE_VALUE
if as, ok := ns[len(ns)-1].(*AssignStmt); ok {
isDefine = isDefine || (ftype == TRANS_ASSIGN_LHS && as.Op == DEFINE)
}
if isDefine {
panic(fmt.Sprintf("builtin identifiers cannot be shadowed: %s", n.Name))
}

pd := lastDecl(ns)
valueDecl, ok := pd.(*ValueDecl)
if !ok || !valueDecl.Const {
Expand Down
9 changes: 9 additions & 0 deletions gnovm/tests/files/iota_identifier.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
iota := 5 // ERROR "builtin identifiers cannot be shadowed"
_ = iota
}

// Error:
// main/iota_identifier.gno:4:2-6: builtin identifiers cannot be shadowed: iota
11 changes: 11 additions & 0 deletions gnovm/tests/files/iota_identifier_range.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

func main() {
s := []int{1, 2, 3}
for iota := range s { // ERROR "builtin identifiers cannot be shadowed"
_ = iota
}
}

// Error:
// main/iota_identifier_range.gno:5:6-10: builtin identifiers cannot be shadowed: iota