Files
Nim/tests/whenstmt/twhen.nim
Saem Ghani 2aba116bbc when statements branches exit early (#17143)
When statement branches exit early outside of nimvm. In nimvm it seems that all
sides of the branches must be evaluated as the code gen happens at a later
stage, this remains intact.
2021-02-22 12:27:23 +01:00

48 lines
754 B
Nim

discard """
nimout: '''
nimvm - when
nimvm - whenElif
nimvm - whenElse
'''
output: '''
when
whenElif
whenElse
'''
"""
# test both when and when nimvm to ensure proper evaluation
proc compileOrRuntimeProc(s: string) =
when nimvm:
echo "nimvm - " & s
else:
echo s
template output(s: string) =
static:
compileOrRuntimeProc(s)
compileOrRuntimeProc(s)
when compiles(1):
output("when")
elif compiles(2):
output("fail - whenElif")
else:
output("fail - whenElse")
when compiles(nonexistent):
output("fail - when")
elif compiles(1):
output("whenElif")
else:
output("fail - whenElse")
when compiles(nonexistent):
output("fail - when")
elif compiles(nonexistent):
output("fail - whenElif")
else:
output("whenElse")