mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-30 18:02:05 +00:00
Allow single branch when statements
(cherry picked from commit 2102e3b02f)
This commit is contained in:
@@ -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)
|
||||
|
||||
21
tests/whenstmt/t12517.nim
Normal file
21
tests/whenstmt/t12517.nim
Normal file
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user