diff --git a/compiler/varpartitions.nim b/compiler/varpartitions.nim index 024b58e624..784d2d9e17 100644 --- a/compiler/varpartitions.nim +++ b/compiler/varpartitions.nim @@ -431,11 +431,15 @@ proc destMightOwn(c: var Partitions; dest: var VarIndex; n: PNode) = of nkIfStmt, nkIfExpr: for i in 0.. 0: @@ -488,8 +492,17 @@ proc destMightOwn(c: var Partitions; dest: var VarIndex; n: PNode) = # we know the result is derived from the first argument: var roots: seq[(PSym, int)] allRoots(n[1], roots, RootEscapes) - for r in roots: - connect(c, dest.sym, r[0], n[1].info) + if roots.len == 0 and c.inConditional > 0: + # when in a conditional expression, + # to ensure that the first argument isn't outlived + # by the lvalue, we need find the root, otherwise + # it is probably a local temporary + # (e.g. a return value from a call), + # we should prevent cursorfication + dest.flags.incl preventCursor + else: + for r in roots: + connect(c, dest.sym, r[0], n[1].info) else: let magic = if n[0].kind == nkSym: n[0].sym.magic else: mNone