diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim index b3d2ac3865..aa3e865ffd 100644 --- a/compiler/sempass2.nim +++ b/compiler/sempass2.nim @@ -888,8 +888,9 @@ proc trackIf(tracked: PEffects, n: PNode) = setLen(tracked.guards.s, oldFacts) dec tracked.inIfStmt -proc trackBlock(tracked: PEffects, n: PNode) = +proc trackBlock(tracked: PEffects, n: PNode; typ: PType) = if n.kind in {nkStmtList, nkStmtListExpr}: + let myBlock = tracked.currentBlock var oldState = -1 for i in 0.. 0: setLen(tracked.init, oldState) + if typ != nil and typ.kind in {tyVar, tyLent, tyOpenArray, tyVarargs}: + let last = lastSon(n) + let root = getRoot(last) + if root != nil: + let owner = tracked.scopes.getOrDefault(root.id, -1) + if owner >= 0: + localError(tracked.config, last.info, "'" & renderTree(last) & "' borrows from location '" & root.name.s & + "' which does not live long enough") else: track(tracked, n) @@ -1333,12 +1342,12 @@ proc track(tracked: PEffects, n: PNode) = tracked.init.setLen(oldState) track(tracked, n[1][0]) of nkIfStmt, nkIfExpr: trackIf(tracked, n) - of nkBlockStmt, nkBlockExpr: trackBlock(tracked, n[1]) + of nkBlockStmt, nkBlockExpr: trackBlock(tracked, n[1], n.typ) of nkWhileStmt: # 'while true' loop? inc tracked.currentBlock if isTrue(n[0]): - trackBlock(tracked, n[1]) + trackBlock(tracked, n[1], nil) else: # loop may never execute: let oldState = tracked.init.len