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()
+
k K. Agaram <vc@akkartik.com> 2022-06-29 17:52:40 -0700 new test: dragging and dropping a file on lines.love' href='/akkartik/text.love/commit/main_tests.lua?id=bfbe73e0efd2f80d5a2402ced3a93d6748319fbd'>bfbe73e ^
d009390 ^

3b36093 ^
d009390 ^
8bbc1ff ^

b700021 ^
d009390 ^





















1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73