Skip to content

Commit 384eab4

Browse files
committed
Add more comments
1 parent ef7146e commit 384eab4

File tree

7 files changed

+22
-6
lines changed

7 files changed

+22
-6
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,15 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
537537
annot = New(annot, Nil)
538538
else
539539
val trefs =
540-
if refs.isEmpty then ref(defn.NothingType)
541-
else refs.map(SingletonTypeTree).reduce[Tree]((a, b) => makeOrType(a, b))
540+
if refs.isEmpty then
541+
// The NothingType is used to represent the empty capture set.
542+
ref(defn.NothingType)
543+
else
544+
// Treat all references as term references before typing.
545+
// A dummy term symbol will be created for each capture variable,
546+
// and references to them will be replaced with the corresponding
547+
// type references during typing.
548+
refs.map(SingletonTypeTree).reduce[Tree]((a, b) => makeOrType(a, b))
542549
annot = New(AppliedTypeTree(annot, trefs :: Nil), Nil)
543550
annot.putAttachment(RetainsAnnot, ())
544551
Annotated(parent, annot)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ extension (tree: Tree)
5858
tree.putAttachment(Captures, refs)
5959
refs
6060

61-
/** The type representing the capture set of retains annotation.
62-
*/
61+
/** The type representing the capture set of @retains, @retainsCap or @retainsByName annotation. */
6362
def retainedSet(using Context): Type =
6463
tree match
6564
case Apply(TypeApply(_, refs :: Nil), _) => refs.tpe
@@ -93,8 +92,7 @@ extension (tp: Type)
9392
if tp.isNothingType then Nil
9493
else tp :: Nil // should be checked by wellformedness
9594

96-
/** A list of capabilities tof a retained set.
97-
*/
95+
/** A list of capabilities of a retained set. */
9896
def retainedElements(using Context): List[Capability] =
9997
retainedElementsRaw.map(_.toCapability)
10098

@@ -530,6 +528,9 @@ class CleanupRetains(using Context) extends TypeMap:
530528
RetainingType(tp, defn.NothingType, byName = annot.symbol == defn.RetainsByNameAnnot)
531529
case _ => mapOver(tp)
532530

531+
/** A base class for extractors that match annotated types with a specific
532+
* Capability annotation.
533+
*/
533534
abstract class AnnotatedCapability(annotCls: Context ?=> ClassSymbol):
534535
def apply(tp: Type)(using Context): AnnotatedType =
535536
AnnotatedType(tp, Annotation(annotCls, util.Spans.NoSpan))

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ object NamerOps:
318318

319319
/** Add a dummy term symbol for a type def that has capture parameter flag.
320320
* The dummy symbol has the same name as the original type symbol and is stable.
321+
* The underlying info stores the corresponding type reference.
321322
*
322323
* @param param the original type symbol of the capture parameter
323324
*/

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,8 @@ object SymDenotations {
786786
*
787787
* However, a stable member might not yet be initialized (if it is an object or anyhow lazy).
788788
* So the first call to a stable member might fail and/or produce side effects.
789+
*
790+
* Note, (f: => T) is treated as a stable TermRef only in Capture Sets.
789791
*/
790792
final def isStableMember(using Context): Boolean = {
791793
def isUnstableValue =

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,7 @@ object Types extends TypeUtils {
770770
case tp: ClassInfo => tp.appliedRef
771771
case _ => widenIfUnstable
772772
}
773+
// The dummy term capture variable can only be found in a capture set.
773774
val excluded1 = if ctx.mode.is(Mode.InCaptureSet) then excluded else excluded | CaptureParam
774775
findMember(name, pre, required, excluded1)
775776
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ trait TypeAssigner {
162162
// if this fails.
163163
ctx.javaFindMember(name, pre, lookInCompanion = false)
164164
else
165+
// The dummy term capture variable can only be found in a capture set.
165166
val excluded = if ctx.mode.is(Mode.InCaptureSet) then EmptyFlags else CaptureParam
166167
qualType.findMember(name, pre, excluded = excluded)
167168

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
626626
val saved2 = foundUnderScala2
627627
unimported = Set.empty
628628
foundUnderScala2 = NoType
629+
// The dummy term capture variable can only be found in a capture set.
629630
val excluded = if ctx.mode.is(Mode.InCaptureSet) then EmptyFlags else CaptureParam
630631
try
631632
val found = findRef(name, pt, EmptyFlags, excluded, tree.srcPos)
@@ -2533,6 +2534,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
25332534
def typedSingletonTypeTree(tree: untpd.SingletonTypeTree)(using Context): Tree = {
25342535
val ref1 = typedExpr(tree.ref, SingletonTypeProto)
25352536
if ctx.mode.is(Mode.InCaptureSet) && ref1.symbol.is(Flags.CaptureParam) then
2537+
// When a dummy term capture variable is found, it is replaced with
2538+
// the corresponding type references (stored in the underling types).
25362539
return Ident(ref1.tpe.widen.asInstanceOf[TypeRef]).withSpan(tree.span)
25372540
checkStable(ref1.tpe, tree.srcPos, "singleton type")
25382541
assignType(cpy.SingletonTypeTree(tree)(ref1), ref1)

0 commit comments

Comments
 (0)