From 91b37311d9974ae30745946a52c9a971da1616f4 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Fri, 7 Sep 2018 21:07:06 +0200 Subject: [PATCH] Fix AST generation for case statements (#8908) Fixes #7534 --- compiler/transf.nim | 6 +++++- tests/js/t7534.nim | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 tests/js/t7534.nim diff --git a/compiler/transf.nim b/compiler/transf.nim index 84297aa6a1..347df3e49e 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 0000000000..64aadb8d68 --- /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))))