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: global var dependencies #2077

Open
wants to merge 46 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
ae8c44e
...
jaekwon Jun 23, 2024
3c46e27
fix more tests
jaekwon Jun 23, 2024
f4306ed
fix tests
jaekwon Jun 23, 2024
5cc2494
...
jaekwon Jun 24, 2024
31b2263
update golden tests
jaekwon Jun 24, 2024
f1d7c3e
...
jaekwon Jun 24, 2024
492f103
...
jaekwon Jun 24, 2024
31bbcb6
remove predefine call; update comments
jaekwon Jun 26, 2024
86f03c4
fix linter
jaekwon Jul 3, 2024
109a1c4
Merge branch 'master' into init_static
petar-dambovaliev Jul 6, 2024
b41627d
fix example
petar-dambovaliev Jul 6, 2024
a28e742
Merge branch 'master' into init_static
petar-dambovaliev Jul 6, 2024
32d7aa0
golden
petar-dambovaliev Jul 6, 2024
aa9c3a1
Merge branch 'master' into init_static
petar-dambovaliev Jul 6, 2024
92db816
golden
petar-dambovaliev Jul 6, 2024
1ac3a6a
Merge branch 'master' into init_static
petar-dambovaliev Jul 6, 2024
53cbced
golden
petar-dambovaliev Jul 6, 2024
252e7cd
golden
petar-dambovaliev Jul 6, 2024
2f5509e
save
petar-dambovaliev May 13, 2024
6d4e511
save
petar-dambovaliev May 13, 2024
d26e51f
save
petar-dambovaliev May 13, 2024
45fa1d8
save
petar-dambovaliev May 13, 2024
94dba1f
save
petar-dambovaliev May 13, 2024
c45cabc
save
petar-dambovaliev May 13, 2024
c8bc8e5
save
petar-dambovaliev May 15, 2024
5a83e61
save
petar-dambovaliev Jun 12, 2024
4e48847
save
petar-dambovaliev Jun 12, 2024
ac3b2fb
save
petar-dambovaliev Jun 12, 2024
481ce59
save
petar-dambovaliev Jun 12, 2024
9e8544d
save
petar-dambovaliev Jun 12, 2024
59ed04b
save
petar-dambovaliev Jun 12, 2024
9df6662
save
petar-dambovaliev Jun 12, 2024
db713cc
save
petar-dambovaliev Jul 8, 2024
d8dc965
Merge branch 'master' into vardeps
petar-dambovaliev Jul 8, 2024
f4e7318
Update unknow_lib.txtar
petar-dambovaliev Jul 8, 2024
6873cf5
Update test_with-native-fallback.txtar
petar-dambovaliev Jul 8, 2024
f447489
Merge branch 'master' into vardeps
petar-dambovaliev Jul 8, 2024
3deceb7
save
petar-dambovaliev Jul 8, 2024
cea5e78
save
petar-dambovaliev Jul 8, 2024
a27fd0e
save
petar-dambovaliev Jul 10, 2024
aeae7d7
save
petar-dambovaliev Jul 15, 2024
79eab9a
save
petar-dambovaliev Jul 15, 2024
a948580
save
petar-dambovaliev Jul 16, 2024
7178cfb
save
petar-dambovaliev Jul 16, 2024
b5f23c2
Merge branch 'master' into vardeps
petar-dambovaliev Sep 11, 2024
05f348a
Merge branch 'master' into vardeps
petar-dambovaliev Sep 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 238 additions & 0 deletions gnovm/pkg/gnolang/preprocess.go
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3073,6 +3073,237 @@
return findUndefined2(store, last, x, nil)
}

// finds the next undefined identifier and returns it if it is global
func findUndefined2SkipLocals(store Store, last BlockNode, x Expr, t Type) Name {
name := findUndefined2(store, last, x, t)

existsLocal := func(name Name, bn BlockNode) bool {
curr := bn
for {
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
currNames := bn.GetBlockNames()
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be bn.GetBlockNames() or curr.GetBlockNames()?


for _, currName := range currNames {
if currName == name {
return true
}
}

if bn.GetStaticBlock().Source == curr {
return false
}

curr = bn.GetStaticBlock().GetParentNode(store)

Check warning on line 3095 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3095

Added line #L3095 was not covered by tests

if curr == nil {
return false

Check warning on line 3098 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3097-L3098

Added lines #L3097 - L3098 were not covered by tests
}
}
}

if name != "" {
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
pkg := packageOf(last)

if _, _, ok := pkg.FileSet.GetDeclForSafe(name); !ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this return "" if this method returns true, indicating that the name is defined?

return ""
}

isLocal := existsLocal(name, last)

if isLocal {
return ""
}
}

return name
}

func findUndefinedStmt(store Store, last BlockNode, stmt Stmt, t Type) Name {
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
switch s := stmt.(type) {
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
case *TypeDecl:
un := findUndefined2SkipLocals(store, last, s.Type, t)

if un != "" {
return un

Check warning on line 3126 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3126

Added line #L3126 was not covered by tests
}
case *ValueDecl:
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
un := findUndefined2SkipLocals(store, last, s.Type, t)

Check warning on line 3129 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3128-L3129

Added lines #L3128 - L3129 were not covered by tests

if un != "" {
return un

Check warning on line 3132 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3131-L3132

Added lines #L3131 - L3132 were not covered by tests
}
for _, rh := range s.Values {
un := findUndefined2SkipLocals(store, last, rh, t)

Check warning on line 3135 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3134-L3135

Added lines #L3134 - L3135 were not covered by tests

if un != "" {
return un

Check warning on line 3138 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3137-L3138

Added lines #L3137 - L3138 were not covered by tests
}
}
case *DeclStmt:
for _, rh := range s.Body {
un := findUndefinedStmt(store, last, rh, t)

if un != "" {
return un

Check warning on line 3146 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3146

Added line #L3146 was not covered by tests
}
}
case *IncDecStmt:
un := findUndefined2SkipLocals(store, last, s.X, t)

Check warning on line 3150 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3149-L3150

Added lines #L3149 - L3150 were not covered by tests

if un != "" {
return un

Check warning on line 3153 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3152-L3153

Added lines #L3152 - L3153 were not covered by tests
}
case *PanicStmt:
un := findUndefined2SkipLocals(store, last, s.Exception, t)

Check warning on line 3156 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3155-L3156

Added lines #L3155 - L3156 were not covered by tests
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved

if un != "" {
return un

Check warning on line 3159 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3158-L3159

Added lines #L3158 - L3159 were not covered by tests
}
case *BlockStmt:
for _, rh := range s.Body {
un := findUndefinedStmt(store, s, rh, t)

Check warning on line 3163 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3161-L3163

Added lines #L3161 - L3163 were not covered by tests

if un != "" {
return un

Check warning on line 3166 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3165-L3166

Added lines #L3165 - L3166 were not covered by tests
}
}
case *DeferStmt:
un := findUndefined2SkipLocals(store, last, s.Call.Func, t)

Check warning on line 3170 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3169-L3170

Added lines #L3169 - L3170 were not covered by tests

if un != "" {
return un

Check warning on line 3173 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3172-L3173

Added lines #L3172 - L3173 were not covered by tests
}

for _, rh := range s.Call.Args {
un = findUndefined2SkipLocals(store, last, rh, t)

Check warning on line 3177 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3176-L3177

Added lines #L3176 - L3177 were not covered by tests

if un != "" {
return un

Check warning on line 3180 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3179-L3180

Added lines #L3179 - L3180 were not covered by tests
}
}
case *SwitchStmt:
un := findUndefined2SkipLocals(store, last, s.X, t)
if un != "" {
return un

Check warning on line 3186 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3186

Added line #L3186 was not covered by tests
}

un = findUndefinedStmt(store, last, s.Init, t)
if un != "" {
return un

Check warning on line 3191 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3191

Added line #L3191 was not covered by tests
}

for _, b := range s.Clauses {
b := b
un = findUndefinedStmt(store, s, &b, t)

if un != "" {
return un
}
}
case *SwitchClauseStmt:
for _, rh := range s.Cases {
un := findUndefined2SkipLocals(store, last, rh, t)

if un != "" {
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
return un

Check warning on line 3207 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3207

Added line #L3207 was not covered by tests
}
}

for _, b := range s.Body {
un := findUndefinedStmt(store, last, b, t)

if un != "" {
return un
}
}

case *ExprStmt:
return findUndefined2SkipLocals(store, last, s.X, t)
case *AssignStmt:
for _, rh := range s.Rhs {
un := findUndefined2SkipLocals(store, last, rh, t)

if un != "" {
return un
}
}
case *IfStmt:
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
un := findUndefinedStmt(store, last, s.Init, t)
if un != "" {
return un

Check warning on line 3232 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3232

Added line #L3232 was not covered by tests
}

un = findUndefined2SkipLocals(store, last, s.Cond, t)
if un != "" {
return un

Check warning on line 3237 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3237

Added line #L3237 was not covered by tests
}

un = findUndefinedStmt(store, last, &s.Else, t)
if un != "" {
return un
}

un = findUndefinedStmt(store, last, &s.Then, t)
if un != "" {
return un
}
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
case *IfCaseStmt:
for _, b := range s.Body {
un := findUndefinedStmt(store, last, b, t)

if un != "" {
return un
}
}
case *ReturnStmt:
for _, b := range s.Results {
un := findUndefined2SkipLocals(store, last, b, t)
if un != "" {
return un
}
}
case *RangeStmt:
un := findUndefined2SkipLocals(store, last, s.X, t)
if un != "" {
return un
}

for _, b := range s.Body {
un := findUndefinedStmt(store, last, b, t)
if un != "" {
return un

Check warning on line 3273 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3273

Added line #L3273 was not covered by tests
}
}
case *ForStmt:
un := findUndefinedStmt(store, s, s.Init, t)
if un != "" {
return un

Check warning on line 3279 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3276-L3279

Added lines #L3276 - L3279 were not covered by tests
}

un = findUndefined2SkipLocals(store, s, s.Cond, t)
if un != "" {
return un

Check warning on line 3284 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3282-L3284

Added lines #L3282 - L3284 were not covered by tests
}

un = findUndefinedStmt(store, s, s.Post, t)
if un != "" {
return un

Check warning on line 3289 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3287-L3289

Added lines #L3287 - L3289 were not covered by tests
}

for _, b := range s.Body {
un := findUndefinedStmt(store, last, b, t)
if un != "" {
return un

Check warning on line 3295 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3292-L3295

Added lines #L3292 - L3295 were not covered by tests
}
}
case *BranchStmt:

Check warning on line 3298 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3298

Added line #L3298 was not covered by tests
case nil:
return ""
default:
panic(fmt.Sprintf("findUndefinedStmt: %T not supported", s))

Check warning on line 3302 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3301-L3302

Added lines #L3301 - L3302 were not covered by tests
}
return ""
}

func findUndefined2(store Store, last BlockNode, x Expr, t Type) (un Name) {
if x == nil {
return
Expand Down Expand Up @@ -3185,6 +3416,13 @@
ct.String()))
}
case *FuncLitExpr:
for _, stmt := range cx.Body {
un = findUndefinedStmt(store, cx, stmt, t)

if un != "" {
return
}
}
return findUndefined(store, last, &cx.Type)
case *FieldTypeExpr:
return findUndefined(store, last, cx.Type)
Expand Down
16 changes: 16 additions & 0 deletions gnovm/tests/files/closure.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

func main() {

}

var a = func() {
b()
}

var b = func() {
a()
}

// Error:
// main/files/closure.gno:7:5: constant definition loop with a
80 changes: 80 additions & 0 deletions gnovm/tests/files/var27.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

func main() {}

var myDep string

var myVar1 = func() {
a := myDep1
}

var myDep1 string

var myVar2 = func() {
aaa := ""

switch myDep {
case aaa:
println(myDep2)
}
}

var myDep2 string

var myVar3 = func() {
for _, c := range myDep3 {
println(c)
}
}

var myDep3 string

var v1 = func() int {
v2 := 11
return v2
}()

var v2 = func() int {
return v1
}()

var v3 = func() int {
return func() int {
v4 := 11
return v4
}()
}()

var v4 = func() int {
return v3
}()

var v5 = func() int {
v6 := 11
return func() int {
return v6
}()
}()

var v6 = func() int {
return v5
}()

var other = func() {
if true {
something := 2
print(something) // 2
} else {
print(something) // a string, but single shared 'st' masks the outer/global reference.
}
}
var something = "a string"

var other1 = func() {
if true {
something1 := 2
print(something1) // 2
}
print(something1) // a string, but single shared 'st' masks the outer/global reference.
}
var something1 = "a string"
18 changes: 18 additions & 0 deletions gnovm/tests/files/var28.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

func main() {}

var foo = func() (bool, bool) {
return true, true
}

var x = func() bool { return a }()
var a, b = foo()

var a1 = func() int {
type B1 b1
x := B1(1)
return int(x)
}

type b1 int
Loading