Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disallow implicit return if result is used, don't use void context #24406

Draft
wants to merge 4 commits into
base: devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2061,7 +2061,6 @@ proc semAsgn(c: PContext, n: PNode; mode=asgnNormal): PNode =
let lhs = n[0]
let rhs = semExprWithType(c, n[1], {efTypeAllowed}, le)
if lhs.kind == nkSym and lhs.sym.kind == skResult:
n.typ() = c.enforceVoidContext
if c.p.owner.kind != skMacro and resultTypeIsInferrable(lhs.sym.typ):
var rhsTyp = rhs.typ
if rhsTyp.kind in tyUserTypeClasses and rhsTyp.isResolvedUserTypeClass:
Expand Down Expand Up @@ -2092,10 +2091,12 @@ proc semReturn(c: PContext, n: PNode): PNode =
if c.p.owner.kind in {skConverter, skMethod, skProc, skFunc, skMacro} or
(not c.p.owner.typ.isNil and isClosureIterator(c.p.owner.typ)):
if n[0].kind != nkEmpty:
var removeResultSymUsed = false
if n[0].kind == nkAsgn and n[0][0].kind == nkSym and c.p.resultSym == n[0][0].sym:
discard "return is already transformed"
elif c.p.resultSym != nil:
# transform ``return expr`` to ``result = expr; return``
removeResultSymUsed = sfUsed notin c.p.resultSym.flags
var a = newNodeI(nkAsgn, n[0].info)
a.add newSymNode(c.p.resultSym)
a.add n[0]
Expand All @@ -2104,6 +2105,8 @@ proc semReturn(c: PContext, n: PNode): PNode =
localError(c.config, n.info, errNoReturnTypeDeclared)
return
result[0] = semAsgn(c, n[0])
if removeResultSymUsed:
c.p.resultSym.flags.excl sfUsed
# optimize away ``result = result``:
if result[0][1].kind == nkSym and result[0][1].sym == c.p.resultSym:
result[0] = c.graph.emptyNode
Expand All @@ -2116,7 +2119,10 @@ proc semProcBody(c: PContext, n: PNode; expectedType: PType = nil): PNode =
return n
openScope(c)
result = semExpr(c, n, expectedType = expectedType)
if c.p.resultSym != nil and not isEmptyType(result.typ):
if c.p.resultSym != nil and
not isEmptyType(result.typ) and
sfUsed notin c.p.resultSym.flags:
incl(c.p.resultSym.flags, sfUsed)
if result.kind == nkNilLit:
# or ImplicitlyDiscardable(result):
# new semantic: 'result = x' triggers the void context
Expand Down
1 change: 0 additions & 1 deletion compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1897,7 +1897,6 @@ proc addResult(c: PContext, n: PNode, t: PType, owner: TSymKind) =
var s = newSym(skResult, getIdent(c.cache, "result"), c.idgen,
getCurrOwner(c), n.info)
s.typ = t
incl(s.flags, sfUsed)

if owner == skMacro or t != nil:
if n.len > resultPos and n[resultPos] != nil:
Expand Down
2 changes: 1 addition & 1 deletion lib/packages/docutils/rst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2842,7 +2842,7 @@ proc readTableRow(p: var RstParser): ColSeq =
proc getColContents(p: var RstParser, colLim: ColumnLimits): string =
for i in colLim.first ..< colLim.last:
result.add(p.tok[i].symbol)
result.strip
result = result.strip

proc isValidDelimiterRow(p: var RstParser, colNum: int): bool =
let row = readTableRow(p)
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions tests/discard/tvoidcontext2.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
discard """
errormsg: '''expression '"invalid"' is of type 'string' and has to be used (or discarded)'''
line: 11
"""

proc foo(x: var string) =
x = "hello"

proc bar(): string =
foo(result)
"invalid"

echo bar()
12 changes: 12 additions & 0 deletions tests/discard/twrongvoidcontext.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
discard """
output: '''
true
'''
"""

# issue #16855

proc f(): int =
echo (result = 1; result > 0)

doAssert f() == 1
Loading