Skip to content

Commit 85fa13d

Browse files
authored
Merge pull request scala#10615 from lrytz/t12918
Avoid completableFuture.asScala crash for MinimalStage
2 parents eeccf19 + 1423552 commit 85fa13d

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/library/scala/jdk/javaapi/FutureConverters.scala

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,19 @@ object FutureConverters {
7070
case cf: CF[T] => cf.wrapped
7171
// in theory not safe (could be `class C extends Future[A] with CompletionStage[B]`):
7272
case f: Future[T @unchecked] => f
73-
case cf: CompletableFuture[T @unchecked] if cf.isDone && !cf.isCompletedExceptionally =>
74-
val p = new P[T](cs)
75-
p.tryComplete(Success(cf.join()))
76-
p.future
7773
case _ =>
7874
val p = new P[T](cs)
79-
cs.handle(p)
75+
val completedCF = cs match {
76+
case cf0: CompletableFuture[T @unchecked] =>
77+
// drop `MinimalStage` (scala/bug#12918)
78+
val cf = cf0.toCompletableFuture
79+
if (cf.isDone && !cf.isCompletedExceptionally) cf else null
80+
case _ => null
81+
}
82+
if (completedCF != null)
83+
p.tryComplete(Success(completedCF.join()))
84+
else
85+
cs.handle(p)
8086
p.future
8187
}
8288
}

test/files/run/t12918.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// javaVersion: 9+
2+
3+
import java.util.concurrent._
4+
import scala.jdk.FutureConverters._
5+
6+
object Test extends App {
7+
val cf = CompletableFuture.completedStage("42")
8+
assert("42" == cf.asScala.value.get.get)
9+
}

0 commit comments

Comments
 (0)