Skip to content

Commit

Permalink
fix(gnovm): fix issue with locally re-definition (gnolang#3014)
Browse files Browse the repository at this point in the history
closes: gnolang#3013
<!-- please provide a detailed description of the changes made in this
pull request. -->

<details><summary>Contributors' checklist...</summary>

- [ ] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
</details>

---------

Co-authored-by: Morgan Bazalgette <morgan@morganbaz.com>
  • Loading branch information
ltzmaxwell and thehowl authored Oct 24, 2024
1 parent 520195e commit 287c22e
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 6 deletions.
7 changes: 1 addition & 6 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,24 +157,19 @@ func initStaticBlocks(store Store, ctx BlockNode, bn BlockNode) {
switch n := n.(type) {
case *AssignStmt:
if n.Op == DEFINE {
var defined bool
for _, lx := range n.Lhs {
nx := lx.(*NameExpr)
ln := nx.Name
if ln == blankIdentifier {
continue
}
if !isLocallyDefined2(last, ln) {
// if loop extern, will change to
// if loop extern, will promote to
// NameExprTypeHeapDefine later.
nx.Type = NameExprTypeDefine
last.Predefine(false, ln)
defined = true
}
}
if !defined {
panic(fmt.Sprintf("nothing defined in assignment %s", n.String()))
}
}
case *ImportDecl:
nx := &n.NameExpr
Expand Down
15 changes: 15 additions & 0 deletions gnovm/tests/files/redefine.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

var ss = []int{1, 2, 3}

func main() {
for _, s := range ss {
s := s
println(s)
}
}

// Output:
// 1
// 2
// 3
25 changes: 25 additions & 0 deletions gnovm/tests/files/redefine2.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

var testTable = []struct {
name string
}{
{
"one",
},
{
"two",
},
}

func main() {

for _, testCase := range testTable {
testCase := testCase

println(testCase.name)
}
}

// Output:
// one
// two
13 changes: 13 additions & 0 deletions gnovm/tests/files/redefine3.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

func main() {
for i := 0; i < 3; i++ {
i := i
println(i)
}
}

// Output:
// 0
// 1
// 2
15 changes: 15 additions & 0 deletions gnovm/tests/files/redefine4.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

func main() {
a := 1
b := 3
println(a, b) // prints 1 3

// Re-declaration of 'a' is allowed because 'c' is a new variable
a, c := 2, 5
println(a, c) // prints 2 5
}

// Output:
// 1 3
// 2 5
18 changes: 18 additions & 0 deletions gnovm/tests/files/redefine5.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

func main() {
a := 1
println(a) // prints 1

if true {
a := 2 // valid: new scope inside the if statement
println(a) // prints 2
}

println(a) // prints 1: outer variable is unchanged
}

// Output:
// 1
// 2
// 1
9 changes: 9 additions & 0 deletions gnovm/tests/files/redefine6.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
a, b := 1, 2
a, b := 3, 4
}

// Error:
// files/redefine6.gno:5:2: no new variables on left side of :=

0 comments on commit 287c22e

Please sign in to comment.