Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #19833 #19833; transform a discardable expression into a discard statement #23516

Draft
wants to merge 1 commit into
base: devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ proc semExprNoType(c: PContext, n: PNode): PNode =
let isPush = c.config.hasHint(hintExtendedContext)
if isPush: pushInfoContext(c.config, n.info)
result = semExpr(c, n, {efWantStmt})
discardCheck(c, result, {})
result = discardCheck(c, result, {})
if isPush: popInfoContext(c.config)

proc isTypeExpr(n: PNode): bool =
Expand Down Expand Up @@ -2022,7 +2022,7 @@ proc semProcBody(c: PContext, n: PNode; expectedType: PType = nil): PNode =
a[1] = result
result = semAsgn(c, a)
else:
discardCheck(c, result, {})
result = discardCheck(c, result, {})

if c.p.owner.kind notin {skMacro, skTemplate} and
c.p.resultSym != nil and c.p.resultSym.typ.isMetaType:
Expand Down
65 changes: 50 additions & 15 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,25 @@ const
nkElifBranch, nkElifExpr, nkElseExpr, nkBlockStmt, nkBlockExpr,
nkHiddenStdConv, nkHiddenDeref}

proc implicitlyDiscardable(n: PNode): bool =
const skipForDiscardableStmt = skipForDiscardable - {nkHiddenStdConv, nkHiddenDeref}

type
DiscardableKind = enum
No, LastBlock, Discardable

proc implicitlyDiscardableClassifier(n: PNode): DiscardableKind =
var n = n
while n.kind in skipForDiscardable: n = n.lastSon
result = n.kind in nkLastBlockStmts or
(isCallExpr(n) and n[0].kind == nkSym and
sfDiscardable in n[0].sym.flags)
if n.kind in nkLastBlockStmts:
result = LastBlock
elif isCallExpr(n) and n[0].kind == nkSym and
sfDiscardable in n[0].sym.flags:
result = Discardable
else:
result = No

proc implicitlyDiscardable(n: PNode): bool =
result = implicitlyDiscardableClassifier(n) in {LastBlock, Discardable}

proc fixNilType(c: PContext; n: PNode) =
if isAtom(n):
Expand All @@ -153,13 +166,31 @@ proc fixNilType(c: PContext; n: PNode) =
for it in n: fixNilType(c, it)
n.typ = nil

proc discardCheck(c: PContext, result: PNode, flags: TExprFlags) =
proc wrapDiscardableExpr(n: PNode): PNode =
result = n
var n = n
var parent = n
var hasWork = false
while n.kind in skipForDiscardableStmt:
parent = n
n = n.lastSon
if n.kind notin skipForDiscardableStmt:
parent[^1] = newTreeI(nkDiscardStmt, n.info, n)
hasWork = true
break
if not hasWork:
result = newTreeI(nkDiscardStmt, result.info, result)

proc discardCheck(c: PContext, n: PNode, flags: TExprFlags): PNode =
result = n
if c.matchedConcept != nil or efInTypeof in flags: return

if result.typ != nil and result.typ.kind notin {tyTyped, tyVoid}:
if implicitlyDiscardable(result):
var n = newNodeI(nkDiscardStmt, result.info, 1)
n[0] = result
let kind = implicitlyDiscardableClassifier(result)
if kind == Discardable:
result = wrapDiscardableExpr(result)
elif kind == LastBlock:
discard
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 Expand Up @@ -207,7 +238,8 @@ proc semIf(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil):
else: illFormedAst(it, c.config)
if isEmptyType(typ) or typ.kind in {tyNil, tyUntyped} or
(not hasElse and efInTypeof notin flags):
for it in n: discardCheck(c, it.lastSon, flags)
for it in n:
it[^1] = discardCheck(c, it[^1], flags)
result.transitionSonsKind(nkIfStmt)
# propagate any enforced VoidContext:
if typ == c.enforceVoidContext: result.typ = c.enforceVoidContext
Expand Down Expand Up @@ -314,12 +346,14 @@ proc semTry(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil)
closeScope(c)

if isEmptyType(typ) or typ.kind in {tyNil, tyUntyped}:
discardCheck(c, n[0], flags)
for i in 1..<n.len: discardCheck(c, n[i].lastSon, flags)
n[0] = discardCheck(c, n[0], flags)
for i in 1..<n.len:
n[i][^1] = discardCheck(c, n[i][^1], flags)
if typ == c.enforceVoidContext:
result.typ = c.enforceVoidContext
else:
if n.lastSon.kind == nkFinally: discardCheck(c, n.lastSon.lastSon, flags)
if n.lastSon.kind == nkFinally:
n[^1][^1] = discardCheck(c, n[^1][^1], flags)
if not endsInNoReturn(n[0]):
n[0] = fitNode(c, typ, n[0], n[0].info)
for i in 1..last:
Expand Down Expand Up @@ -1035,7 +1069,7 @@ proc semForVars(c: PContext, n: PNode; flags: TExprFlags): PNode =
openScope(c)
n[^1] = semExprBranch(c, n[^1], flags)
if efInTypeof notin flags:
discardCheck(c, n[^1], flags)
n[^1] = discardCheck(c, n[^1], flags)
closeScope(c)
c.p.breakInLoop = oldBreakInLoop
dec(c.p.nestedLoopCounter)
Expand Down Expand Up @@ -1244,7 +1278,8 @@ proc semCase(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil
closeScope(c)
if isEmptyType(typ) or typ.kind in {tyNil, tyUntyped} or
(not hasElse and efInTypeof notin flags):
for i in 1..<n.len: discardCheck(c, n[i].lastSon, flags)
for i in 1..<n.len:
n[i][^1] = discardCheck(c, n[i][^1], flags)
# propagate any enforced VoidContext:
if typ == c.enforceVoidContext:
result.typ = c.enforceVoidContext
Expand Down Expand Up @@ -2733,7 +2768,7 @@ proc semStmtList(c: PContext, n: PNode, flags: TExprFlags, expectedType: PType =
n.typ = n[i].typ
if not isEmptyType(n.typ): n.transitionSonsKind(nkStmtListExpr)
elif i != last or voidContext:
discardCheck(c, n[i], flags)
n[i] = discardCheck(c, n[i], flags)
else:
n.typ = n[i].typ
if not isEmptyType(n.typ): n.transitionSonsKind(nkStmtListExpr)
Expand Down