Uploaded image for project: 'Scala Programming Language'
  1. Scala Programming Language
  2. SI-10034

Future.failed(th).failure returns incorrect value

    Details

      Description

      Regression:

      Future.failure(new Exception()).failed does not return a successful future containing the failure, in contrast to Future(new Exception()).failed, which does—the latter being the expected behavior.

      scala-core $ sbt console
      [info] Starting scala interpreter...
      Welcome to Scala 2.12.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_74).
      Type in expressions for evaluation. Or try :help.
       
      scala>  import scala.concurrent._
      import scala.concurrent._
       
      scala> import scala.concurrent.ExecutionContext.Implicits.global
      import scala.concurrent.ExecutionContext.Implicits.global
       
      scala>  val f = Future.failed(new Exception("error"))
      f: scala.concurrent.Future[Nothing] = Future(Failure(java.lang.Exception: error))
       
      scala> f.failed
      res0: scala.concurrent.Future[Throwable] = Future(Failure(java.lang.Exception: error))
       
      scala>  val f = Future(throw new Exception("error"))
      f: scala.concurrent.Future[Nothing] = Future(<not completed>)
       
      scala> f
      res1: scala.concurrent.Future[Nothing] = Future(Failure(java.lang.Exception: error))
       
      scala> f.failed
      res2: scala.concurrent.Future[Throwable] = Future(Success(java.lang.Exception: error))

      Note how res2 differs from res0, even though they both operate on what look like equivalent failed futures (f and res1)

      This is because the offending line (https://github.com/scala/scala/blob/2.12.x/src/library/scala/concurrent/impl/Promise.scala#L387)
      casts the future without switching the Failure to a Success.

        Attachments

          Activity

            People

            • Assignee:
              viktor.klang Viktor Klang
              Reporter:
              paploo Jeff Reinecke
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: