From 4789af71fe107e137524bc7746b902e644aca2dc Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 3 Sep 2024 00:10:01 +0800 Subject: [PATCH] fixes #24031; js codegen bug for case statement with just else branch (#24047) fixes #24031 --- compiler/jsgen.nim | 9 +++++++-- tests/casestmt/tcase_issues.nim | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 tests/casestmt/tcase_issues.nim diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index d6310d5847f4b..e3c0b1f463261 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -1098,14 +1098,19 @@ proc genCaseJS(p: PProc, n: PNode, r: var TCompRes) = lineF(p, "break;$n", []) of nkElse: if transferRange: - lineF(p, "else{$n", []) + if n.len == 2: # a dangling else for a case statement + transferRange = false + lineF(p, "switch ($1) {$n", [cond.rdLoc]) + lineF(p, "default: $n", []) + else: + lineF(p, "else{$n", []) else: lineF(p, "default: $n", []) p.nested: gen(p, it[0], stmt) moveInto(p, stmt, r) if transferRange: - lineF(p, "}$n", []) + lineF(p, "}$n", []) else: lineF(p, "break;$n", []) else: internalError(p.config, it.info, "jsgen.genCaseStmt") diff --git a/tests/casestmt/tcase_issues.nim b/tests/casestmt/tcase_issues.nim new file mode 100644 index 0000000000000..20a79df2c577f --- /dev/null +++ b/tests/casestmt/tcase_issues.nim @@ -0,0 +1,7 @@ +discard """ + targets: "c js" +""" + +block: # bug #24031 + case 0 + else: discard \ No newline at end of file