Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS CPS transformer crash: no statements in binding of a match #842

Open
jiribenes opened this issue Feb 21, 2025 · 0 comments
Open

JS CPS transformer crash: no statements in binding of a match #842

jiribenes opened this issue Feb 21, 2025 · 0 comments
Labels
area:compiler area:js bug Something isn't working

Comments

@jiribenes
Copy link
Contributor

jiribenes commented Feb 21, 2025

Context: I was testing #841 with fun examples on my drive :)

The following program (the repro from #770!) when compiled with --no-optimize:

type Answer { Yes(); No() }

def println(ans: Answer): Unit = ans match {
  case Yes() => println("y")
  case No()  => println("n")
}

def join(left: Answer, right: Answer): Answer = (left, right) match {
  case (Yes(), Yes()) => Yes()
  case (No() ,     _) => No()
  case (_    , No() ) => No()
}

def main() = println(join(Yes(), No()))

returns an error in generator.js.TransformerCps:

Exception in thread "main" java.util.NoSuchElementException: last of empty list
        at scala.collection.immutable.Nil$.last(List.scala:665)
        at scala.collection.immutable.Nil$.last(List.scala:661)
        at effekt.generator.js.TransformerCps$.$anonfun$7(TransformerCps.scala:253)
        at scala.collection.immutable.List.map(List.scala:250)
        at effekt.generator.js.TransformerCps$.toJS(TransformerCps.scala:257)
        at effekt.generator.js.TransformerCps$.toJS$$anonfun$17(TransformerCps.scala:226)
        at effekt.generator.js.Tree$package$.stmts(Tree.scala:253)
        at effekt.generator.js.TransformerCps$.toJS$$anonfun$17(TransformerCps.scala:226)
        at effekt.generator.js.Tree$package$.stmts(Tree.scala:253)
        at effekt.generator.js.TransformerCps$.toJS$$anonfun$17(TransformerCps.scala:226)
        at effekt.generator.js.Tree$package$.stmts(Tree.scala:253)
        at effekt.generator.js.TransformerCps$.toJS$$anonfun$17(TransformerCps.scala:226)
        at effekt.generator.js.Tree$package$.stmts(Tree.scala:253)

The error comes from the stmts.last here:

// (function () { switch (sc.tag) { case 0: return f17.apply(null, sc.data) }
case cps.Stmt.Match(sc, clauses, default) =>
val scrutinee = toJS(sc)
pure(js.Switch(js.Member(scrutinee, `tag`),
clauses.map { case (tag, clause) =>
val (e, binding) = toJS(scrutinee, tag, clause);
val stmts = binding.stmts
stmts.last match {
case terminator : (js.Stmt.Return | js.Stmt.Break | js.Stmt.Continue) => (e, stmts)
case other => (e, stmts :+ js.Break())
}
},
default.map { s => toJS(s).stmts }) :: Nil)

@jiribenes jiribenes added area:compiler area:js bug Something isn't working labels Feb 21, 2025
@jiribenes jiribenes changed the title Crash in JS transformer Crash in JS CPS transformer Feb 21, 2025
@jiribenes jiribenes changed the title Crash in JS CPS transformer JS CPS transformer crash: no statements in binding of a match Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:compiler area:js bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant