-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -362,6 +362,40 @@ proc pathExpr(node: PNode; owner: PSym): PNode = | |
break | ||
else: | ||
break | ||
of nkElifExpr, nkElifBranch, nkElseExpr, nkElse, nkExceptBranch, nkOfBranch: | ||
n = n.lastSon | ||
of nkIfExpr, nkIfStmt, nkTryStmt, nkCaseStmt: | ||
block samePathCheck: | ||
let firstExpr = | ||
case n.kind | ||
of nkTryStmt: | ||
n[0] # First element is try body | ||
of nkCaseStmt: | ||
n[1].lastSon # First branch is the second element | ||
else: | ||
n[0].lastSon | ||
let firstValidPath = pathExpr(firstExpr, owner) | ||
# The first expression should be a valid path | ||
if firstValidPath == nil: | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Any trailing |
||
# Only check branches with a type, as no types implies noreturn for | ||
# expressions | ||
Comment on lines
+384
to
+385
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. 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 |
||
if not n[idx].lastSon.typ.isEmptyType(): | ||
# If the first path is not a symbol, then we currently can't check | ||
# if other paths points to the same resource, so bail. | ||
if firstValidPath.kind != nkSym: | ||
break samePathCheck | ||
|
||
let path = pathExpr(n[idx], owner) | ||
if path.kind != nkSym or path.sym.itemId != firstValidPath.sym.itemId: | ||
break samePathCheck | ||
|
||
result = firstValidPath | ||
|
||
break | ||
else: | ||
break | ||
# borrowFromConstExpr(n) is correct here because we need 'node' | ||
|
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?