summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorCan Lehmann <85876381+can-lehmann@users.noreply.github.com>2022-10-17 08:01:53 +0200
committerGitHub <noreply@github.com>2022-10-17 08:01:53 +0200
commit2102e3b02f88e006494d66fbe474161bc151a1dc (patch)
treedda37bd14e22e08cb524d9067eaee4a9e099f5a7
parent081dfea746d77f838dc60aecaf578abbba838ec5 (diff)
downloadNim-2102e3b02f88e006494d66fbe474161bc151a1dc.tar.gz
Fix #12517 Allow single branch when nimvm statements (#20577)
Allow single branch when statements
-rw-r--r--compiler/semexprs.nim9
-rw-r--r--tests/whenstmt/t12517.nim21
2 files changed, 27 insertions, 3 deletions
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim
index 9cbe11616..daee3dffa 100644
--- a/compiler/semexprs.nim
+++ b/compiler/semexprs.nim
@@ -2422,8 +2422,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
@@ -2461,7 +2461,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, expectedType: PType = nil): PNode =
   result = newNodeI(nkCurly, n.info)
diff --git a/tests/whenstmt/t12517.nim b/tests/whenstmt/t12517.nim
new file mode 100644
index 000000000..8be0171e1
--- /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()
+