fixes #23775; injectdestructors now handles discardable statements (#23780)

fixes #23775
This commit is contained in:
ringabout
2024-07-02 14:49:37 +08:00
committed by GitHub
parent 96aba18963
commit fe3039410f
3 changed files with 27 additions and 2 deletions

View File

@@ -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)
@@ -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)

View File

@@ -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: " &

View File

@@ -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)