108
108
# yield 0
109
109
# raise ...
110
110
# :finallyPath[LEVEL] = 3 # Exception did not happen. Our finally can continue to state 3
111
- # :state = 2 # And we continue to our finally
111
+ # :state = 2 # And we continue to our finally
112
112
# break :stateLoop
113
113
# of 1: # Except
114
+ # inc(:curExcLevel, -1) # Exception is caught
114
115
# yield 1
115
116
# :tmpResult = 3 # Return
116
117
# :finalyPath[LEVEL] = 0 # Configure finally path.
117
118
# :state = 2 # Goto Finally
118
119
# break :stateLoop
120
+ # popCurrentException() # XXX: This is likely wrong, see #25031
119
121
# :state = 2 # What would happen should we not return
120
122
# break :stateLoop
121
123
# of 2: # Finally
@@ -140,8 +142,6 @@ import
140
142
renderer, magicsys, lowerings, lambdalifting, modulegraphs, lineinfos,
141
143
options
142
144
143
- import astalgo
144
-
145
145
import std/ tables
146
146
147
147
when defined(nimPreviewSlimSystem):
@@ -344,12 +344,10 @@ proc collectExceptState(ctx: var Ctx, n: PNode): PNode {.inline.} =
344
344
else :
345
345
ifBranch = newNodeI(nkElse, c.info)
346
346
347
- ifBranch.add(c [^ 1 ])
347
+ ifBranch.add(newTreeI(nkStmtList, c.info, ctx.newChangeCurExcLevel(c.info, - 1 ), c [^ 1 ]) )
348
348
ifStmt.add(ifBranch)
349
349
350
350
if ifStmt.len != 0 :
351
- # let setupExc = newTree(nkCall, newSymNode(ctx.g.getCompilerProc("pushCurrentException")), ctx.newCurExcAccess())
352
- # result = newTree(nkStmtList, ctx.newNullifyCurExc(n.info), ifStmt)
353
351
result = newTree(nkStmtList, ifStmt)
354
352
else :
355
353
result = ctx.g.emptyNode
@@ -361,14 +359,24 @@ proc addElseToExcept(ctx: var Ctx, n, gotoOut: PNode) =
361
359
if n[0 ].kind == nkIfStmt and n[0 ][^ 1 ].kind != nkElse:
362
360
# Not all cases are covered, which means exception is not handled
363
361
# and we should go to parent exception landing state
362
+ let action =
363
+ if ctx.curFinallyLevel == 0 :
364
+ # There is no suitable finally around. We must reraise.
365
+ newTreeI(nkRaiseStmt, n.info, ctx.g.emptyNode)
366
+ else :
367
+ # Jump to finally.
368
+ newTree(nkGotoState, ctx.curExcLandingState)
369
+
364
370
n[0 ].add(newTree(nkElse,
365
- newTreeI(nkStmtList, n.info,
366
- newTree(nkGotoState, ctx.curExcLandingState))))
371
+ newTreeI(nkStmtList, n.info, action)))
367
372
368
373
# Exception is handled
374
+ # XXX: This is likely wrong. See #25031. We must clear exception not only here
375
+ # at the end of except but also when except flow is interrupted with break or return
376
+ # and if there's a raise in except flow, current exception must be replaced with the
377
+ # raised one.
369
378
n.add newTree(nkCall,
370
379
newSymNode(ctx.g.getCompilerProc(" popCurrentException" )))
371
- n.add(ctx.newChangeCurExcLevel(n.info, - 1 ))
372
380
if gotoOut != nil :
373
381
# We have a finally node following this except block, and exception is handled
374
382
# Configure its path to continue normally
@@ -848,7 +856,6 @@ proc newEndFinallyNode(ctx: var Ctx, info: TLineInfo): PNode =
848
856
newTree(nkElifBranch, excNilCmp, retStmt),
849
857
newTree(nkElse,
850
858
newTree(nkStmtList,
851
- # newTreeI(nkCall, info, newSymNode(ctx.g.getCompilerProc("closureIterSetupExc")), nilnode),
852
859
newTreeI(nkRaiseStmt, info, ctx.g.emptyNode))))
853
860
854
861
result = newTree(nkIfStmt,
@@ -1496,7 +1503,7 @@ proc transformClosureIterator*(g: ModuleGraph; idgen: IdGenerator; fn: PSym, n:
1496
1503
result = wrapIntoStateLoop(ctx, caseDispatcher)
1497
1504
result = liftLocals(ctx, result )
1498
1505
1499
- when true :
1506
+ when false :
1500
1507
echo " TRANSFORM TO STATES:"
1501
1508
echo renderTree(result )
1502
1509
0 commit comments