From abcfb6b75983c2f798cc887342ff1a9ff10c0324 Mon Sep 17 00:00:00 2001 From: "Andrey R (cooldome)" Date: Wed, 23 Sep 2020 11:46:52 +0100 Subject: [PATCH] try different approach --- compiler/semexprs.nim | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index d6b1468002..fb4cde9956 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -1754,21 +1754,19 @@ proc semReturn(c: PContext, n: PNode): PNode = if c.p.owner.kind in {skConverter, skMethod, skProc, skFunc, skMacro} or (not c.p.owner.typ.isNil and isClosureIterator(c.p.owner.typ)): if n[0].kind != nkEmpty: - if n[0].kind == nkAsgn and n[0][0].kind == nkSym and c.p.resultSym == n[0][0].sym: - discard "return is already transformed" - elif c.p.resultSym != nil: + if c.p.resultSym != nil: # transform ``return expr`` to ``result = expr; return`` - var a = newNodeI(nkAsgn, n[0].info) - a.add newSymNode(c.p.resultSym) - a.add n[0] - n[0] = a + var asgn = newNodeI(nkAsgn, n[0].info) + asgn.add newSymNode(c.p.resultSym) + asgn.add n[0] + asgn = semAsgn(c, asgn) + if asgn[1].kind == nkSym and asgn[1].sym == c.p.resultSym: + # optimize away ``result = result``: + result = newTreeI(nkReturnStmt, n.info, c.graph.emptyNode) + else: + result = newTreeI(nkStmtList, n.info, asgn, newTreeI(nkReturnStmt, n.info, c.graph.emptyNode)) else: localError(c.config, n.info, errNoReturnTypeDeclared) - return - result[0] = semAsgn(c, n[0]) - # optimize away ``result = result``: - if result[0][1].kind == nkSym and result[0][1].sym == c.p.resultSym: - result[0] = c.graph.emptyNode else: localError(c.config, n.info, "'return' not allowed here")