Minor changes to discardable handling (#8155)

This commit is contained in:
LemonBoy 2018-07-01 15:27:14 +02:00 • committed by Andreas Rumpf
commit dbbe311e18
3 changed files with 17 additions and 21 deletions

View file

@ -908,19 +908,6 @@ proc buildEchoStmt(c: PContext, n: PNode): PNode =
proc semExprNoType(c: PContext, n: PNode): PNode = proc semExprNoType(c: PContext, n: PNode): PNode =
result = semExpr(c, n, {efWantStmt}) result = semExpr(c, n, {efWantStmt})
# make an 'if' expression an 'if' statement again for backwards
# compatibility (.discardable was a bad idea!); bug #6980
var isStmt = false
if result.kind == nkIfExpr:
isStmt = true
for condActionPair in result:
let action = condActionPair.lastSon
if not implicitlyDiscardable(action) and not
endsInNoReturn(action):
isStmt = false
if isStmt:
result.kind = nkIfStmt
result.typ = nil
discardCheck(c, result) discardCheck(c, result)
proc isTypeExpr(n: PNode): bool = proc isTypeExpr(n: PNode): bool =

View file

@ -111,13 +111,15 @@ proc semExprBranchScope(c: PContext, n: PNode): PNode =
const const
skipForDiscardable = {nkIfStmt, nkIfExpr, nkCaseStmt, nkOfBranch, skipForDiscardable = {nkIfStmt, nkIfExpr, nkCaseStmt, nkOfBranch,
nkElse, nkStmtListExpr, nkTryStmt, nkFinally, nkExceptBranch, nkElse, nkStmtListExpr, nkTryStmt, nkFinally, nkExceptBranch,
nkElifBranch, nkElifExpr, nkElseExpr, nkBlockStmt, nkBlockExpr} nkElifBranch, nkElifExpr, nkElseExpr, nkBlockStmt, nkBlockExpr,
nkHiddenStdConv}
proc implicitlyDiscardable(n: PNode): bool = proc implicitlyDiscardable(n: PNode): bool =
var n = n var n = n
while n.kind in skipForDiscardable: n = n.lastSon while n.kind in skipForDiscardable: n = n.lastSon
result = isCallExpr(n) and n.sons[0].kind == nkSym and result = n.kind == nkRaiseStmt or
sfDiscardable in n.sons[0].sym.flags (isCallExpr(n) and n.sons[0].kind == nkSym and
sfDiscardable in n.sons[0].sym.flags)
proc fixNilType(c: PContext; n: PNode) = proc fixNilType(c: PContext; n: PNode) =
if isAtom(n): if isAtom(n):
@ -132,11 +134,8 @@ proc discardCheck(c: PContext, result: PNode) =
if c.matchedConcept != nil: return if c.matchedConcept != nil: return
if result.typ != nil and result.typ.kind notin {tyStmt, tyVoid}: if result.typ != nil and result.typ.kind notin {tyStmt, tyVoid}:
if implicitlyDiscardable(result): if implicitlyDiscardable(result):
var n = result var n = newNodeI(nkDiscardStmt, result.info, 1)
result.typ = nil n[0] = result
while n.kind in skipForDiscardable:
n = n.lastSon
n.typ = nil
elif result.typ.kind != tyError and c.config.cmd != cmdInteractive: elif result.typ.kind != tyError and c.config.cmd != cmdInteractive:
var n = result var n = result
while n.kind in skipForDiscardable: n = n.lastSon while n.kind in skipForDiscardable: n = n.lastSon

View file

@ -151,3 +151,13 @@ if true:
fooBool() fooBool()
else: else:
raise newException(ValueError, "argh") raise newException(ValueError, "argh")
# bug #5374
proc test1(): int64 {.discardable.} = discard
proc test2(): int {.discardable.} = discard
if true:
test1()
else:
test2()