Skip to content

Commit

Permalink
fix(gnovm): fix issue with locally re-definition (#3014)
Browse files Browse the repository at this point in the history
closes: #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 <[email protected]>
  • 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 :=

1 comment on commit 287c22e

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Go Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.20.

Benchmark suite Current: 287c22e Previous: 520195e Ratio
BenchmarkBinary/EmptyStruct:encode 473.3 ns/op 96 B/op 2 allocs/op 288.3 ns/op 96 B/op 2 allocs/op 1.64
BenchmarkBinary/EmptyStruct:encode - ns/op 473.3 ns/op 288.3 ns/op 1.64
BenchmarkBinary/EmptyStruct:decode 273 ns/op 0 B/op 0 allocs/op 133.4 ns/op 0 B/op 0 allocs/op 2.05
BenchmarkBinary/EmptyStruct:decode - ns/op 273 ns/op 133.4 ns/op 2.05
BenchmarkBinary/ShortArraysStruct:decode 383.5 ns/op 0 B/op 0 allocs/op 217.8 ns/op 0 B/op 0 allocs/op 1.76
BenchmarkBinary/ShortArraysStruct:decode - ns/op 383.5 ns/op 217.8 ns/op 1.76
BenchmarkDetaching 123.8 ns/op 0 B/op 0 allocs/op 75.57 ns/op 0 B/op 0 allocs/op 1.64
BenchmarkDetaching - ns/op 123.8 ns/op 75.57 ns/op 1.64
BenchmarkRemoved 38.92 ns/op 0 B/op 0 allocs/op 29.24 ns/op 0 B/op 0 allocs/op 1.33
BenchmarkRemoved - ns/op 38.92 ns/op 29.24 ns/op 1.33
BenchmarkBcryptGenerateFromPassword/benchmark-security-param 63642928 ns/op 5130 B/op 9 allocs/op 31930722 ns/op 5125 B/op 9 allocs/op 1.99
BenchmarkBcryptGenerateFromPassword/benchmark-security-param - ns/op 63642928 ns/op 31930722 ns/op 1.99
BenchmarkBcryptGenerateFromPassword/benchmark-security-param 127558017 ns/op 5139 B/op 9 allocs/op 31930722 ns/op 5125 B/op 9 allocs/op 3.99
BenchmarkBcryptGenerateFromPassword/benchmark-security-param - ns/op 127558017 ns/op 31930722 ns/op 3.99
BenchmarkBcryptGenerateFromPassword/benchmark-security-param 254520635 ns/op 5158 B/op 9 allocs/op 31930722 ns/op 5125 B/op 9 allocs/op 7.97
BenchmarkBcryptGenerateFromPassword/benchmark-security-param - ns/op 254520635 ns/op 31930722 ns/op 7.97
BenchmarkBcryptGenerateFromPassword/benchmark-security-param 508803084 ns/op 5196 B/op 10 allocs/op 31930722 ns/op 5125 B/op 9 allocs/op 15.93
BenchmarkBcryptGenerateFromPassword/benchmark-security-param - ns/op 508803084 ns/op 31930722 ns/op 15.93
BenchmarkBcryptGenerateFromPassword/benchmark-security-param 1017383442 ns/op 5528 B/op 13 allocs/op 31930722 ns/op 5125 B/op 9 allocs/op 31.86
BenchmarkBcryptGenerateFromPassword/benchmark-security-param - ns/op 1017383442 ns/op 31930722 ns/op 31.86
BenchmarkBcryptGenerateFromPassword/benchmark-security-param - allocs/op 13 allocs/op 9 allocs/op 1.44
BenchmarkBcryptGenerateFromPassword/benchmark-security-param 2034221178 ns/op 5736 B/op 15 allocs/op 31930722 ns/op 5125 B/op 9 allocs/op 63.71
BenchmarkBcryptGenerateFromPassword/benchmark-security-param - ns/op 2034221178 ns/op 31930722 ns/op 63.71
BenchmarkBcryptGenerateFromPassword/benchmark-security-param - allocs/op 15 allocs/op 9 allocs/op 1.67
BenchmarkSigning 84390 ns/op 1856 B/op 36 allocs/op 25723 ns/op 64 B/op 1 allocs/op 3.28
BenchmarkSigning - ns/op 84390 ns/op 25723 ns/op 3.28
BenchmarkSigning - B/op 1856 B/op 64 B/op 29
BenchmarkSigning - allocs/op 36 allocs/op 1 allocs/op 36
BenchmarkSigning 83467 ns/op 1856 B/op 36 allocs/op 25723 ns/op 64 B/op 1 allocs/op 3.24
BenchmarkSigning - ns/op 83467 ns/op 25723 ns/op 3.24
BenchmarkSigning - B/op 1856 B/op 64 B/op 29
BenchmarkSigning - allocs/op 36 allocs/op 1 allocs/op 36
BenchmarkVerification 160705 ns/op 864 B/op 19 allocs/op 61353 ns/op 0 B/op 0 allocs/op 2.62
BenchmarkVerification - ns/op 160705 ns/op 61353 ns/op 2.62
BenchmarkVerification - B/op 864 B/op 0 B/op +∞
BenchmarkVerification - allocs/op 19 allocs/op 0 allocs/op +∞
BenchmarkVerification 167233 ns/op 864 B/op 19 allocs/op 61353 ns/op 0 B/op 0 allocs/op 2.73
BenchmarkVerification - ns/op 167233 ns/op 61353 ns/op 2.73
BenchmarkVerification - B/op 864 B/op 0 B/op +∞
BenchmarkVerification - allocs/op 19 allocs/op 0 allocs/op +∞
BenchmarkRandomBytes/random 68.24 ns/op 16 B/op 1 allocs/op 32.74 ns/op 4 B/op 1 allocs/op 2.08
BenchmarkRandomBytes/random - ns/op 68.24 ns/op 32.74 ns/op 2.08
BenchmarkRandomBytes/random - B/op 16 B/op 4 B/op 4
BenchmarkRandomBytes/random 105.4 ns/op 32 B/op 1 allocs/op 32.74 ns/op 4 B/op 1 allocs/op 3.22
BenchmarkRandomBytes/random - ns/op 105.4 ns/op 32.74 ns/op 3.22
BenchmarkRandomBytes/random - B/op 32 B/op 4 B/op 8
BenchmarkRandomBytes/random 267.4 ns/op 112 B/op 1 allocs/op 32.74 ns/op 4 B/op 1 allocs/op 8.17
BenchmarkRandomBytes/random - ns/op 267.4 ns/op 32.74 ns/op 8.17
BenchmarkRandomBytes/random - B/op 112 B/op 4 B/op 28
BenchmarkRandomBytes/random 2309 ns/op 1024 B/op 1 allocs/op 32.74 ns/op 4 B/op 1 allocs/op 70.53
BenchmarkRandomBytes/random - ns/op 2309 ns/op 32.74 ns/op 70.53
BenchmarkRandomBytes/random - B/op 1024 B/op 4 B/op 256
BenchmarkSmall/boltdb-1000-100-16-40/update 1496479 ns/op 41722 B/op 397 allocs/op 997600 ns/op 38116 B/op 376 allocs/op 1.50
BenchmarkSmall/boltdb-1000-100-16-40/update - ns/op 1496479 ns/op 997600 ns/op 1.50
BenchmarkSmall/memdb-1000-100-16-40/block 16848704 ns/op 9323151 B/op 170137 allocs/op 12672327 ns/op 6606654 B/op 117223 allocs/op 1.33
BenchmarkSmall/memdb-1000-100-16-40/block - ns/op 16848704 ns/op 12672327 ns/op 1.33
BenchmarkSmall/memdb-1000-100-16-40/block - B/op 9323151 B/op 6606654 B/op 1.41
BenchmarkSmall/memdb-1000-100-16-40/block - allocs/op 170137 allocs/op 117223 allocs/op 1.45
BenchmarkMedium/boltdb-100000-100-16-40/update 7084988 ns/op 136336 B/op 1053 allocs/op 5686583 ns/op 104166 B/op 879 allocs/op 1.25
BenchmarkMedium/boltdb-100000-100-16-40/update - ns/op 7084988 ns/op 5686583 ns/op 1.25
BenchmarkMedium/boltdb-100000-100-16-40/update - B/op 136336 B/op 104166 B/op 1.31
BenchmarkMedium/memdb-100000-100-16-40/update 1254618 ns/op 379435 B/op 7517 allocs/op 1021157 ns/op 256615 B/op 4954 allocs/op 1.23
BenchmarkMedium/memdb-100000-100-16-40/update - ns/op 1254618 ns/op 1021157 ns/op 1.23
BenchmarkMedium/memdb-100000-100-16-40/update - B/op 379435 B/op 256615 B/op 1.48
BenchmarkMedium/memdb-100000-100-16-40/update - allocs/op 7517 allocs/op 4954 allocs/op 1.52
BenchmarkLevelDBBatchSizes/goleveldb-100000-400-16-40/update - B/op 48250 B/op 38410 B/op 1.26
BenchmarkLevelDBBatchSizes/goleveldb-100000-400-16-40/update - allocs/op 591 allocs/op 445 allocs/op 1.33
BenchmarkLevelDBBatchSizes/goleveldb-100000-2000-16-40/update - allocs/op 417 allocs/op 338 allocs/op 1.23
BenchmarkHash/ripemd160 2832 ns/op 25 B/op 1 allocs/op 704.3 ns/op 25 B/op 1 allocs/op 4.02
BenchmarkHash/ripemd160 - ns/op 2832 ns/op 704.3 ns/op 4.02
BenchmarkHash/sha2-256 522.3 ns/op 33 B/op 1 allocs/op 169.7 ns/op 33 B/op 1 allocs/op 3.08
BenchmarkHash/sha2-256 - ns/op 522.3 ns/op 169.7 ns/op 3.08
BenchmarkHash/sha3-256 1835 ns/op 33 B/op 1 allocs/op 689.3 ns/op 33 B/op 1 allocs/op 2.66
BenchmarkHash/sha3-256 - ns/op 1835 ns/op 689.3 ns/op 2.66
BenchmarkWriteSecretConnection 6542 ns/op 0 B/op 0 allocs/op 4012 ns/op 0 B/op 0 allocs/op 1.63
BenchmarkWriteSecretConnection - ns/op 6542 ns/op 4012 ns/op 1.63
BenchmarkReadSecretConnection 3143 ns/op 0 B/op 0 allocs/op 2351 ns/op 0 B/op 0 allocs/op 1.34
BenchmarkReadSecretConnection - ns/op 3143 ns/op 2351 ns/op 1.34

This comment was automatically generated by workflow using github-action-benchmark.

CC: @ajnavarro @thehowl @zivkovicmilos

Please sign in to comment.