Skip to content

Commit

Permalink
fixes #24031; js codegen bug for case statement with just else branch (
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored Sep 2, 2024
1 parent fc853cb commit 4789af7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 7 additions & 2 deletions compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 7 additions & 0 deletions tests/casestmt/tcase_issues.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
discard """
targets: "c js"
"""

block: # bug #24031
case 0
else: discard

0 comments on commit 4789af7

Please sign in to comment.