Skip to content

Commit ff629f2

Browse files
committed
Add more comments
1 parent 6893878 commit ff629f2

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
@@ -535,8 +535,15 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
535535
annot = New(annot, Nil)
536536
else
537537
val trefs =
538-
if refs.isEmpty then ref(defn.NothingType)
539-
else refs.map(SingletonTypeTree).reduce[Tree]((a, b) => makeOrType(a, b))
538+
if refs.isEmpty then
539+
// The NothingType is used to represent the empty capture set.
540+
ref(defn.NothingType)
541+
else
542+
// Treat all references as term references before typing.
543+
// A dummy term symbol will be created for each capture variable,
544+
// and references to them will be replaced with the corresponding
545+
// type references during typing.
546+
refs.map(SingletonTypeTree).reduce[Tree]((a, b) => makeOrType(a, b))
540547
annot = New(AppliedTypeTree(annot, trefs :: Nil), Nil)
541548
annot.putAttachment(RetainsAnnot, ())
542549
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
@@ -61,8 +61,7 @@ extension (tree: Tree)
6161
tree.putAttachment(Captures, refs)
6262
refs
6363

64-
/** The type representing the capture set of retains annotation.
65-
*/
64+
/** The type representing the capture set of @retains, @retainsCap or @retainsByName annotation. */
6665
def retainedSet(using Context): Type =
6766
tree match
6867
case Apply(TypeApply(_, refs :: Nil), _) => refs.tpe
@@ -96,8 +95,7 @@ extension (tp: Type)
9695
if tp.isNothingType then Nil
9796
else tp :: Nil // should be checked by wellformedness
9897

99-
/** A list of capabilities tof a retained set.
100-
*/
98+
/** A list of capabilities of a retained set. */
10199
def retainedElements(using Context): List[Capability] =
102100
retainedElementsRaw.map(_.toCapability)
103101

@@ -553,6 +551,9 @@ trait FollowAliasesMap(using Context) extends TypeMap:
553551
else t
554552
else mapOver(t)
555553

554+
/** A base class for extractors that match annotated types with a specific
555+
* Capability annotation.
556+
*/
556557
abstract class AnnotatedCapability(annotCls: Context ?=> ClassSymbol):
557558
def apply(tp: Type)(using Context): AnnotatedType =
558559
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
@@ -748,6 +748,7 @@ object Types extends TypeUtils {
748748
case tp: ClassInfo => tp.appliedRef
749749
case _ => widenIfUnstable
750750
}
751+
// The dummy term capture variable can only be found in a capture set.
751752
val excluded1 = if ctx.mode.is(Mode.InCaptureSet) then excluded else excluded | CaptureParam
752753
findMember(name, pre, required, excluded1)
753754
}

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)