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 39 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
201 changes: 201 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 @@ -2805,6 +2805,200 @@
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().Source

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2827

Added line #L2827 was not covered by tests

if curr == nil {
return false

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2829-L2830

Added lines #L2829 - L2830 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?

// skip it if it's a local identifier
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
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 *BranchStmt:
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
case *ValueDecl:
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
for _, rh := range s.Values {
un := findUndefined2SkipLocals(store, last, rh, t)

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2855-L2858

Added lines #L2855 - L2858 were not covered by tests

if un != "" {
return un

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2860-L2861

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

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2864-L2866

Added lines #L2864 - L2866 were not covered by tests

if un != "" {
return un

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2868-L2869

Added lines #L2868 - L2869 were not covered by tests
}
}
case *IncDecStmt:
un := findUndefined2SkipLocals(store, last, s.X, t)

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2872-L2873

Added lines #L2872 - L2873 were not covered by tests

if un != "" {
return un

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2875-L2876

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

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2878-L2879

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

if un != "" {
return un

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2881-L2882

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

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2884-L2886

Added lines #L2884 - L2886 were not covered by tests

if un != "" {
return un

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2888-L2889

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

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2892-L2893

Added lines #L2892 - L2893 were not covered by tests

if un != "" {
return un

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2895-L2896

Added lines #L2895 - L2896 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2899-L2900

Added lines #L2899 - L2900 were not covered by tests

if un != "" {
return un

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2902-L2903

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

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2909

Added line #L2909 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2914

Added line #L2914 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 2930 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2930

Added line #L2930 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 := findUndefined2SkipLocals(store, last, s.Cond, t)
if un != "" {
return un

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2955

Added line #L2955 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 2991 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2991

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

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2996-L2997

Added lines #L2996 - L2997 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 @@ -2917,6 +3111,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
80 changes: 80 additions & 0 deletions gnovm/tests/files/var21.gno
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
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"
Loading