Skip to content

Loop doesn't terminate when compiled by dotty #7843

Closed
@laurynaslubys

Description

@laurynaslubys

minimized code

object Main {

  val thunkFuture: Future[() => String] = Future.successful(() => throw new RuntimeException("Whoops"))

  final def shouldTerminate: Future[Unit] =
    thunkFuture.flatMap { thunk =>
      def fold() = {
        var done = false
        while (!done) {
          println(s"done outside try: $done")
          try {
            thunk()
          } catch {
            case _: Throwable =>
              println("setting done to true")
              done = true
              println(s"done in catch clause: $done")
          }
        }
      }

      Future {
        fold()
      }
    }


  def main(args: Array[String]): Unit = {
    Await.result(shouldTerminate, Duration.Inf)
    println("done")
  }
}

The program doesn't terminate in 0.21.0-RC1, but terminates correctly in scala 2.12

expectation

terminates and produces output:

[info] done outside try: false
[info] setting done to true
[info] done in catch clause: true
[info] done

actual

doesn't terminate and produces output:

[info] done outside try: false
[info] setting done to true
[info] done in catch clause: true
[info] done outside try: false
[info] setting done to true
[info] done in catch clause: true
... repeated

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions