From a76f36b4e511f2131a9b04cbcb00f8f35f4bac1b Mon Sep 17 00:00:00 2001 From: Can Lehmann <85876381+can-lehmann@users.noreply.github.com> Date: Mon, 17 Oct 2022 08:01:53 +0200 Subject: [PATCH] Fix #12517 Allow single branch when nimvm statements (#20577) Allow single branch when statements (cherry picked from commit 2102e3b02f88e006494d66fbe474161bc151a1dc) --- compiler/semexprs.nim | 9 ++++++--- tests/whenstmt/t12517.nim | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 tests/whenstmt/t12517.nim diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 85327af201..2c1d40ff25 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -2418,8 +2418,8 @@ proc semWhen(c: PContext, n: PNode, semCheck = true): PNode = # ... var whenNimvm = false var typ = commonTypeBegin - if n.len == 2 and n[0].kind == nkElifBranch and - n[1].kind == nkElse: + if n.len in 1..2 and n[0].kind == nkElifBranch and ( + n.len == 1 or n[1].kind == nkElse): let exprNode = n[0][0] if exprNode.kind == nkIdent: whenNimvm = lookUp(c, exprNode).magic == mNimvm @@ -2457,7 +2457,10 @@ proc semWhen(c: PContext, n: PNode, semCheck = true): PNode = else: illFormedAst(n, c.config) if result == nil: result = newNodeI(nkEmpty, n.info) - if whenNimvm: result.typ = typ + if whenNimvm: + result.typ = typ + if n.len == 1: + result.add(newTree(nkElse, newNode(nkStmtList))) proc semSetConstr(c: PContext, n: PNode): PNode = result = newNodeI(nkCurly, n.info) diff --git a/tests/whenstmt/t12517.nim b/tests/whenstmt/t12517.nim new file mode 100644 index 0000000000..8be0171e1b --- /dev/null +++ b/tests/whenstmt/t12517.nim @@ -0,0 +1,21 @@ +# Test based on issue #12517 + +discard """ + nimout: ''' +nimvm +both +''' + output: ''' +both +''' +""" + +proc test() = + when nimvm: + echo "nimvm" + echo "both" + +static: + test() +test() +