1212
1313package scala .concurrent .impl
1414
15- import scala .concurrent .{Batchable , Batchable2 , CanAwait , ExecutionContext , ExecutionException , Future , TimeoutException }
15+ import scala .concurrent .{Batchable , CanAwait , ExecutionContext , ExecutionException , Future , TimeoutException }
1616import scala .concurrent .duration .Duration
1717import scala .annotation .{nowarn , switch , tailrec }
1818import scala .util .control .{ControlThrowable , NonFatal }
@@ -24,6 +24,7 @@ import java.util.Objects.requireNonNull
2424import java .io .{IOException , NotSerializableException , ObjectInputStream , ObjectOutputStream }
2525
2626import language .experimental .captureChecking
27+ import caps .unsafe .*
2728
2829/**
2930 * Latch used to implement waiting on a DefaultPromise's result.
@@ -61,19 +62,19 @@ private[concurrent] object Promise {
6162 * If when compressing a chain of Links it is discovered that the root has been completed,
6263 * the `owner`'s value is completed with that value, and the Link chain is discarded.
6364 **/
64- private [concurrent] final class Link [T ](to : DefaultPromise [T ]) extends AtomicReference [DefaultPromise [T ]](to) {
65+ private [concurrent] final class Link [T ](to : DefaultPromise [T ]^ ) extends AtomicReference [DefaultPromise [T ]^ {to} ](to) {
6566 /**
6667 * Compresses this chain and returns the currently known root of this chain of Links.
6768 **/
68- final def promise (owner : DefaultPromise [T ]): DefaultPromise [T ] = {
69+ final def promise (owner : DefaultPromise [T ]^ ): DefaultPromise [T ]^ { this , to, owner} = {
6970 val c = get()
7071 compressed(current = c, target = c, owner = owner)
7172 }
7273
7374 /**
7475 * The combination of traversing and possibly unlinking of a given `target` DefaultPromise.
7576 **/
76- @ inline @ tailrec private [this ] final def compressed (current : DefaultPromise [T ], target : DefaultPromise [T ], owner : DefaultPromise [T ]): DefaultPromise [T ] = {
77+ @ inline @ tailrec private [this ] final def compressed (current : DefaultPromise [T ]^ , target : DefaultPromise [T ]^ , owner : DefaultPromise [T ]^ ): DefaultPromise [T ]^ { this , current, target, owner} = {
7778 val value = target.get()
7879 if (value.isInstanceOf [Callbacks [_]]) {
7980 if (compareAndSet(current, target)) target // Link
@@ -105,6 +106,7 @@ private[concurrent] object Promise {
105106
106107 // Left non-final to enable addition of extra fields by Java/Scala converters in scala-java8-compat.
107108 class DefaultPromise [T ] private [this ] (initial : AnyRef ) extends AtomicReference [AnyRef ](initial) with scala.concurrent.Promise [T ] with scala.concurrent.Future [T ] with (Try [T ] => Unit ) {
109+ self : DefaultPromise [T ]^ =>
108110 /**
109111 * Constructs a new, completed, Promise.
110112 */
@@ -125,7 +127,7 @@ private[concurrent] object Promise {
125127 /**
126128 * Returns the associated `Future` with this `Promise`
127129 */
128- override final def future : Future [T ] = this
130+ override final def future : Future [T ]^ = ( this : Future [ T ] ^ )
129131
130132 override final def transform [S ](f : Try [T ] => Try [S ])(implicit executor : ExecutionContext ): Future [S ] =
131133 dispatchOrAddCallbacks(get(), new Transformation [T , S ](Xform_transform , f, executor))
@@ -186,7 +188,7 @@ private[concurrent] object Promise {
186188 else this .asInstanceOf [Future [S ]]
187189 }
188190
189- override final def filter (p : T => Boolean )(implicit executor : ExecutionContext ): Future [T ] = {
191+ override final def filter (p : T => Boolean )(implicit executor : ExecutionContext ): Future [T ]^ {p, executor, this } = {
190192 val state = get()
191193 if (! state.isInstanceOf [Failure [_]]) dispatchOrAddCallbacks(state, new Transformation [T , T ](Xform_filter , p, executor)) // Short-circuit if we get a Success
192194 else this
@@ -343,7 +345,7 @@ private[concurrent] object Promise {
343345
344346 /** Link this promise to the root of another promise.
345347 */
346- @ tailrec private [concurrent] final def linkRootOf (target : DefaultPromise [T ], link : Link [T ]): Unit =
348+ @ tailrec private [concurrent] final def linkRootOf (target : DefaultPromise [T ]^ , link : Link [T ]^ ): Unit =
347349 if (this ne target) {
348350 val state = get()
349351 if (state.isInstanceOf [Try [_]]) {
@@ -412,13 +414,13 @@ private[concurrent] object Promise {
412414 * function's type parameters are erased, and the _xform tag will be used to reify them.
413415 **/
414416 final class Transformation [- F , T ] private [this ] (
415- private [this ] final var _fun : Any - > Any ,
416- private [this ] final var _ec : ExecutionContext ,
417+ private [this ] final val _fun : Any = > Any ,
418+ private [this ] final val _ec : ExecutionContext ,
417419 private [this ] final var _arg : Try [F ],
418420 private [this ] final val _xform : Int
419421 ) extends DefaultPromise [T ]() with Callbacks [F ] with Runnable with Batchable {
420422 final def this (xform : Int , f : _ => _, ec : ExecutionContext ) =
421- this (f.asInstanceOf [Any - > Any ], ec.prepare(): @ nowarn(" cat=deprecation" ), null , xform)
423+ this (f.asInstanceOf [Any = > Any ], ec.prepare(): @ nowarn(" cat=deprecation" ), null , xform)
422424
423425 final def benefitsFromBatching : Boolean = _xform != Xform_onComplete && _xform != Xform_foreach
424426
@@ -429,12 +431,12 @@ private[concurrent] object Promise {
429431 final def submitWithValue (resolved : Try [F ]): this .type = {
430432 _arg = resolved
431433 val e = _ec
432- try e.execute(this ) /* Safe publication of _arg, _fun, _ec */
434+ try e.execute(this .unsafeAssumePure ) /* Safe publication of _arg, _fun, _ec */
433435 catch {
434436 case t : Throwable =>
435437 // _fun = null // allow to GC
436438 _arg = null // see above
437- _ec = null // see above again
439+ // _ec = null // see above again
438440 handleFailure(t, e)
439441 }
440442
@@ -460,7 +462,7 @@ private[concurrent] object Promise {
460462 val ec = _ec
461463 // _fun = null // allow to GC
462464 _arg = null // see above
463- _ec = null // see above
465+ // _ec = null // see above
464466 try {
465467 val resolvedResult : Try [_] =
466468 (_xform : @ switch) match {
0 commit comments