diff options
-rw-r--r-- | compiler/semexprs.nim | 1 | ||||
-rw-r--r-- | tests/whenstmt/twhen.nim | 47 | ||||
-rw-r--r-- | tests/whenstmt/twhen_macro.nim | 24 |
3 files changed, 72 insertions, 0 deletions
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index c1365a83d..e32e08d4b 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -2364,6 +2364,7 @@ proc semWhen(c: PContext, n: PNode, semCheck = true): PNode = discard elif e.intVal != 0 and result == nil: setResult(it[1]) + return # we're not in nimvm and we already have a result of nkElse, nkElseExpr: checkSonsLen(it, 1, c.config) if result == nil or whenNimvm: diff --git a/tests/whenstmt/twhen.nim b/tests/whenstmt/twhen.nim new file mode 100644 index 000000000..b155ddcfb --- /dev/null +++ b/tests/whenstmt/twhen.nim @@ -0,0 +1,47 @@ +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") + diff --git a/tests/whenstmt/twhen_macro.nim b/tests/whenstmt/twhen_macro.nim new file mode 100644 index 000000000..b5168144e --- /dev/null +++ b/tests/whenstmt/twhen_macro.nim @@ -0,0 +1,24 @@ +import macros + +discard """ + output: ''' +when - test +''' +""" + +# test that when stmt works from within a macro + +macro output(s: string, xs: varargs[untyped]): auto = + result = quote do: + when compiles(`s`): + "when - " & `s` + elif compiles(`s`): + "elif - " & `s` + # should never get here so this should not break + broken.xs + else: + "else - " & `s` + # should never get here so this should not break + more.broken.xs + +echo output("test") \ No newline at end of file |