summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorLemonBoy <LemonBoy@users.noreply.github.com>2018-09-07 21:07:06 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-09-07 21:07:06 +0200
commit91b37311d9974ae30745946a52c9a971da1616f4 (patch)
tree705c74fa395ca8e538bd906ca56e3fac08749231
parenteae3c305a775efc2d04495199704957964f3a644 (diff)
downloadNim-91b37311d9974ae30745946a52c9a971da1616f4.tar.gz
Fix AST generation for case statements (#8908)
Fixes #7534
-rw-r--r--compiler/transf.nim6
-rw-r--r--tests/js/t7534.nim7
2 files changed, 12 insertions, 1 deletions
diff --git a/compiler/transf.nim b/compiler/transf.nim
index 84297aa6a..347df3e49 100644
--- a/compiler/transf.nim
+++ b/compiler/transf.nim
@@ -624,7 +624,11 @@ proc transformCase(c: PTransf, n: PNode): PTransNode =
     case it.kind
     of nkElifBranch:
       if ifs.PNode == nil:
-        ifs = newTransNode(nkIfStmt, it.info, 0)
+        # Generate the right node depending on whether `n` is used as a stmt or
+        # as an expr
+        let kind = if n.typ != nil: nkIfExpr else: nkIfStmt
+        ifs = newTransNode(kind, it.info, 0)
+        ifs.PNode.typ = n.typ
       ifs.add(e)
     of nkElse:
       if ifs.PNode == nil: result.add(e)
diff --git a/tests/js/t7534.nim b/tests/js/t7534.nim
new file mode 100644
index 000000000..64aadb8d6
--- /dev/null
+++ b/tests/js/t7534.nim
@@ -0,0 +1,7 @@
+proc f(x: int): int =
+  result = case x
+    of 1: 2
+    elif x == 2: 3
+    else: 1
+
+doAssert 2 == f(f(f(f(1))))