-
Notifications
You must be signed in to change notification settings - Fork 374
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
feat(gnovm): solution for correctly capturing loop externs #1818
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1818 +/- ##
==========================================
- Coverage 48.82% 48.67% -0.16%
==========================================
Files 579 579
Lines 78045 78310 +265
==========================================
+ Hits 38107 38116 +9
- Misses 36852 37103 +251
- Partials 3086 3091 +5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
for _, loop := range loops { | ||
if loop.isGotoLoop { // for/range has done the work | ||
// find label stmt | ||
lblstmt, idx := body.GetLabeledStmt(loop.label) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think label conflicts are possible; same label used in different places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This search is conducted within specific block nodes such as funcDecl, forStmt, etc., and not within a fileNode. Thus, each label should be unique within the scope of the search. Did I misunderstand your question?
also, if jump from different places to same label stmt, it's also working, see this case:
package main
import (
"fmt"
)
func main() {
i := 0
var fs []func()
defer func() {
for _, ff := range fs {
ff()
}
}()
if i == 0 {
goto Start
}
Start:
if i <= 5 {
x := i
fmt.Println("i: ", i)
i++
fs = append(fs, func() { println(x) })
goto Start // Jumps back to the label "Start"
}
if i <= 10 {
x := i
fmt.Println("Counting past 5:", i)
i++
fs = append(fs, func() { println(x) })
goto Start // Another jump back to the label "Start"
}
fmt.Println("Finished counting")
}
// Output:
// i: 0
// i: 1
// i: 2
// i: 3
// i: 4
// i: 5
// Counting past 5: 6
// Counting past 5: 7
// Counting past 5: 8
// Counting past 5: 9
// Counting past 5: 10
// Finished counting
// 0
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
// 10
@@ -364,6 +416,8 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { | |||
last.Define(Name(rn), anyValue(rf.Type)) | |||
} | |||
} | |||
// record, mostly used by goto... that with funcLit embedded within. | |||
closureStack = append(closureStack, last) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't unwind, weird.
it's not a stack if it doesn't get unstacked, right?
|
||
// for goto... | ||
// wrap goto loop into block stmt. | ||
func rebuildBody(b Body, gloop *LoopInfo, loc Location) Body { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unfortunately, i don't think this works for the following kind of goto-loop.
LABEL1:
...
LABEL2:
...
if cond1 {
goto LABEL1
}
...
if cond2 {
goto LABEL2
}
notice how goto LABEL1 and goto LABEL2 make two loops that intersect, but one loop is not nested within the other.
Here's another example where Go and Gno currently differs, without any closures: func main() {
az := []*int{}
for i := 0; i < 10; i++ {
it := i
az = append(az, &it)
}
println(az)
for idx, v := range az {
println(idx, *v)
}
} Even referencing a loop-declared differs in behavior; in Go, &it is a new value location, for every loop iteration. |
close due to theoretical incompleteness. |
Address #1135.
This is the latest effort in a series of attempts: #1585, #1768, #1780.
The main idea is following the talk with Jae that:
The process can be break down into 3 steps:
if the loopVar
i
is references by the funcLit, we'll need to inject another stmt ofi := i
, to be:clear all the preprocessed memos, value-paths, staticBlocks, and REDO the preprocess work.(this is a cumbersome work, maybe we need a multi-phase preprocess).
cc: @jaekwon @deelawn @thehowl .