mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 05:50:30 +00:00
Minor changes to discardable handling (#8155)
This commit is contained in:
@@ -908,19 +908,6 @@ proc buildEchoStmt(c: PContext, n: PNode): PNode =
|
||||
|
||||
proc semExprNoType(c: PContext, n: PNode): PNode =
|
||||
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)
|
||||
|
||||
proc isTypeExpr(n: PNode): bool =
|
||||
|
||||
@@ -111,13 +111,15 @@ proc semExprBranchScope(c: PContext, n: PNode): PNode =
|
||||
const
|
||||
skipForDiscardable = {nkIfStmt, nkIfExpr, nkCaseStmt, nkOfBranch,
|
||||
nkElse, nkStmtListExpr, nkTryStmt, nkFinally, nkExceptBranch,
|
||||
nkElifBranch, nkElifExpr, nkElseExpr, nkBlockStmt, nkBlockExpr}
|
||||
nkElifBranch, nkElifExpr, nkElseExpr, nkBlockStmt, nkBlockExpr,
|
||||
nkHiddenStdConv}
|
||||
|
||||
proc implicitlyDiscardable(n: PNode): bool =
|
||||
var n = n
|
||||
while n.kind in skipForDiscardable: n = n.lastSon
|
||||
result = isCallExpr(n) and n.sons[0].kind == nkSym and
|
||||
sfDiscardable in n.sons[0].sym.flags
|
||||
result = n.kind == nkRaiseStmt or
|
||||
(isCallExpr(n) and n.sons[0].kind == nkSym and
|
||||
sfDiscardable in n.sons[0].sym.flags)
|
||||
|
||||
proc fixNilType(c: PContext; n: PNode) =
|
||||
if isAtom(n):
|
||||
@@ -132,11 +134,8 @@ proc discardCheck(c: PContext, result: PNode) =
|
||||
if c.matchedConcept != nil: return
|
||||
if result.typ != nil and result.typ.kind notin {tyStmt, tyVoid}:
|
||||
if implicitlyDiscardable(result):
|
||||
var n = result
|
||||
result.typ = nil
|
||||
while n.kind in skipForDiscardable:
|
||||
n = n.lastSon
|
||||
n.typ = nil
|
||||
var n = newNodeI(nkDiscardStmt, result.info, 1)
|
||||
n[0] = result
|
||||
elif result.typ.kind != tyError and c.config.cmd != cmdInteractive:
|
||||
var n = result
|
||||
while n.kind in skipForDiscardable: n = n.lastSon
|
||||
|
||||
@@ -151,3 +151,13 @@ if true:
|
||||
fooBool()
|
||||
else:
|
||||
raise newException(ValueError, "argh")
|
||||
|
||||
# bug #5374
|
||||
|
||||
proc test1(): int64 {.discardable.} = discard
|
||||
proc test2(): int {.discardable.} = discard
|
||||
|
||||
if true:
|
||||
test1()
|
||||
else:
|
||||
test2()
|
||||
|
||||
Reference in New Issue
Block a user