Skip to content

Commit

Permalink
completely consider void, giving discard check error again
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Nov 4, 2024
1 parent 9d5c443 commit 46206d6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
13 changes: 4 additions & 9 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2119,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 All @@ -2131,14 +2134,6 @@ proc semProcBody(c: PContext, n: PNode; expectedType: PType = nil): PNode =
# are not expressions:
fixNilType(c, result)
else:
if sfUsed in c.p.resultSym.flags:
var last = result
while last.kind in {nkStmtList, nkStmtListExpr}:
last = last.lastSon
localError(c.config, last.info, "cannot use implicit return, " &
"the `result` symbol was used in '" & c.p.owner.name.s & "'; " &
"got expression of type '" & last.typ.typeToString & "'")
incl(c.p.resultSym.flags, sfUsed)
var a = newNodeI(nkAsgn, n.info, 2)
a[0] = newSymNode(c.p.resultSym)
a[1] = result
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
8 changes: 6 additions & 2 deletions tests/discard/tvoidcontext1.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
discard """
errormsg: '''expression '"invalid"' is of type 'string' and has to be used (or discarded)'''
line: 12
"""

proc valid*(): string =
let x = 317
"valid"

proc invalid*(): string =
result = "foo"
"invalid" #[tt.Error
^ cannot use implicit return, the `result` symbol was used in 'invalid'; got expression of type 'string']#
"invalid"
8 changes: 6 additions & 2 deletions tests/discard/tvoidcontext2.nim
Original file line number Diff line number Diff line change
@@ -1,9 +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" #[tt.Error
^ cannot use implicit return, the `result` symbol was used in 'bar'; got expression of type 'string']#
"invalid"

echo bar()

0 comments on commit 46206d6

Please sign in to comment.