@@ -888,8 +888,9 @@ proc trackIf(tracked: PEffects, n: PNode) =
888888 setLen (tracked.guards.s, oldFacts)
889889 dec tracked.inIfStmt
890890
891- proc trackBlock (tracked: PEffects , n: PNode ) =
891+ proc trackBlock (tracked: PEffects , n: PNode ; typ: PType ) =
892892 if n.kind in {nkStmtList, nkStmtListExpr}:
893+ let myBlock = tracked.currentBlock
893894 var oldState = - 1
894895 for i in 0 ..< n.len:
895896 if hasSubnodeWith (n[i], nkBreakStmt):
@@ -901,6 +902,14 @@ proc trackBlock(tracked: PEffects, n: PNode) =
901902 if oldState < 0 : oldState = tracked.init.len
902903 track (tracked, n[i])
903904 if oldState > 0 : setLen (tracked.init, oldState)
905+ if typ != nil and typ.kind in {tyVar, tyLent, tyOpenArray, tyVarargs}:
906+ let last = lastSon (n)
907+ let root = getRoot (last)
908+ if root != nil :
909+ let owner = tracked.scopes.getOrDefault (root.id, - 1 )
910+ if owner >= 0 :
911+ localError (tracked.config, last.info, " '" & renderTree (last) & " ' borrows from location '" & root.name.s &
912+ " ' which does not live long enough" )
904913 else :
905914 track (tracked, n)
906915
@@ -1333,12 +1342,12 @@ proc track(tracked: PEffects, n: PNode) =
13331342 tracked.init.setLen (oldState)
13341343 track (tracked, n[1 ][0 ])
13351344 of nkIfStmt, nkIfExpr: trackIf (tracked, n)
1336- of nkBlockStmt, nkBlockExpr: trackBlock (tracked, n[1 ])
1345+ of nkBlockStmt, nkBlockExpr: trackBlock (tracked, n[1 ], n.typ )
13371346 of nkWhileStmt:
13381347 # 'while true' loop?
13391348 inc tracked.currentBlock
13401349 if isTrue (n[0 ]):
1341- trackBlock (tracked, n[1 ])
1350+ trackBlock (tracked, n[1 ], nil )
13421351 else :
13431352 # loop may never execute:
13441353 let oldState = tracked.init.len
0 commit comments