Skip to content

Commit

Permalink
fixes #23775; injectdestructors now handles discardable statements (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored Jul 2, 2024
1 parent 96aba18 commit fe30394
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions compiler/injectdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ template handleNestedTempl(n, processCall: untyped, willProduceStmt = false,
for j in 0 ..< it.len-1:
branch[j] = copyTree(it[j])
var ofScope = nestedScope(s, it.lastSon)
branch[^1] = if it[^1].typ.isEmptyType or willProduceStmt:
branch[^1] = if n.typ.isEmptyType or it[^1].typ.isEmptyType or willProduceStmt:
processScope(c, ofScope, maybeVoid(it[^1], ofScope))
else:
processScopeExpr(c, ofScope, it[^1], processCall, tmpFlags)
Expand Down Expand Up @@ -701,7 +701,7 @@ template handleNestedTempl(n, processCall: untyped, willProduceStmt = false,
#Condition needs to be destroyed outside of the condition/branch scope
branch[0] = p(it[0], c, s, normal)

branch[^1] = if it[^1].typ.isEmptyType or willProduceStmt:
branch[^1] = if n.typ.isEmptyType or it[^1].typ.isEmptyType or willProduceStmt:
processScope(c, branchScope, maybeVoid(it[^1], branchScope))
else:
processScopeExpr(c, branchScope, it[^1], processCall, tmpFlags)
Expand Down
1 change: 1 addition & 0 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ proc discardCheck(c: PContext, result: PNode, flags: TExprFlags) =
if implicitlyDiscardable(result):
var n = newNodeI(nkDiscardStmt, result.info, 1)
n[0] = result
# notes that it doesn't transform nodes into discard statements
elif result.typ.kind != tyError and c.config.cmd != cmdInteractive:
if result.typ.kind == tyNone:
localError(c.config, result.info, "expression has no type: " &
Expand Down
24 changes: 24 additions & 0 deletions tests/discard/tdiscardable.nim
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,27 @@ block: # issue #14665
continue
inc i
test()

block: # bug #23775
proc retInt(): int {.discardable.} =
42

proc retString(): string {.discardable.} =
"text"

type
Enum = enum
A, B, C, D

proc doStuff(msg: Enum) =
case msg:
of A:
retString()
of B:
retInt()
of C:
discard retString()
else:
let _ = retString()

doStuff(C)

0 comments on commit fe30394

Please sign in to comment.