more checking for 'var T' as return type; refs #7373

This commit is contained in:
Andreas Rumpf 2018-03-24 08:36:58 +01:00
commit 6f747674be
4 changed files with 50 additions and 3 deletions

View file

@ -1301,13 +1301,20 @@ proc propertyWriteAccess(c: PContext, n, nOrig, a: PNode): PNode =
#analyseIfAddressTakenInCall(c, result)
proc takeImplicitAddr(c: PContext, n: PNode; isLent: bool): PNode =
# See RFC #7373, calls returning 'var T' are assumed to
# return a view into the first argument (if there is one):
let root = exprRoot(n)
if root != nil and root.owner == c.p.owner and
root.kind in {skLet, skVar, skTemp} and sfGlobal notin root.flags:
localError(n.info, "'$1' escapes its stack frame; context: '$2'" % [
root.name.s, renderTree(n, {renderNoComments})])
case n.kind
of nkHiddenAddr, nkAddr: return n
of nkHiddenDeref, nkDerefExpr: return n.sons[0]
of nkBracketExpr:
if len(n) == 1: return n.sons[0]
else: discard
var valid = isAssignable(c, n)
let valid = isAssignable(c, n)
if valid != arLValue:
if valid == arLocalLValue:
localError(n.info, errXStackEscape, renderTree(n, {renderNoComments}))