Skip to content

Commit 4195533

Browse files
committed
Enhancement: Move unsafe box/unbox ops into separate caps.unsafe module
1 parent 7de783c commit 4195533

File tree

7 files changed

+24
-19
lines changed

7 files changed

+24
-19
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -961,10 +961,11 @@ 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_unsafeBox: Symbol = CapsModule.requiredMethod("unsafeBox")
965-
@tu lazy val Caps_unsafeUnbox: Symbol = CapsModule.requiredMethod("unsafeUnbox")
966-
@tu lazy val Caps_unsafeBoxFunArg: Symbol = CapsModule.requiredMethod("unsafeBoxFunArg")
967964
@tu lazy val captureRoot: TermSymbol = CapsModule.requiredValue("*")
965+
@tu lazy val CapsUnsafeModule: Symbol = requiredModule("scala.caps.unsafe")
966+
@tu lazy val Caps_unsafeBox: Symbol = CapsUnsafeModule.requiredMethod("unsafeBox")
967+
@tu lazy val Caps_unsafeUnbox: Symbol = CapsUnsafeModule.requiredMethod("unsafeUnbox")
968+
@tu lazy val Caps_unsafeBoxFunArg: Symbol = CapsUnsafeModule.requiredMethod("unsafeBoxFunArg")
968969

969970
// Annotation base classes
970971
@tu lazy val AnnotationClass: ClassSymbol = requiredClass("scala.annotation.Annotation")

library/src/scala/caps.scala

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@ import annotation.experimental
77
/** The universal capture reference */
88
val `*`: Any = ()
99

10-
/** If argument is of type `cs T`, converts to type `box cs T`. This
11-
* avoids the error that would be raised when boxing `*`.
12-
*/
13-
extension [T](x: T) def unsafeBox: T = x
10+
object unsafe:
1411

15-
/** If argument is of type `box cs T`, converts to type `cs T`. This
16-
* avoids the error that would be raised when unboxing `*`.
17-
*/
18-
extension [T](x: T) def unsafeUnbox: T = x
12+
/** If argument is of type `cs T`, converts to type `box cs T`. This
13+
* avoids the error that would be raised when boxing `*`.
14+
*/
15+
extension [T](x: T) def unsafeBox: T = x
1916

20-
/** If argument is of type `box cs T`, converts to type `cs T`. This
21-
* avoids the error that would be raised when unboxing `*`.
22-
*/
23-
extension [T, U](f: T => U) def unsafeBoxFunArg: T => U = f
17+
/** If argument is of type `box cs T`, converts to type `cs T`. This
18+
* avoids the error that would be raised when unboxing `*`.
19+
*/
20+
extension [T](x: T) def unsafeUnbox: T = x
21+
22+
/** If argument is of type `box cs T`, converts to type `cs T`. This
23+
* avoids the error that would be raised when unboxing `*`.
24+
*/
25+
extension [T, U](f: T => U) def unsafeBoxFunArg: T => U = f
26+
end unsafe
2427

2528
/** Mixing in this trait forces a trait or class to be pure, i.e.
2629
* have no capabilities retained in its self type.

project/MiMaFilters.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ object MiMaFilters {
2020
ProblemFilters.exclude[MissingClassProblem]("scala.runtime.stdLibPatches.language$experimental$captureChecking$"),
2121
ProblemFilters.exclude[MissingClassProblem]("scala.caps"),
2222
ProblemFilters.exclude[MissingClassProblem]("scala.caps$Pure"),
23+
ProblemFilters.exclude[MissingClassProblem]("scala.caps$unsafe$"),
2324
)
2425
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import caps.*
1+
import caps.unsafe.*
22
def test =
33
val tasks = new collection.mutable.ArrayBuffer[() => Unit]
44
val _: Unit = tasks.foreach(((task: () => Unit) => task()).unsafeBoxFunArg)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import caps.*
1+
import caps.unsafe.*
22
def test =
33
var finalizeActions = collection.mutable.ListBuffer[() => Unit]()
44
val action = finalizeActions.remove(0).unsafeUnbox

tests/pos-custom-args/captures/vars1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import caps.*
1+
import caps.unsafe.*
22

33
object Test:
44
type ErrorHandler = (Int, String) => Unit
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dotty.tools
22
object test {
33

4-
val x = caps.unsafeBox
4+
val x = caps.unsafe.unsafeBox
55

66
}

0 commit comments

Comments
 (0)