-
Notifications
You must be signed in to change notification settings - Fork 39
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
varpartitions: add support for if/try/case paths #1455
base: devel
Are you sure you want to change the base?
Conversation
This commit adds basic support for paths in conditional cases. Conditionals are considered valid paths when all branches of execution that does not terminate execution yields the same path.
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.
Just some initial thoughts
break samePathCheck | ||
|
||
let remainderStart = if n.kind == nkCaseStmt: 2 else: 1 | ||
for idx in remainderStart ..< n.len: |
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.
Any trailing finally
should be removed from the iteration since it's not part of the expression, as in it will always be a statement, right?
# Only check branches with a type, as no types implies noreturn for | ||
# expressions |
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.
They're either noreturn or unit, the difference being that the latter doesn't "abort" the current expression like the former does. You might need to rely on the endsInNoReturn
analysis.
@@ -362,6 +362,40 @@ proc pathExpr(node: PNode; owner: PSym): PNode = | |||
break | |||
else: | |||
break | |||
of nkElifExpr, nkElifBranch, nkElseExpr, nkElse, nkExceptBranch, nkOfBranch: |
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.
Should this dig into nkBlock
expression and statement varieties?
In my opinion, extending the path expression specification to have A simpler yet more useful solution would be to allow proc borrowFromComplex(c: var Partitions; dest: PSym, src: PNode) =
## Borrows from all relevant sub-expressions of `src`.
case src.kind
of nkStmtListExpr, nkBlockExpr, nkExceptBranch:
borrowFromComplex(c, dest, src[^1])
of nkIfExpr:
for it in src.items:
borrowFromComplex(c, dest, it[^1])
of nkCaseStmt:
for (_, it) in branches(src):
borrowFromComplex(c, dest, it[^1])
of nkTryStmt:
for i in 0..<(src.len - ord(src[^1].kind == nkFinally)):
borrowFromComplex(c, dest, src[i])
of nkHiddenAddr:
# currently necessary to skip the outermost hidden-addr
borrowFromComplex(c, dest, src[0])
elif endsInNoReturn(src):
discard "nothing to do"
else:
borrowFrom(c, dest, src) and then changing nimskull/compiler/sem/varpartitions.nim Line 594 in 677964c
borrowFromComplex(c, dest.sym, src)
You'll still get a run-time or compiler crash when doing so, since |
Summary
This commit adds basic support for paths in conditional cases. Conditionals are considered valid paths when all branches of execution that does not terminate execution yields the same path.
Details
Fixes full_issue_url
Notes for Reviewers
Todo list: