Fix defer not not-working at top level (#10191)

This commit is contained in:
Neelesh Chandola 2019-01-07 05:21:17 +05:30 • committed by Andreas Rumpf
commit e77dd683eb
4 changed files with 4 additions and 13 deletions

View file

@ -556,8 +556,6 @@ proc isEmptyTree(n: PNode): bool =
else: result = false else: result = false
proc semStmtAndGenerateGenerics(c: PContext, n: PNode): PNode = proc semStmtAndGenerateGenerics(c: PContext, n: PNode): PNode =
if n.kind == nkDefer:
localError(c.config, n.info, "defer statement not supported at top level")
if c.topStmts == 0 and not isImportSystemStmt(c.graph, n): if c.topStmts == 0 and not isImportSystemStmt(c.graph, n):
if sfSystemModule notin c.module.flags and not isEmptyTree(n): if sfSystemModule notin c.module.flags and not isEmptyTree(n):
c.importTable.addSym c.graph.systemModule # import the "System" identifier c.importTable.addSym c.graph.systemModule # import the "System" identifier

View file

@ -2624,6 +2624,8 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
of nkStaticStmt: of nkStaticStmt:
result = semStaticStmt(c, n) result = semStaticStmt(c, n)
of nkDefer: of nkDefer:
if c.currentScope == c.topLevelScope:
localError(c.config, n.info, "defer statement not supported at top level")
n.sons[0] = semExpr(c, n.sons[0]) n.sons[0] = semExpr(c, n.sons[0])
if not n.sons[0].typ.isEmptyType and not implicitlyDiscardable(n.sons[0]): if not n.sons[0].typ.isEmptyType and not implicitlyDiscardable(n.sons[0]):
localError(c.config, n.info, "'defer' takes a 'void' expression") localError(c.config, n.info, "'defer' takes a 'void' expression")

View file

@ -9,7 +9,6 @@ import os, asyncfile, asyncdispatch
const F = "test_async.txt" const F = "test_async.txt"
removeFile(F) removeFile(F)
defer: removeFile(F)
let f = openAsync(F, fmWrite) let f = openAsync(F, fmWrite)
var futs = newSeq[Future[void]]() var futs = newSeq[Future[void]]()
for i in 1..3: for i in 1..3:
@ -17,4 +16,4 @@ for i in 1..3:
waitFor(all(futs)) waitFor(all(futs))
f.close() f.close()
echo readFile(F) echo readFile(F)
removeFile(F)

View file

@ -1,6 +1,5 @@
discard """ discard """
output: '''hi output: '''hi
hi
1 1
hi hi
2 2
@ -10,13 +9,6 @@ A'''
# bug #1742 # bug #1742
template test(): untyped =
let a = 0
defer: echo "hi"
a
let i = test()
import strutils import strutils
let x = try: parseInt("133a") let x = try: parseInt("133a")
except: -1 except: -1
@ -31,7 +23,7 @@ template atFuncEnd =
template testB(): untyped = template testB(): untyped =
let a = 0 let a = 0
defer: echo "hi" # Delete this line to make it work defer: echo "hi"
a a
proc main = proc main =