diff options
author | Can Lehmann <85876381+can-lehmann@users.noreply.github.com> | 2022-10-17 08:01:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-17 08:01:53 +0200 |
commit | 2102e3b02f88e006494d66fbe474161bc151a1dc (patch) | |
tree | dda37bd14e22e08cb524d9067eaee4a9e099f5a7 | |
parent | 081dfea746d77f838dc60aecaf578abbba838ec5 (diff) | |
download | Nim-2102e3b02f88e006494d66fbe474161bc151a1dc.tar.gz |
Fix #12517 Allow single branch when nimvm statements (#20577)
Allow single branch when statements
-rw-r--r-- | compiler/semexprs.nim | 9 | ||||
-rw-r--r-- | tests/whenstmt/t12517.nim | 21 |
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() + |