Skip to content

Add @retainsArg to read-only and reach capabilities #23238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions library/src/scala/caps/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ sealed trait Exists extends Capability

@experimental
object internal:
import annotation.retainsArg

/** A wrapper indicating a type variable in a capture argument list of a
* @retains annotation. E.g. `^{x, Y^}` is represented as `@retains(x, capsOf[Y])`.
Expand All @@ -98,13 +99,13 @@ object internal:
* as `caps.reachCapability(x)`. When converted to CaptureRef types in capture sets
* they are represented as `x.type @annotation.internal.reachCapability`.
*/
extension (x: Any) def reachCapability: Any = x
extension (x: Any @retainsArg) def reachCapability: Any = x

/** Read-only capabilities x.rd which appear as terms in @retains annotations are encoded
* as `caps.readOnlyCapability(x)`. When converted to CaptureRef types in capture sets
* they are represented as `x.type @annotation.internal.readOnlyCapability`.
*/
extension (x: Any) def readOnlyCapability: Any = x
extension (x: Any @retainsArg) def readOnlyCapability: Any = x

/** An internal annotation placed on a refinement created by capture checking.
* Refinements with this annotation unconditionally override any
Expand Down
12 changes: 11 additions & 1 deletion tests/pos-custom-args/captures/i20231.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import caps.cap

class Async
class C(val x: Async ?=> Unit)
def foo(x: Async ?=> Unit): C^{x} = C(x)
def foo(x: Async ?=> Unit)(using Async): C^{x} = C(x)
def foo(x: Async ?=> Unit)(using Async): C^{x} = C(x)

// should also work for reach and read-only capabilities
class D(val x: Async ?=> Unit) extends caps.Mutable
def bar1(x: Async ?=> Unit): D^{cap, x*} = D(x)
def bar1(x: Async ?=> Unit)(using Async): D^{cap, x*} = D(x)
def bar2(x: Async ?=> Unit): D^{cap.rd, x.rd} = D(x)
def bar2(x: Async ?=> Unit)(using Async): D^{cap.rd, x.rd} = D(x)

Loading