Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gnovm): fix issue with locally re-definition #3014

Merged
merged 13 commits into from
Oct 24, 2024
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) {
thehowl marked this conversation as resolved.
Show resolved Hide resolved
thehowl marked this conversation as resolved.
Show resolved Hide resolved
// 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 :=
Loading