enforce 'var T' produces a view into the first parameter; refs #7373
This commit is contained in:
parent
6f747674be
commit
3be4f9111c
2 changed files with 19 additions and 4 deletions
|
|
@ -1304,10 +1304,13 @@ proc takeImplicitAddr(c: PContext, n: PNode; isLent: bool): PNode =
|
||||||
# See RFC #7373, calls returning 'var T' are assumed to
|
# See RFC #7373, calls returning 'var T' are assumed to
|
||||||
# return a view into the first argument (if there is one):
|
# return a view into the first argument (if there is one):
|
||||||
let root = exprRoot(n)
|
let root = exprRoot(n)
|
||||||
if root != nil and root.owner == c.p.owner and
|
if root != nil and root.owner == c.p.owner:
|
||||||
root.kind in {skLet, skVar, skTemp} and sfGlobal notin root.flags:
|
if root.kind in {skLet, skVar, skTemp} and sfGlobal notin root.flags:
|
||||||
localError(n.info, "'$1' escapes its stack frame; context: '$2'" % [
|
localError(n.info, "'$1' escapes its stack frame; context: '$2'" % [
|
||||||
root.name.s, renderTree(n, {renderNoComments})])
|
root.name.s, renderTree(n, {renderNoComments})])
|
||||||
|
elif root.kind == skParam and root.position != 0:
|
||||||
|
localError(n.info, "'$1' is not the first parameter; context: '$2'" % [
|
||||||
|
root.name.s, renderTree(n, {renderNoComments})])
|
||||||
case n.kind
|
case n.kind
|
||||||
of nkHiddenAddr, nkAddr: return n
|
of nkHiddenAddr, nkAddr: return n
|
||||||
of nkHiddenDeref, nkDerefExpr: return n.sons[0]
|
of nkHiddenDeref, nkDerefExpr: return n.sons[0]
|
||||||
|
|
|
||||||
12
tests/varres/twrong_parameter.nim
Normal file
12
tests/varres/twrong_parameter.nim
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
discard """
|
||||||
|
line: 6
|
||||||
|
errormsg: "'x' is not the first parameter; context: 'x'"
|
||||||
|
"""
|
||||||
|
|
||||||
|
proc forward(abc: int; x: var int): var int = result = x
|
||||||
|
|
||||||
|
proc foo(): var int =
|
||||||
|
var y = 9
|
||||||
|
result = forward(45, y)
|
||||||
|
|
||||||
|
echo foo()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue