From 287c22ec830a1408f1d3de6319602640d841c7cc Mon Sep 17 00:00:00 2001 From: ltzmaxwell Date: Wed, 23 Oct 2024 23:09:04 -0500 Subject: [PATCH] fix(gnovm): fix issue with locally re-definition (#3014) closes: https://github.com/gnolang/gno/issues/3013
Contributors' checklist... - [ ] 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
--------- Co-authored-by: Morgan Bazalgette --- gnovm/pkg/gnolang/preprocess.go | 7 +------ gnovm/tests/files/redefine.gno | 15 +++++++++++++++ gnovm/tests/files/redefine2.gno | 25 +++++++++++++++++++++++++ gnovm/tests/files/redefine3.gno | 13 +++++++++++++ gnovm/tests/files/redefine4.gno | 15 +++++++++++++++ gnovm/tests/files/redefine5.gno | 18 ++++++++++++++++++ gnovm/tests/files/redefine6.gno | 9 +++++++++ 7 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 gnovm/tests/files/redefine.gno create mode 100644 gnovm/tests/files/redefine2.gno create mode 100644 gnovm/tests/files/redefine3.gno create mode 100644 gnovm/tests/files/redefine4.gno create mode 100644 gnovm/tests/files/redefine5.gno create mode 100644 gnovm/tests/files/redefine6.gno diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index e1dc3671333..04cc83b54f0 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -157,7 +157,6 @@ 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 @@ -165,16 +164,12 @@ func initStaticBlocks(store Store, ctx BlockNode, bn BlockNode) { 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 diff --git a/gnovm/tests/files/redefine.gno b/gnovm/tests/files/redefine.gno new file mode 100644 index 00000000000..62eea61f25e --- /dev/null +++ b/gnovm/tests/files/redefine.gno @@ -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 diff --git a/gnovm/tests/files/redefine2.gno b/gnovm/tests/files/redefine2.gno new file mode 100644 index 00000000000..b0621fe2a03 --- /dev/null +++ b/gnovm/tests/files/redefine2.gno @@ -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 diff --git a/gnovm/tests/files/redefine3.gno b/gnovm/tests/files/redefine3.gno new file mode 100644 index 00000000000..7a2d0859f5c --- /dev/null +++ b/gnovm/tests/files/redefine3.gno @@ -0,0 +1,13 @@ +package main + +func main() { + for i := 0; i < 3; i++ { + i := i + println(i) + } +} + +// Output: +// 0 +// 1 +// 2 diff --git a/gnovm/tests/files/redefine4.gno b/gnovm/tests/files/redefine4.gno new file mode 100644 index 00000000000..db310fae3c2 --- /dev/null +++ b/gnovm/tests/files/redefine4.gno @@ -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 diff --git a/gnovm/tests/files/redefine5.gno b/gnovm/tests/files/redefine5.gno new file mode 100644 index 00000000000..9637df913f0 --- /dev/null +++ b/gnovm/tests/files/redefine5.gno @@ -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 diff --git a/gnovm/tests/files/redefine6.gno b/gnovm/tests/files/redefine6.gno new file mode 100644 index 00000000000..ff5ca97d063 --- /dev/null +++ b/gnovm/tests/files/redefine6.gno @@ -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 :=