Allow single branch when statements
This commit is contained in:
parent
081dfea746
commit
2102e3b02f
2 changed files with 27 additions and 3 deletions
|
|
@ -2422,8 +2422,8 @@ proc semWhen(c: PContext, n: PNode, semCheck = true): PNode =
|
||||||
# ...
|
# ...
|
||||||
var whenNimvm = false
|
var whenNimvm = false
|
||||||
var typ = commonTypeBegin
|
var typ = commonTypeBegin
|
||||||
if n.len == 2 and n[0].kind == nkElifBranch and
|
if n.len in 1..2 and n[0].kind == nkElifBranch and (
|
||||||
n[1].kind == nkElse:
|
n.len == 1 or n[1].kind == nkElse):
|
||||||
let exprNode = n[0][0]
|
let exprNode = n[0][0]
|
||||||
if exprNode.kind == nkIdent:
|
if exprNode.kind == nkIdent:
|
||||||
whenNimvm = lookUp(c, exprNode).magic == mNimvm
|
whenNimvm = lookUp(c, exprNode).magic == mNimvm
|
||||||
|
|
@ -2461,7 +2461,10 @@ proc semWhen(c: PContext, n: PNode, semCheck = true): PNode =
|
||||||
else: illFormedAst(n, c.config)
|
else: illFormedAst(n, c.config)
|
||||||
if result == nil:
|
if result == nil:
|
||||||
result = newNodeI(nkEmpty, n.info)
|
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 =
|
proc semSetConstr(c: PContext, n: PNode, expectedType: PType = nil): PNode =
|
||||||
result = newNodeI(nkCurly, n.info)
|
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()
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue