Skip to content

Commit ef7146e

Browse files
committed
Cleanup code
1 parent ffa04cb commit ef7146e

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extension (tree: Tree)
5959
refs
6060

6161
/** The type representing the capture set of retains annotation.
62-
*/
62+
*/
6363
def retainedSet(using Context): Type =
6464
tree match
6565
case Apply(TypeApply(_, refs :: Nil), _) => refs.tpe
@@ -69,19 +69,22 @@ extension (tree: Tree)
6969

7070
extension (tp: Type)
7171

72-
def toCapabilities(using Context): List[Capability] = tp match
72+
def toCapability(using Context): Capability = tp match
7373
case ReachCapability(tp1) =>
74-
tp1.toCapabilities.map(_.reach)
74+
tp1.toCapability.reach
7575
case ReadOnlyCapability(tp1) =>
76-
tp1.toCapabilities.map(_.readOnly)
76+
tp1.toCapability.readOnly
7777
case ref: TermRef if ref.isCapRef =>
78-
GlobalCap :: Nil
78+
GlobalCap
7979
case ref: Capability if ref.isTrackableRef =>
80-
ref :: Nil
80+
ref
8181
case _ =>
8282
// if this was compiled from cc syntax, problem should have been reported at Typer
8383
throw IllegalCaptureRef(tp)
8484

85+
/** A list of raw elements of a retained set.
86+
* This will not crash even if it contains a non-wellformed Capability.
87+
*/
8588
def retainedElementsRaw(using Context): List[Type] = tp match
8689
case OrType(tp1, tp2) =>
8790
tp1.retainedElementsRaw ++ tp2.retainedElementsRaw
@@ -90,8 +93,10 @@ extension (tp: Type)
9093
if tp.isNothingType then Nil
9194
else tp :: Nil // should be checked by wellformedness
9295

96+
/** A list of capabilities tof a retained set.
97+
*/
9398
def retainedElements(using Context): List[Capability] =
94-
retainedElementsRaw.flatMap(_.toCapabilities)
99+
retainedElementsRaw.map(_.toCapability)
95100

96101
/** Is this type a Capability that can be tracked?
97102
* This is true for

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,8 @@ class Definitions {
550550
@tu lazy val SingletonType: TypeRef = SingletonClass.typeRef
551551

552552
@tu lazy val MaybeCapabilityAnnot: ClassSymbol =
553-
completeClass(enterCompleteClassSymbol(
554-
ScalaPackageClass, tpnme.maybeCapability, Final, List(StaticAnnotationClass.typeRef)))
553+
completeClass(enterCompleteClassSymbol(
554+
ScalaPackageClass, tpnme.maybeCapability, Final, List(StaticAnnotationClass.typeRef)))
555555

556556
@tu lazy val CollectionSeqType: TypeRef = requiredClassRef("scala.collection.Seq")
557557
@tu lazy val SeqType: TypeRef = requiredClassRef("scala.collection.immutable.Seq")
@@ -1328,8 +1328,8 @@ class Definitions {
13281328
*/
13291329
object ByNameFunction:
13301330
def apply(tp: Type)(using Context): Type = tp match
1331-
case tp @ RetainingType(tp1, refs) if tp.annot.symbol == RetainsByNameAnnot =>
1332-
RetainingType(apply(tp1), refs)
1331+
case tp @ RetainingType(tp1, refSet) if tp.annot.symbol == RetainsByNameAnnot =>
1332+
RetainingType(apply(tp1), refSet)
13331333
case _ =>
13341334
defn.ContextFunction0.typeRef.appliedTo(tp :: Nil)
13351335
def unapply(tp: Type)(using Context): Option[Type] = tp match

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,17 @@ class PlainPrinter(_ctx: Context) extends Printer {
174174
// ~ Str("?").provided(!cs.isConst)
175175
core ~ cs.optionalInfo
176176

177-
private def toTextRetainedElem[T <: Untyped](ref: Tree[T]): Text = ref match
178-
case ref: RefTree[?] =>
179-
ref.typeOpt match
180-
case c: Capability => toTextCapability(c)
181-
case _ => toText(ref)
182-
case _ => toText(ref)
183-
184-
private def toTextRetainedElems[T <: Untyped](refs: List[Tree[T]]): Text =
177+
private def toTextRetainedElem(ref: Type): Text = ref match
178+
case c: Capability => toTextCapability(c)
179+
case _ =>
180+
try toTextCapability(ref.toCapability)
181+
catch case _ =>
182+
toText(ref)
183+
184+
private def toTextRetainedElems(refs: List[Type]): Text =
185185
"{" ~ Text(refs.map(ref => toTextRetainedElem(ref)), ", ") ~ "}"
186186

187-
type GeneralCaptureSet = CaptureSet | List[tpd.Tree]
187+
type GeneralCaptureSet = CaptureSet | List[Type]
188188

189189
protected def isUniversalCaptureSet(refs: GeneralCaptureSet): Boolean = refs match
190190
case refs: CaptureSet =>
@@ -203,12 +203,12 @@ class PlainPrinter(_ctx: Context) extends Printer {
203203
)
204204
isUniversal
205205
|| !refs.elems.isEmpty && refs.elems.forall(_.isCapOrFresh) && !ccVerbose
206-
case (ref: tpd.Tree) :: Nil => ref.symbol == defn.captureRoot
206+
case (ref: TermRef) :: Nil => ref.symbol == defn.captureRoot
207207
case _ => false
208208

209209
protected def toTextGeneralCaptureSet(refs: GeneralCaptureSet): Text = refs match
210210
case refs: CaptureSet => toTextCaptureSet(refs)
211-
case refs: List[tpd.Tree] => toTextRetainedElems(refs)
211+
case refs: List[Type] => toTextRetainedElems(refs)
212212

213213
/** Print capturing type, overridden in RefinedPrinter to account for
214214
* capturing function types.
@@ -284,10 +284,9 @@ class PlainPrinter(_ctx: Context) extends Printer {
284284
&& refs.isReadOnly
285285
then toText(parent)
286286
else toTextCapturing(parent, refs, boxText)
287-
case tp @ RetainingType(parent, refsType) =>
288-
val refs = refsType.retainedElementsRaw
287+
case tp @ RetainingType(parent, refSet) =>
289288
if Feature.ccEnabledSomewhere then
290-
toTextCapturing(parent, refs.map(r => ast.tpd.TypeTree(r)), "") ~ Str("R").provided(printDebug)
289+
toTextCapturing(parent, refSet.retainedElementsRaw, "") ~ Str("R").provided(printDebug)
291290
else toText(parent)
292291
case tp: PreviousErrorType if ctx.settings.XprintTypes.value =>
293292
"<error>" // do not print previously reported error message because they may try to print this error type again recursively
@@ -314,7 +313,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
314313
case AnnotatedType(parent, ann) if ann.symbol == defn.RetainsByNameAnnot =>
315314
ann.tree.retainedSet.retainedElementsRaw match
316315
case (ref: TermRef) :: Nil if ref.symbol == defn.captureRoot => Str("=>")
317-
case refs => Str("->") ~ toTextRetainedElems(refs.map(r => ast.tpd.TypeTree(r)))
316+
case refs => Str("->") ~ toTextRetainedElems(refs)
318317
case _ =>
319318
if Feature.pureFunsEnabled then "->" else "=>"
320319
changePrec(GlobalPrec)(arrowText ~ " " ~ toText(restp))

0 commit comments

Comments
 (0)