|
| 1 | +package scala |
| 2 | +package caps |
| 3 | + |
| 4 | +import annotation.{experimental, compileTimeOnly, retainsCap} |
| 5 | + |
| 6 | +/** |
| 7 | + * Base trait for classes that represent capabilities in the |
| 8 | + * [object-capability model](https://en.wikipedia.org/wiki/Object-capability_model). |
| 9 | + * |
| 10 | + * A capability is a value representing a permission, access right, resource or effect. |
| 11 | + * Capabilities are typically passed to code as parameters; they should not be global objects. |
| 12 | + * Often, they come with access restrictions such as scoped lifetimes or limited sharing. |
| 13 | + * |
| 14 | + * An example is the [[scala.util.boundary.Label Label]] class in [[scala.util.boundary]]. |
| 15 | + * It represents a capability in the sense that it gives permission to [[scala.util.boundary.break break]] |
| 16 | + * to the enclosing boundary represented by the `Label`. It has a scoped lifetime, since breaking to |
| 17 | + * a `Label` after the associated `boundary` was exited gives a runtime exception. |
| 18 | + * |
| 19 | + * [[Capability]] has a formal meaning when |
| 20 | + * [[scala.language.experimental.captureChecking Capture Checking]] |
| 21 | + * is turned on. |
| 22 | + * But even without capture checking, extending this trait can be useful for documenting the intended purpose |
| 23 | + * of a class. |
| 24 | + */ |
| 25 | +@experimental |
| 26 | +trait Capability extends Any |
| 27 | + |
| 28 | +/** The universal capture reference. */ |
| 29 | +@experimental |
| 30 | +object cap extends Capability |
| 31 | + |
| 32 | +/** Marker trait for classes with methods that requires an exclusive reference. */ |
| 33 | +@experimental |
| 34 | +trait Mutable extends Capability |
| 35 | + |
| 36 | +/** Marker trait for capabilities that can be safely shared in a concurrent context. |
| 37 | + * During separation checking, shared capabilities are not taken into account. |
| 38 | + */ |
| 39 | +@experimental |
| 40 | +trait SharedCapability extends Capability |
| 41 | + |
| 42 | +/** Carrier trait for capture set type parameters */ |
| 43 | +@experimental |
| 44 | +trait CapSet extends Any |
| 45 | + |
| 46 | +/** A type constraint expressing that the capture set `C` needs to contain |
| 47 | + * the capability `R` |
| 48 | + */ |
| 49 | +@experimental |
| 50 | +sealed trait Contains[+C >: CapSet <: CapSet @retainsCap, R <: Singleton] |
| 51 | + |
| 52 | +@experimental |
| 53 | +object Contains: |
| 54 | + /** The only implementation of `Contains`. The constraint that `{R} <: C` is |
| 55 | + * added separately by the capture checker. |
| 56 | + */ |
| 57 | + @experimental |
| 58 | + given containsImpl[C >: CapSet <: CapSet @retainsCap, R <: Singleton]: Contains[C, R]() |
| 59 | + |
| 60 | +/** An annotation on parameters `x` stating that the method's body makes |
| 61 | + * use of the reach capability `x*`. Consequently, when calling the method |
| 62 | + * we need to charge the deep capture set of the actual argiment to the |
| 63 | + * environment. |
| 64 | + * |
| 65 | + * Note: This should go into annotations. For now it is here, so that we |
| 66 | + * can experiment with it quickly between minor releases |
| 67 | + */ |
| 68 | +@experimental |
| 69 | +final class use extends annotation.StaticAnnotation |
| 70 | + |
| 71 | +/** An annotations on parameters and update methods. |
| 72 | + * On a parameter it states that any capabilties passed in the argument |
| 73 | + * are no longer available afterwards, unless they are of class `SharableCapabilitty`. |
| 74 | + * On an update method, it states that the `this` of the enclosing class is |
| 75 | + * consumed, which means that any capabilities of the method prefix are |
| 76 | + * no longer available afterwards. |
| 77 | + */ |
| 78 | +@experimental |
| 79 | +final class consume extends annotation.StaticAnnotation |
| 80 | + |
| 81 | +/** A trait that used to allow expressing existential types. Replaced by |
| 82 | +* root.Result instances. |
| 83 | +*/ |
| 84 | +@experimental |
| 85 | +@deprecated |
| 86 | +sealed trait Exists extends Capability |
| 87 | + |
| 88 | +@experimental |
| 89 | +object internal: |
| 90 | + |
| 91 | + /** A wrapper indicating a type variable in a capture argument list of a |
| 92 | + * @retains annotation. E.g. `^{x, Y^}` is represented as `@retains(x, capsOf[Y])`. |
| 93 | + */ |
| 94 | + @compileTimeOnly("Should be be used only internally by the Scala compiler") |
| 95 | + def capsOf[CS >: CapSet <: CapSet @retainsCap]: Any = ??? |
| 96 | + |
| 97 | + /** Reach capabilities x* which appear as terms in @retains annotations are encoded |
| 98 | + * as `caps.reachCapability(x)`. When converted to CaptureRef types in capture sets |
| 99 | + * they are represented as `x.type @annotation.internal.reachCapability`. |
| 100 | + */ |
| 101 | + extension (x: Any) def reachCapability: Any = x |
| 102 | + |
| 103 | + /** Read-only capabilities x.rd which appear as terms in @retains annotations are encoded |
| 104 | + * as `caps.readOnlyCapability(x)`. When converted to CaptureRef types in capture sets |
| 105 | + * they are represented as `x.type @annotation.internal.readOnlyCapability`. |
| 106 | + */ |
| 107 | + extension (x: Any) def readOnlyCapability: Any = x |
| 108 | + |
| 109 | + /** An internal annotation placed on a refinement created by capture checking. |
| 110 | + * Refinements with this annotation unconditionally override any |
| 111 | + * info from the parent type, so no intersection needs to be formed. |
| 112 | + * This could be useful for tracked parameters as well. |
| 113 | + */ |
| 114 | + final class refineOverride extends annotation.StaticAnnotation |
| 115 | + |
| 116 | + /** An annotation used internally for root capability wrappers of `cap` that |
| 117 | + * represent either Fresh or Result capabilities. |
| 118 | + * A capability is encoded as `caps.cap @rootCapability(...)` where |
| 119 | + * `rootCapability(...)` is a special kind of annotation of type `root.Annot` |
| 120 | + * that contains either a hidden set for Fresh instances or a method type binder |
| 121 | + * for Result instances. |
| 122 | + */ |
| 123 | + final class rootCapability extends annotation.StaticAnnotation |
| 124 | + |
| 125 | +@experimental |
| 126 | +object unsafe: |
| 127 | + /** |
| 128 | + * Marks the constructor parameter as untracked. |
| 129 | + * The capture set of this parameter will not be included in |
| 130 | + * the capture set of the constructed object. |
| 131 | + * |
| 132 | + * @note This should go into annotations. For now it is here, so that we |
| 133 | + * can experiment with it quickly between minor releases |
| 134 | + */ |
| 135 | + final class untrackedCaptures extends annotation.StaticAnnotation |
| 136 | + |
| 137 | + extension [T](x: T) |
| 138 | + /** A specific cast operation to remove a capture set. |
| 139 | + * If argument is of type `T^C`, assume it is of type `T` instead. |
| 140 | + * Calls to this method are treated specially by the capture checker. |
| 141 | + */ |
| 142 | + def unsafeAssumePure: T = x |
| 143 | + |
| 144 | + /** A wrapper around code for which separation checks are suppressed. |
| 145 | + */ |
| 146 | + def unsafeAssumeSeparate(op: Any): op.type = op |
| 147 | + |
| 148 | +end unsafe |
0 commit comments