Skip to content

Commit

Permalink
fix(gnovm): value decl loop (gnolang#2074)
Browse files Browse the repository at this point in the history
fixes [this](gnolang#1849) by splitting
value declarations with multiple values into single declarations with a
single value.

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

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

---------

Co-authored-by: Morgan Bazalgette <morgan@morganbaz.com>
  • Loading branch information
petar-dambovaliev and thehowl committed Sep 12, 2024
1 parent 8f800ec commit aa4bc99
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 16 deletions.
60 changes: 44 additions & 16 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ func PredefineFileSet(store Store, pn *PackageNode, fset *FileSet) {
// skip declarations already predefined
// (e.g. through recursion for a
// dependent)
} else {
// recursively predefine dependencies.
d2, _ := predefineNow(store, fn, d)
fn.Decls[i] = d2
continue
}

// recursively predefine dependencies.
d2, _ := predefineNow(store, fn, d)
fn.Decls[i] = d2
}
}
}
Expand All @@ -63,11 +64,12 @@ func PredefineFileSet(store Store, pn *PackageNode, fset *FileSet) {
// skip declarations already predefined
// (e.g. through recursion for a
// dependent)
} else {
// recursively predefine dependencies.
d2, _ := predefineNow(store, fn, d)
fn.Decls[i] = d2
continue
}

// recursively predefine dependencies.
d2, _ := predefineNow(store, fn, d)
fn.Decls[i] = d2
}
}
}
Expand All @@ -81,11 +83,12 @@ func PredefineFileSet(store Store, pn *PackageNode, fset *FileSet) {
// skip declarations already predefined
// (e.g. through recursion for a
// dependent)
} else {
// recursively predefine dependencies.
d2, _ := predefineNow(store, fn, d)
fn.Decls[i] = d2
continue
}

// recursively predefine dependencies.
d2, _ := predefineNow(store, fn, d)
fn.Decls[i] = d2
}
}
}
Expand All @@ -97,11 +100,36 @@ func PredefineFileSet(store Store, pn *PackageNode, fset *FileSet) {
if d.GetAttribute(ATTR_PREDEFINED) == true {
// skip declarations already predefined (e.g.
// through recursion for a dependent)
} else {
// recursively predefine dependencies.
d2, _ := predefineNow(store, fn, d)
fn.Decls[i] = d2
continue
}

if vd, ok := d.(*ValueDecl); ok && len(vd.NameExprs) > 1 && len(vd.Values) == len(vd.NameExprs) {
split := make([]Decl, len(vd.NameExprs))

for j := 0; j < len(vd.NameExprs); j++ {
base := vd.Copy().(*ValueDecl)
base.NameExprs = NameExprs{NameExpr{
Attributes: base.NameExprs[j].Attributes,
Path: base.NameExprs[j].Path,
Name: base.NameExprs[j].Name,
}}

if j < len(base.Values) {
base.Values = Exprs{base.Values[j].Copy().(Expr)}
}

split[j], _ = predefineNow(store, fn, base)
}

fn.Decls = append(fn.Decls[:i], append(split, fn.Decls[i+1:]...)...) //nolint:makezero
i += len(vd.NameExprs)
continue
}

// recursively predefine dependencies.
d2, _ := predefineNow(store, fn, d)

fn.Decls[i] = d2
}
}
}
Expand Down
1 change: 1 addition & 0 deletions gnovm/tests/files/var19.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

func main() {
var a, b, c = 1, a+1

println(a)
println(b)
println(c)
Expand Down
14 changes: 14 additions & 0 deletions gnovm/tests/files/var29.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

func main() {
println(a)
println(b)
println(c)
}

var a, b, c = 1, a + 1, b + 1

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

func main() {
println(a)
println(b)
println(c)
println(d)
}

var a, b, c = 1, a + d, 3
var d = a

// Output:
// 1
// 2
// 3
// 1

0 comments on commit aa4bc99

Please sign in to comment.