Skip to content

Commit a6624de

Browse files
committed
Polishing and change printing options
1 parent c3b2f8a commit a6624de

File tree

11 files changed

+124
-117
lines changed

11 files changed

+124
-117
lines changed

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/cc/CaptureOps.scala

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Types.*, Symbols.*, Contexts.*, Annotations.*, Flags.*
77
import ast.{tpd, untpd}
88
import Decorators.*, NameOps.*
99
import config.SourceVersion
10-
import config.Printers.{capt, ccSetup}
10+
import config.Printers.capt
1111
import util.Property.Key
1212
import tpd.*
1313
import StdNames.nme
@@ -24,10 +24,13 @@ object ccConfig:
2424
*/
2525
private[cc] val adaptUnpickledFunctionTypes = false
2626

27+
/** If true, use `sealed` as encapsulation mechanism instead of the
28+
* previous global retriction that `cap` can't be boxed or unboxed.
29+
*/
30+
def allowUniversalInBoxed(using Context) =
31+
Feature.sourceVersion.isAtLeast(SourceVersion.`3.3`)
2732
end ccConfig
2833

29-
def allowUniversalInBoxed(using Context) =
30-
Feature.sourceVersion.isAtLeast(SourceVersion.`3.3`)
3134

3235
/** Are we at checkCaptures phase? */
3336
def isCaptureChecking(using Context): Boolean =
@@ -251,13 +254,12 @@ extension (tp: Type)
251254
extension (cls: ClassSymbol)
252255

253256
def pureBaseClass(using Context): Option[Symbol] =
254-
if cls.isClass then cls.asClass.baseClasses.find: bc =>
257+
cls.baseClasses.find: bc =>
255258
defn.pureBaseClasses.contains(bc)
256259
|| bc.givenSelfType.dealiasKeepAnnots.match
257260
case CapturingType(_, refs) => refs.isAlwaysEmpty
258261
case RetainingType(_, refs) => refs.isEmpty
259262
case selfType => selfType.exists && selfType.captureSet.isAlwaysEmpty
260-
else None
261263

262264
extension (sym: Symbol)
263265

@@ -303,19 +305,15 @@ extension (sym: Symbol)
303305
&& sym != defn.Caps_unsafeBox
304306
&& sym != defn.Caps_unsafeUnbox
305307

306-
def isTrackedSomewhere(using Context): Boolean =
307-
val search = new TypeAccumulator[Boolean]:
308-
def apply(found: Boolean, tp: Type) =
309-
def isTrackedHere = variance >= 0 && !tp.captureSet.isAlwaysEmpty
310-
found || isTrackedHere || foldOver(found, tp)
311-
search(false, sym.info)
312-
313-
// TODO Also include vals (right now they are manually entered in levelOwners by Setup)
308+
/** Can this symbol possibly own a local root?
309+
* TODO: Disallow anonymous functions?
310+
*/
314311
def isLevelOwner(using Context): Boolean =
315312
sym.isClass
316-
|| sym.is(Method, butNot = Accessor)// && !sym.isAnonymousFunction // TODO enable?
313+
|| sym.is(Method, butNot = Accessor)
317314

318-
/** The level owner enclosing `sym` which has the given name, or NoSymbol if none exists.
315+
/** The level owner enclosing `sym` which has the given name, or NoSymbol
316+
* if none exists.
319317
*/
320318
def levelOwnerNamed(name: String)(using Context): Symbol =
321319
def recur(sym: Symbol): Symbol =
@@ -347,11 +345,17 @@ extension (sym: Symbol)
347345
nme.LOCAL_CAPTURE_ROOT, Synthetic, defn.Caps_Cap.typeRef)
348346
ccState.localRoots.getOrElseUpdate(owner, newRoot)
349347

348+
/** The outermost symbol owned by both `sym` and `other`. if none exists
349+
* since the owning scopes of `sym` and `other` are not nested, invoke
350+
* `onConflict` to return a symbol.
351+
*/
350352
def maxNested(other: Symbol, onConflict: (Symbol, Symbol) => Context ?=> Symbol)(using Context): Symbol =
351353
if !sym.exists || other.isContainedIn(sym) then other
352354
else if !other.exists || sym.isContainedIn(other) then sym
353355
else onConflict(sym, other)
354356

357+
/** The innermost symbol owning both `sym` and `other`.
358+
*/
355359
def minNested(other: Symbol)(using Context): Symbol =
356360
if !other.exists || other.isContainedIn(sym) then sym
357361
else if !sym.exists || sym.isContainedIn(other) then other

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

Lines changed: 38 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ package cc
44

55
import core.*
66
import Types.*, Symbols.*, Flags.*, Contexts.*, Decorators.*
7-
import config.Printers.{capt, ccSetup}
7+
import config.Printers.capt
88
import Annotations.Annotation
99
import annotation.threadUnsafe
1010
import annotation.constructorOnly
1111
import annotation.internal.sharable
1212
import reporting.trace
1313
import printing.{Showable, Printer}
1414
import printing.Texts.*
15-
import util.{SimpleIdentitySet, Property, optional}, optional.{break, ?}
15+
import util.{SimpleIdentitySet, Property}
1616
import typer.ErrorReporting.Addenda
1717
import util.common.alwaysTrue
1818
import scala.collection.mutable
@@ -84,23 +84,13 @@ sealed abstract class CaptureSet extends Showable:
8484
final def containsRoot(using Context) =
8585
elems.exists(_.isRootCapability)
8686

87+
/** Does this capture set disallow an addiiton of `cap`, whereas it
88+
* might allow an addition of a local root?
89+
*/
8790
final def disallowsUniversal(using Context) =
8891
if isConst then !isUniversal && elems.exists(_.isLocalRootCapability)
8992
else asVar.noUniversal
9093

91-
/** Add new elements to this capture set if allowed.
92-
* @pre `newElems` is not empty and does not overlap with `this.elems`.
93-
* Constant capture sets never allow to add new elements.
94-
* Variables allow it if and only if the new elements can be included
95-
* in all their dependent sets.
96-
* @param origin The set where the elements come from, or `empty` if not known.
97-
* @return CompareResult.OK if elements were added, or a conflicting
98-
* capture set that prevents addition otherwise.
99-
*/
100-
protected final def tryInclude(newElems: Refs, origin: CaptureSet)(using Context, VarState): CompareResult =
101-
(CompareResult.OK /: newElems): (r, elem) =>
102-
r.andAlso(tryInclude(elem, origin))
103-
10494
/** Try to include an element in this capture set.
10595
* @param elem The element to be added
10696
* @param origin The set that originated the request, or `empty` if the request came from outside.
@@ -124,6 +114,11 @@ sealed abstract class CaptureSet extends Showable:
124114
if accountsFor(elem) then CompareResult.OK
125115
else addNewElem(elem)
126116

117+
/** Try to include all element in `refs` to this capture set. */
118+
protected final def tryInclude(newElems: Refs, origin: CaptureSet)(using Context, VarState): CompareResult =
119+
(CompareResult.OK /: newElems): (r, elem) =>
120+
r.andAlso(tryInclude(elem, origin))
121+
127122
/** Add an element to this capture set, assuming it is not already accounted for,
128123
* and omitting any mapping or filtering.
129124
*
@@ -132,12 +127,14 @@ sealed abstract class CaptureSet extends Showable:
132127
* capture set.
133128
*/
134129
protected final def addNewElem(elem: CaptureRef)(using Context, VarState): CompareResult =
135-
if elem.isRootCapability || summon[VarState] == FrozenState then addThisElem(elem)
136-
else addThisElem(elem).orElse:
137-
val underlying = elem.captureSetOfInfo
138-
tryInclude(underlying.elems, this).andAlso:
139-
underlying.addDependent(this)
140-
CompareResult.OK
130+
if elem.isRootCapability || summon[VarState] == FrozenState then
131+
addThisElem(elem)
132+
else
133+
addThisElem(elem).orElse:
134+
val underlying = elem.captureSetOfInfo
135+
tryInclude(underlying.elems, this).andAlso:
136+
underlying.addDependent(this)
137+
CompareResult.OK
141138

142139
/** Add new elements one by one using `addNewElem`, abort on first failure */
143140
protected final def addNewElems(newElems: Refs)(using Context, VarState): CompareResult =
@@ -146,7 +143,7 @@ sealed abstract class CaptureSet extends Showable:
146143

147144
/** Add a specific element, assuming it is not already accounted for,
148145
* and omitting any mapping or filtering, without possibility to backtrack
149-
* to underlying capture set
146+
* to the underlying capture set.
150147
*/
151148
protected def addThisElem(elem: CaptureRef)(using Context, VarState): CompareResult
152149

@@ -157,29 +154,14 @@ sealed abstract class CaptureSet extends Showable:
157154
protected def addAsDependentTo(cs: CaptureSet)(using Context): this.type =
158155
cs.addDependent(this)(using ctx, UnrecordedState)
159156
this
160-
/*
161-
/** Try to include all references of `elems` that are not yet accounted for by this
162-
* capture set. Inclusion is via `addNewElems`.
163-
* @param origin The set where the elements come from, or `empty` if not known.
164-
* @return CompareResult.OK if all unaccounted elements could be added,
165-
* capture set that prevents addition otherwise.
166-
*/
167-
protected final def tryInclude(elems: Refs, origin: CaptureSet)(using Context, VarState): CompareResult =
168-
val unaccounted = elems.filter(!accountsFor(_))
169-
if unaccounted.isEmpty then CompareResult.OK
170-
else tryInclude(unaccounted, origin)
171157

172-
/** Equivalent to `tryInclude({elem}, origin)`, but more efficient */
173-
protected final def tryInclude(elem: CaptureRef, origin: CaptureSet)(using Context, VarState): CompareResult =
174-
if accountsFor(elem) then CompareResult.OK
175-
else tryInclude(elem, origin)
176-
*/
177-
/* x subsumes y if one of the following is true:
178-
* - x is the same as y,
179-
* - x is a this reference and y refers to a field of x
180-
* - x and y are local roots and y is an enclosing root of x
181-
*/
182158
extension (x: CaptureRef)(using Context)
159+
160+
/* x subsumes y if one of the following is true:
161+
* - x is the same as y,
162+
* - x is a this reference and y refers to a field of x
163+
* - x is a super root of y
164+
*/
183165
private def subsumes(y: CaptureRef) =
184166
(x eq y)
185167
|| x.isSuperRootOf(y)
@@ -193,8 +175,8 @@ sealed abstract class CaptureSet extends Showable:
193175
*/
194176
private def isSuperRootOf(y: CaptureRef): Boolean = x match
195177
case x: TermRef =>
196-
if x.isUniversalRootCapability then true
197-
else if x.isLocalRootCapability && !y.isUniversalRootCapability then
178+
x.isUniversalRootCapability
179+
|| x.isLocalRootCapability && !y.isUniversalRootCapability && {
198180
val xowner = x.localRootOwner
199181
y match
200182
case y: TermRef =>
@@ -203,7 +185,7 @@ sealed abstract class CaptureSet extends Showable:
203185
xowner.isContainedIn(y.cls)
204186
case _ =>
205187
false
206-
else false
188+
}
207189
case _ => false
208190
end extension
209191

@@ -548,14 +530,14 @@ object CaptureSet:
548530
private def levelOK(elem: CaptureRef)(using Context): Boolean =
549531
if elem.isUniversalRootCapability then !noUniversal
550532
else !levelLimit.exists
551-
|| elem.match
552-
case elem: TermRef =>
553-
var sym = elem.symbol
554-
if sym.isLevelOwner then sym = sym.owner
555-
levelLimit.isContainedIn(sym.levelOwner)
556-
case elem: ThisType =>
557-
levelLimit.isContainedIn(elem.cls.levelOwner)
558-
case _ => true
533+
|| elem.match
534+
case elem: TermRef =>
535+
var sym = elem.symbol
536+
if sym.isLevelOwner then sym = sym.owner
537+
levelLimit.isContainedIn(sym.levelOwner)
538+
case elem: ThisType =>
539+
levelLimit.isContainedIn(elem.cls.levelOwner)
540+
case _ => true
559541

560542
def addDependent(cs: CaptureSet)(using Context, VarState): CompareResult =
561543
if (cs eq this) || cs.isUniversal || isConst then
@@ -1015,10 +997,8 @@ object CaptureSet:
1015997

1016998
/** The capture set of the type underlying CaptureRef */
1017999
def ofInfo(ref: CaptureRef)(using Context): CaptureSet = ref match
1018-
case ref: TermRef if ref.isRootCapability =>
1019-
ref.singletonCaptureSet
1020-
case _ =>
1021-
ofType(ref.underlying, followResult = true)
1000+
case ref: TermRef if ref.isRootCapability => ref.singletonCaptureSet
1001+
case _ => ofType(ref.underlying, followResult = true)
10221002

10231003
/** Capture set of a type */
10241004
def ofType(tp: Type, followResult: Boolean)(using Context): CaptureSet =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ object CapturingType:
2929
/** Smart constructor that
3030
* - drops empty capture sets
3131
* - drops a capability class expansion if it is further refined with another capturing type
32-
* - fuses compatible capturiong types.
32+
* - fuses compatible capturing types.
3333
* An outer type capturing type A can be fused with an inner capturing type B if their
3434
* boxing status is the same or if A is boxed.
3535
*/

0 commit comments

Comments
 (0)