Skip to content

Commit 060676e

Browse files
authored
Special handling of experimental.captureChecking import (#17427)
The question is, how do we introduce capture checking safely. There are two conflicting requirements: - Capture checking should not leak into standard Scala. It should be meaningful only in code that has capture checking explicitly enabled. - We need to be able to build up libraries that express capture information and that can be consumed from other code. This needs to start with the standard library itself. So far, everything related to capture checking was marked experimental. This means all code that refers to a capture checking abstraction in any way whatsoever needs to be declared experimental. That clearly does not work for the new use cases. But fortunately, capture checking has some properties that enable a different scheme. Specifically, a file compiled under capture checking looks like a completely normal component (both Tasty and binary) to any other file that uses it and that is not compiled with captureChecking. Only when the consumer is also compiled with capture checking, the capturing types of the original file will be revealed. The same holds for binaries. Capture checking has no effect at all on the binaries that get generated and all types and annotations needed for capture checking are erased. This allows the following more flexible scheme: - We can turn capture checking on with a setting or language import in any source file. The sources do not have to be @experimental. - If capture checking is turned on, a number of annotations and other symbols that are needed for capture checking and are normally experimental are also made available. The important property is that capture checking in one component cannot poison other normal components. Like @experimental itself, the whole scheme is transitive. With the new scheme we do not need a special exemption for the dotty package anymore, so the code implementing the exception is dropped.
2 parents 4e4552e + e6d1242 commit 060676e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+117
-85
lines changed

compiler/src/dotty/tools/dotc/config/Feature.scala

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,21 @@ object Feature:
136136
if !isExperimentalEnabled then
137137
report.error(em"Experimental $which may only be used with a nightly or snapshot version of the compiler$note", srcPos)
138138

139+
private def ccException(sym: Symbol)(using Context): Boolean =
140+
ccEnabled && defn.ccExperimental.contains(sym)
141+
139142
def checkExperimentalDef(sym: Symbol, srcPos: SrcPos)(using Context) =
140143
if !isExperimentalEnabled then
141-
val symMsg =
142-
if sym.hasAnnotation(defn.ExperimentalAnnot) then
143-
i"$sym is marked @experimental"
144-
else if sym.owner.hasAnnotation(defn.ExperimentalAnnot) then
145-
i"${sym.owner} is marked @experimental"
146-
else
147-
i"$sym inherits @experimental"
148-
report.error(em"$symMsg and therefore may only be used in an experimental scope.", srcPos)
144+
val experimentalSym =
145+
if sym.hasAnnotation(defn.ExperimentalAnnot) then sym
146+
else if sym.owner.hasAnnotation(defn.ExperimentalAnnot) then sym.owner
147+
else NoSymbol
148+
if !ccException(experimentalSym) then
149+
val symMsg =
150+
if experimentalSym.exists
151+
then i"$experimentalSym is marked @experimental"
152+
else i"$sym inherits @experimental"
153+
report.error(em"$symMsg and therefore may only be used in an experimental scope.", srcPos)
149154

150155
/** Check that experimental compiler options are only set for snapshot or nightly compiler versions. */
151156
def checkExperimentalSettings(using Context): Unit =

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,8 @@ class Definitions {
976976
@tu lazy val Caps_unsafeBoxFunArg: Symbol = CapsUnsafeModule.requiredMethod("unsafeBoxFunArg")
977977
@tu lazy val Caps_SealedAnnot: ClassSymbol = requiredClass("scala.caps.Sealed")
978978

979+
@tu lazy val PureClass: Symbol = requiredClass("scala.Pure")
980+
979981
// Annotation base classes
980982
@tu lazy val AnnotationClass: ClassSymbol = requiredClass("scala.annotation.Annotation")
981983
@tu lazy val StaticAnnotationClass: ClassSymbol = requiredClass("scala.annotation.StaticAnnotation")
@@ -1944,6 +1946,14 @@ class Definitions {
19441946
case Some(pkgs) => pkgs.contains(sym.owner)
19451947
case none => false
19461948

1949+
/** Experimental definitions that can nevertheless be accessed from a stable
1950+
* compiler if capture checking is enabled.
1951+
*/
1952+
@tu lazy val ccExperimental: Set[Symbol] = Set(
1953+
CapsModule, CapsModule.moduleClass, PureClass,
1954+
CapabilityAnnot, RequiresCapabilityAnnot,
1955+
RetainsAnnot, RetainsByNameAnnot, WithPureFunsAnnot)
1956+
19471957
// ----- primitive value class machinery ------------------------------------------
19481958

19491959
class PerRun[T](generate: Context ?=> T) {
@@ -2041,15 +2051,17 @@ class Definitions {
20412051
def isValueSubClass(sym1: Symbol, sym2: Symbol): Boolean =
20422052
valueTypeEnc(sym2.asClass.name) % valueTypeEnc(sym1.asClass.name) == 0
20432053

2044-
@tu lazy val specialErasure: SimpleIdentityMap[Symbol, ClassSymbol] =
2045-
SimpleIdentityMap.empty[Symbol]
2046-
.updated(AnyClass, ObjectClass)
2047-
.updated(MatchableClass, ObjectClass)
2048-
.updated(AnyValClass, ObjectClass)
2049-
.updated(SingletonClass, ObjectClass)
2050-
.updated(TupleClass, ProductClass)
2051-
.updated(NonEmptyTupleClass, ProductClass)
2052-
.updated(PairClass, ObjectClass)
2054+
@tu lazy val specialErasure: collection.Map[Symbol, ClassSymbol] =
2055+
val m = mutable.Map[Symbol, ClassSymbol]()
2056+
m(AnyClass) = ObjectClass
2057+
m(MatchableClass) = ObjectClass
2058+
m(PureClass) = ObjectClass
2059+
m(AnyValClass) = ObjectClass
2060+
m(SingletonClass) = ObjectClass
2061+
m(TupleClass) = ProductClass
2062+
m(NonEmptyTupleClass) = ProductClass
2063+
m(PairClass) = ObjectClass
2064+
m
20532065

20542066
// ----- Initialization ---------------------------------------------------
20552067

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ object TypeErasure {
410410
val candidates = takeUntil(tp2superclasses)(!_.is(Trait))
411411

412412
// Candidates st "no other common superclass or trait derives from S"
413-
// Also, drop `PairClass` since it is not valid after erasue
413+
// Also, drop `PairClass` since it is not valid after erasure
414414
val minimums = candidates.filter { cand =>
415415
cand != defn.PairClass
416416
&& candidates.forall(x => !x.derivesFrom(cand) || x.eq(cand))

compiler/src/dotty/tools/dotc/transform/Erasure.scala

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,14 @@ class Erasure extends Phase with DenotTransformer {
190190
def assertErased(tp: Type, tree: tpd.Tree = tpd.EmptyTree)(using Context): Unit = {
191191
def isAllowed(cls: Symbol, sourceName: String) =
192192
tp.typeSymbol == cls && ctx.compilationUnit.source.file.name == sourceName
193-
assert(isErasedType(tp) ||
194-
isAllowed(defn.ArrayClass, "Array.scala") ||
195-
isAllowed(defn.TupleClass, "Tuple.scala") ||
196-
isAllowed(defn.NonEmptyTupleClass, "Tuple.scala") ||
197-
isAllowed(defn.PairClass, "Tuple.scala"),
198-
i"The type $tp - ${tp.toString} of class ${tp.getClass} of tree $tree : ${tree.tpe} / ${tree.getClass} is illegal after erasure, phase = ${ctx.phase.prev}")
193+
assert(
194+
isErasedType(tp)
195+
|| isAllowed(defn.ArrayClass, "Array.scala")
196+
|| isAllowed(defn.TupleClass, "Tuple.scala")
197+
|| isAllowed(defn.NonEmptyTupleClass, "Tuple.scala")
198+
|| isAllowed(defn.PairClass, "Tuple.scala")
199+
|| isAllowed(defn.PureClass, "Pure.scala"),
200+
i"The type $tp - ${tp.toString} of class ${tp.getClass} of tree $tree : ${tree.tpe} / ${tree.getClass} is illegal after erasure, phase = ${ctx.phase.prev}")
199201
}
200202
}
201203

compiler/src/dotty/tools/dotc/transform/SymUtils.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,6 @@ object SymUtils:
370370
self.hasAnnotation(defn.ExperimentalAnnot)
371371
|| isDefaultArgumentOfExperimentalMethod
372372
|| (!self.is(Package) && self.owner.isInExperimentalScope)
373-
|| self.topLevelClass.ownersIterator.exists(p =>
374-
p.is(Package) && p.owner.isRoot && p.name == tpnme.dotty)
375373

376374
/** The declared self type of this class, as seen from `site`, stripping
377375
* all refinements for opaque types.

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,9 @@ object Checking {
784784
for case imp @ Import(qual, selectors) <- trees do
785785
def isAllowedImport(sel: untpd.ImportSelector) =
786786
val name = Feature.experimental(sel.name)
787-
name == Feature.scala2macros || name == Feature.erasedDefinitions
787+
name == Feature.scala2macros
788+
|| name == Feature.erasedDefinitions
789+
|| name == Feature.captureChecking
788790

789791
languageImport(qual) match
790792
case Some(nme.experimental)

library/src/scala/Pure.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package scala
2+
import annotation.experimental
3+
4+
/** A marker trait that declares that all inheriting classes are "pure" in the
5+
* sense that their values retain no capabilities including capabilities needed
6+
* to perform effects. This has formal meaning only under capture checking.
7+
*/
8+
@experimental trait Pure:
9+
this: Pure =>

library/src/scala/caps.scala

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,3 @@ import annotation.experimental
4040
*/
4141
@deprecated("The Sealed annotation should not be directly used in source code.\nUse the `sealed` modifier on type parameters instead.")
4242
class Sealed extends annotation.Annotation
43-
44-
/** Mixing in this trait forces a trait or class to be pure, i.e.
45-
* have no capabilities retained in its self type.
46-
*/
47-
trait Pure:
48-
this: Pure =>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@annotation.experimental class C(x: () => Unit) extends caps.Pure // error
1+
@annotation.experimental class C(x: () => Unit) extends Pure // error
22

3-
@annotation.experimental class D(@annotation.constructorOnly x: () => Unit) extends caps.Pure // ok
3+
@annotation.experimental class D(@annotation.constructorOnly x: () => Unit) extends Pure // ok
44

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package scala.runtime
2+
3+
import language.experimental.captureChecking
4+
5+
object test:
6+
type T = Pure
7+
8+
class Foo extends Object, Pure:
9+
val x: Pure = ???
10+
def foo() = ()
11+

0 commit comments

Comments
 (0)