Skip to content

Commit 6893878

Browse files
committed
Cleanup code
1 parent 085df3d commit 6893878

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
@@ -62,7 +62,7 @@ extension (tree: Tree)
6262
refs
6363

6464
/** The type representing the capture set of retains annotation.
65-
*/
65+
*/
6666
def retainedSet(using Context): Type =
6767
tree match
6868
case Apply(TypeApply(_, refs :: Nil), _) => refs.tpe
@@ -72,19 +72,22 @@ extension (tree: Tree)
7272

7373
extension (tp: Type)
7474

75-
def toCapabilities(using Context): List[Capability] = tp match
75+
def toCapability(using Context): Capability = tp match
7676
case ReachCapability(tp1) =>
77-
tp1.toCapabilities.map(_.reach)
77+
tp1.toCapability.reach
7878
case ReadOnlyCapability(tp1) =>
79-
tp1.toCapabilities.map(_.readOnly)
79+
tp1.toCapability.readOnly
8080
case ref: TermRef if ref.isCapRef =>
81-
GlobalCap :: Nil
81+
GlobalCap
8282
case ref: Capability if ref.isTrackableRef =>
83-
ref :: Nil
83+
ref
8484
case _ =>
8585
// if this was compiled from cc syntax, problem should have been reported at Typer
8686
throw IllegalCaptureRef(tp)
8787

88+
/** A list of raw elements of a retained set.
89+
* This will not crash even if it contains a non-wellformed Capability.
90+
*/
8891
def retainedElementsRaw(using Context): List[Type] = tp match
8992
case OrType(tp1, tp2) =>
9093
tp1.retainedElementsRaw ++ tp2.retainedElementsRaw
@@ -93,8 +96,10 @@ extension (tp: Type)
9396
if tp.isNothingType then Nil
9497
else tp :: Nil // should be checked by wellformedness
9598

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

99104
/** Is this type a Capability that can be tracked?
100105
* 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")
@@ -1326,8 +1326,8 @@ class Definitions {
13261326
*/
13271327
object ByNameFunction:
13281328
def apply(tp: Type)(using Context): Type = tp match
1329-
case tp @ RetainingType(tp1, refs) if tp.annot.symbol == RetainsByNameAnnot =>
1330-
RetainingType(apply(tp1), refs)
1329+
case tp @ RetainingType(tp1, refSet) if tp.annot.symbol == RetainsByNameAnnot =>
1330+
RetainingType(apply(tp1), refSet)
13311331
case _ =>
13321332
defn.ContextFunction0.typeRef.appliedTo(tp :: Nil)
13331333
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)