Skip to content

Commit

Permalink
Merge pull request #10615 from lrytz/t12918
Browse files Browse the repository at this point in the history
Avoid completableFuture.asScala crash for MinimalStage
  • Loading branch information
lrytz committed Dec 20, 2023
2 parents eeccf19 + 1423552 commit 85fa13d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/library/scala/jdk/javaapi/FutureConverters.scala
Expand Up @@ -70,13 +70,19 @@ object FutureConverters {
case cf: CF[T] => cf.wrapped
// in theory not safe (could be `class C extends Future[A] with CompletionStage[B]`):
case f: Future[T @unchecked] => f
case cf: CompletableFuture[T @unchecked] if cf.isDone && !cf.isCompletedExceptionally =>
val p = new P[T](cs)
p.tryComplete(Success(cf.join()))
p.future
case _ =>
val p = new P[T](cs)
cs.handle(p)
val completedCF = cs match {
case cf0: CompletableFuture[T @unchecked] =>
// drop `MinimalStage` (scala/bug#12918)
val cf = cf0.toCompletableFuture
if (cf.isDone && !cf.isCompletedExceptionally) cf else null
case _ => null
}
if (completedCF != null)
p.tryComplete(Success(completedCF.join()))
else
cs.handle(p)
p.future
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/files/run/t12918.scala
@@ -0,0 +1,9 @@
// javaVersion: 9+

import java.util.concurrent._
import scala.jdk.FutureConverters._

object Test extends App {
val cf = CompletableFuture.completedStage("42")
assert("42" == cf.asScala.value.get.get)
}

0 comments on commit 85fa13d

Please sign in to comment.