Skip to content

Commit 874837a

Browse files
committed
Refactor printing of capture sets
1 parent 63b6aae commit 874837a

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ sealed abstract class CaptureSet extends Showable:
320320
Annotation(CaptureAnnotation(this, boxed = false)(cls).tree)
321321

322322
override def toText(printer: Printer): Text =
323-
Str("{") ~ Text(elems.toList.map(printer.toTextCaptureRef), ", ") ~ Str("}") ~~ description
323+
printer.toTextCaptureSet(this)
324324

325325
object CaptureSet:
326326
type Refs = SimpleIdentitySet[CaptureRef]

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,16 @@ class PlainPrinter(_ctx: Context) extends Printer {
149149
+ defn.ObjectClass
150150
+ defn.FromJavaObjectSymbol
151151

152-
def toTextCaptureSet(cs: CaptureSet): Text =
153-
def nestingLevelStr = cs match
154-
case cs: CaptureSet.Var if showNestingLevel => s"<in ${cs.owner.show}/${cs.owner.ccNestingLevel}>"
155-
case _ => ""
152+
def toTextCaptureSet(cs: CaptureSet, describe: Boolean): Text =
153+
def descr = Str(cs.description).provided(describe)
154+
~ cs.match
155+
case cs: CaptureSet.Var if showNestingLevel => s"<in ${cs.owner.show}/${cs.owner.ccNestingLevel}>"
156+
case _ => ""
156157
if printDebug && !cs.isConst then cs.toString
157158
else if ctx.settings.YccDebug.value then cs.show
158159
else if cs == CaptureSet.Fluid then "<fluid>"
159-
else if !cs.isConst && cs.elems.isEmpty then Str("?") ~ nestingLevelStr
160-
else "{" ~ Text(cs.elems.toList.map(toTextCaptureRef), ", ") ~ "}" ~ nestingLevelStr
160+
else if !cs.isConst && cs.elems.isEmpty then Str("?") ~~ descr
161+
else "{" ~ Text(cs.elems.toList.map(toTextCaptureRef), ", ") ~ "}" ~~ descr
161162

162163
/** Print capturing type, overridden in RefinedPrinter to account for
163164
* capturing function types.
@@ -225,7 +226,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
225226
}.close
226227
case tp @ EventuallyCapturingType(parent, refs) =>
227228
val boxText: Text = Str("box ") provided tp.isBoxed //&& ctx.settings.YccDebug.value
228-
val refsText = if refs.isUniversal then rootSetText else toTextCaptureSet(refs)
229+
val refsText = if refs.isUniversal then rootSetText else toTextCaptureSet(refs, describe = false)
229230
toTextCapturing(parent, refsText, boxText)
230231
case tp: PreviousErrorType if ctx.settings.XprintTypes.value =>
231232
"<error>" // do not print previously reported error message because they may try to print this error type again recuresevely
@@ -250,7 +251,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
250251
case ExprType(restp) =>
251252
def arrowText: Text = restp match
252253
case ct @ EventuallyCapturingType(parent, refs) if ct.annot.symbol == defn.RetainsByNameAnnot =>
253-
if refs.isUniversal then Str("=>") else Str("->") ~ toTextCaptureSet(refs)
254+
if refs.isUniversal then Str("=>") else Str("->") ~ toTextCaptureSet(refs, describe = false)
254255
case _ =>
255256
if Feature.pureFunsEnabled then "->" else "=>"
256257
changePrec(GlobalPrec)(arrowText ~ " " ~ toText(restp))

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Types.{Type, SingletonType, LambdaParam, NamedType},
1010
import typer.Implicits.SearchResult
1111
import util.SourcePosition
1212
import typer.ImportInfo
13+
import cc.CaptureSet
1314

1415
import scala.annotation.internal.sharable
1516

@@ -106,6 +107,9 @@ abstract class Printer {
106107
/** Textual representation of a reference in a capture set */
107108
def toTextCaptureRef(tp: Type): Text
108109

110+
/** Textual representation of a reference in a capture set */
111+
def toTextCaptureSet(cs: CaptureSet, describe: Boolean = true): Text
112+
109113
/** Textual representation of symbol's declaration */
110114
def dclText(sym: Symbol): Text
111115

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
634634
def toTextAnnot =
635635
toTextLocal(arg) ~~ annotText(annot.symbol.enclosingClass, annot)
636636
def toTextRetainsAnnot =
637-
try changePrec(GlobalPrec)(toText(arg) ~ "^" ~ toTextCaptureSet(captureSet))
637+
try changePrec(GlobalPrec)(toText(arg) ~ "^" ~ toTextCaptureSet(captureSet, describe = false))
638638
catch case ex: IllegalCaptureRef => toTextAnnot
639639
if annot.symbol.maybeOwner == defn.RetainsAnnot
640640
&& Feature.ccEnabled && Config.printCaptureSetsAsPrefix && !printDebug

0 commit comments

Comments
 (0)