-
Notifications
You must be signed in to change notification settings - Fork 375
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: branch stmts #3043
Open
petar-dambovaliev
wants to merge
12
commits into
master
Choose a base branch
from
2973
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix: branch stmts #3043
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c5dcb0a
2973: fix switch branch stmts
petar-dambovaliev 6034015
fix test
petar-dambovaliev 500197e
fix test
petar-dambovaliev 1b71268
fix tests
petar-dambovaliev 348b249
fix tests
petar-dambovaliev 26862b6
save
petar-dambovaliev 6b1356a
Merge branch 'master' into 2973
petar-dambovaliev 477e08b
save
petar-dambovaliev 4a7f2be
save
petar-dambovaliev 91ea275
save
petar-dambovaliev 5913985
save
petar-dambovaliev 919f650
save
petar-dambovaliev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -298,6 +298,11 @@ func initStaticBlocks(store Store, ctx BlockNode, bn BlockNode) { | |
last.Predefine(false, n.VarName) | ||
} | ||
case *SwitchClauseStmt: | ||
blen := len(n.Body) | ||
if blen > 0 { | ||
n.Body[blen-1].SetAttribute(ATTR_LAST_BLOCK_STMT, true) | ||
} | ||
|
||
// parent switch statement. | ||
ss := ns[len(ns)-1].(*SwitchStmt) | ||
// anything declared in ss are copied, | ||
|
@@ -2125,8 +2130,19 @@ func preprocess1(store Store, ctx BlockNode, n Node) Node { | |
|
||
// TRANS_LEAVE ----------------------- | ||
case *BranchStmt: | ||
|
||
petar-dambovaliev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
notAllowedFunc := func(s string) { | ||
_, isFunc := last.(*FuncLitExpr) | ||
|
||
if isFunc { | ||
panic(fmt.Sprintf("%s statement out of place", s)) | ||
} | ||
} | ||
|
||
switch n.Op { | ||
case BREAK: | ||
notAllowedFunc("break") | ||
petar-dambovaliev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if n.Label == "" { | ||
if !findBreakableNode(ns) { | ||
panic("cannot break with no parent loop or switch") | ||
|
@@ -2139,6 +2155,8 @@ func preprocess1(store Store, ctx BlockNode, n Node) Node { | |
} | ||
} | ||
case CONTINUE: | ||
notAllowedFunc("continue") | ||
petar-dambovaliev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if n.Label == "" { | ||
if !findContinuableNode(ns) { | ||
panic("cannot continue with no parent loop") | ||
|
@@ -2154,17 +2172,36 @@ func preprocess1(store Store, ctx BlockNode, n Node) Node { | |
n.Depth = depth | ||
n.BodyIndex = index | ||
case FALLTHROUGH: | ||
if swchC, ok := last.(*SwitchClauseStmt); ok { | ||
// last is a switch clause, find its index in the switch and assign | ||
// it to the fallthrough node BodyIndex. This will be used at | ||
// runtime to determine the next switch clause to run. | ||
swch := lastSwitch(ns) | ||
for i := range swch.Clauses { | ||
if &swch.Clauses[i] == swchC { | ||
// switch clause found | ||
n.BodyIndex = i | ||
break | ||
} | ||
swchC, ok := last.(*SwitchClauseStmt) | ||
if !ok { | ||
// fallthrough is only allowed in a switch statement | ||
panic("fallthrough statement out of place") | ||
} | ||
|
||
if n.GetAttribute(ATTR_LAST_BLOCK_STMT) != true { | ||
// no more clause after the one executed, this is not allowed | ||
panic("fallthrough statement out of place") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any test targeting this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are several tests. grep for |
||
} | ||
|
||
// last is a switch clause, find its index in the switch and assign | ||
// it to the fallthrough node BodyIndex. This will be used at | ||
// runtime to determine the next switch clause to run. | ||
swch := lastSwitch(ns) | ||
|
||
if swch.IsTypeSwitch { | ||
// fallthrough is not allowed in type switches | ||
panic("cannot fallthrough in type switch") | ||
} | ||
|
||
for i := range swch.Clauses { | ||
if i == len(swch.Clauses)-1 { | ||
panic("cannot fallthrough final case in switch") | ||
} | ||
|
||
if &swch.Clauses[i] == swchC { | ||
// switch clause found | ||
n.BodyIndex = i | ||
break | ||
} | ||
} | ||
default: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
func main() { | ||
for i := 0; i < 10; i++ { | ||
if i == 1 { | ||
_ = func() int { | ||
continue | ||
return 11 | ||
}() | ||
} | ||
println(i) | ||
} | ||
println("wat???") | ||
} | ||
|
||
// Error: | ||
// main/files/for21.gno:7:17: continue statement out of place |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
func main() { | ||
for i := 0; i < 10; i++ { | ||
if i == 1 { | ||
_ = func() int { | ||
fallthrough | ||
return 11 | ||
}() | ||
} | ||
println(i) | ||
} | ||
println("wat???") | ||
} | ||
|
||
// Error: | ||
// main/files/for22.gno:7:17: fallthrough statement out of place |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
func main() { | ||
for i := 0; i < 10; i++ { | ||
if i == 1 { | ||
_ = func() int { | ||
break | ||
return 11 | ||
}() | ||
} | ||
println(i) | ||
} | ||
println("wat???") | ||
} | ||
|
||
// Error: | ||
// main/files/for23.gno:7:17: break statement out of place |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
is there a better name?
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 am open to suggestions