Skip to content

Commit a940541

Browse files
authored
A more flexible scheme for handling the universal capability (#18699)
- Start with `sealed` type parameters, extended so that class parameters can also be `sealed`. - Tighten the rules so that type parameter arguments for `sealed` type parameters must themselves be `sealed`, except if the argument is from some outer scope. - Introduce a coercion that maps a universal capture set to a local root that is enclosed by the owners of all free variables of the coerced expression. Supersedes #18566
2 parents c1c2c8e + 181d96e commit a940541

File tree

116 files changed

+2524
-2008
lines changed

Some content is hidden

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

116 files changed

+2524
-2008
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package dotc
44
import core._
55
import Contexts._
66
import typer.{TyperPhase, RefChecks}
7-
import cc.CheckCaptures
87
import parsing.Parser
98
import Phases.Phase
109
import transform._
@@ -85,8 +84,8 @@ class Compiler {
8584
new PatternMatcher) :: // Compile pattern matches
8685
List(new TestRecheck.Pre) :: // Test only: run rechecker, enabled under -Yrecheck-test
8786
List(new TestRecheck) :: // Test only: run rechecker, enabled under -Yrecheck-test
88-
List(new CheckCaptures.Pre) :: // Preparations for check captures phase, enabled under captureChecking
89-
List(new CheckCaptures) :: // Check captures, enabled under captureChecking
87+
List(new cc.Setup) :: // Preparations for check captures phase, enabled under captureChecking
88+
List(new cc.CheckCaptures) :: // Check captures, enabled under captureChecking
9089
List(new ElimOpaque, // Turn opaque into normal aliases
9190
new sjs.ExplicitJSClasses, // Make all JS classes explicit (Scala.js only)
9291
new ExplicitOuter, // Add accessors to outer classes from nested ones.

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ object desugar {
437437
private def toDefParam(tparam: TypeDef, keepAnnotations: Boolean): TypeDef = {
438438
var mods = tparam.rawMods
439439
if (!keepAnnotations) mods = mods.withAnnotations(Nil)
440-
tparam.withMods(mods & EmptyFlags | Param)
440+
tparam.withMods(mods & (EmptyFlags | Sealed) | Param)
441441
}
442442
private def toDefParam(vparam: ValDef, keepAnnotations: Boolean, keepDefault: Boolean): ValDef = {
443443
var mods = vparam.rawMods

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
810810
}
811811
}
812812

813-
/** An extractor for def of a closure contained the block of the closure,
813+
/** An extractor for the method of a closure contained the block of the closure,
814814
* possibly with type ascriptions.
815815
*/
816816
object possiblyTypedClosureDef:

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,21 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
12861286
!(sym.is(Method) && sym.info.isInstanceOf[MethodOrPoly]) // if is a method it is parameterless
12871287
}
12881288

1289+
/** A tree traverser that generates the the same import contexts as original typer for statements.
1290+
* TODO: Should we align TreeMapWithPreciseStatContexts and also keep track of exprOwners?
1291+
*/
1292+
abstract class TreeTraverserWithPreciseImportContexts extends TreeTraverser:
1293+
override def apply(x: Unit, trees: List[Tree])(using Context): Unit =
1294+
def recur(trees: List[Tree]): Unit = trees match
1295+
case (imp: Import) :: rest =>
1296+
traverse(rest)(using ctx.importContext(imp, imp.symbol))
1297+
case tree :: rest =>
1298+
traverse(tree)
1299+
traverse(rest)
1300+
case Nil =>
1301+
recur(trees)
1302+
end TreeTraverserWithPreciseImportContexts
1303+
12891304
extension (xs: List[tpd.Tree])
12901305
def tpes: List[Type] = xs match {
12911306
case x :: xs1 => x.tpe :: xs1.tpes

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

Lines changed: 0 additions & 19 deletions
This file was deleted.

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,17 @@ case class CaptureAnnotation(refs: CaptureSet, boxed: Boolean)(cls: Symbol) exte
2424
import CaptureAnnotation.*
2525
import tpd.*
2626

27-
/** A cache for boxed version of a capturing type with this annotation */
28-
val boxedType = BoxedTypeCache()
27+
/** A cache for the version of this annotation which differs in its boxed status. */
28+
var boxDual: CaptureAnnotation | Null = null
29+
30+
/** A boxed annotation which is either the same annotation or its boxDual */
31+
def boxedAnnot(using Context): CaptureAnnotation =
32+
if boxed then this
33+
else if boxDual != null then boxDual.nn
34+
else
35+
val dual = CaptureAnnotation(refs, boxed = true)(cls)
36+
dual.boxDual = this
37+
dual
2938

3039
/** Reconstitute annotation tree from capture set */
3140
override def tree(using Context) =

0 commit comments

Comments
 (0)