Skip to content

Commit 65da417

Browse files
committed
An alternative way to refer to * with no -Ycc set
1 parent 27040d6 commit 65da417

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

compiler/src/dotty/tools/dotc/cc/CaptureOps.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ private val BoxedType: Key[BoxedTypeCache] = Key()
1515

1616
/** The arguments of a @retains or @retainsByName annotation */
1717
private[cc] def retainedElems(tree: Tree)(using Context): List[Tree] = tree match
18-
case Apply(_, Typed(SeqLiteral(elems, _), _) :: Nil) => elems
19-
case _ => Nil
18+
case Apply(_, Typed(SeqLiteral(elems, _), _) :: Nil) =>
19+
elems.map(elem =>
20+
if elem.symbol == defn.Caps_universal then ref(defn.captureRoot) else elem)
21+
case _ =>
22+
Nil
2023

2124
/** An exception thrown if a @retains argument is not syntactically a CaptureRef */
2225
class IllegalCaptureRef(tpe: Type) extends Exception

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,7 @@ class Definitions {
961961
def RuntimeTupleFunctionsModule(using Context): Symbol = requiredModule("scala.runtime.TupledFunctions")
962962

963963
@tu lazy val CapsModule: Symbol = requiredModule("scala.caps")
964+
@tu lazy val Caps_universal: Symbol = CapsModule.requiredValue("universal")
964965
@tu lazy val Caps_unsafeUnbox: Symbol = CapsModule.requiredMethod("unsafeUnbox")
965966

966967
// Annotation base classes

library/src/scala/caps.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ object caps:
99
* avoids the error that would be raised when unboxing `*`.
1010
*/
1111
extension [T](x: T) def unsafeUnbox: T = x
12+
13+
/** An alternative for `*`, which can be used without -Ycc */
14+
val universal: Any = ()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import caps.*
2+
import annotation.retains
3+
4+
val foo: Int => Int = x => x
5+
val bar: (Int -> Int) @retains(universal) = foo
6+
val baz: {*} Int -> Int = bar
7+
8+

0 commit comments

Comments
 (0)