Skip to content

Commit 4f119bc

Browse files
Explicit Text to String conversion
1 parent 27ee9a4 commit 4f119bc

File tree

9 files changed

+81
-74
lines changed

9 files changed

+81
-74
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
601601
" := " ~ tp.toText(printer)
602602
}
603603
val indent = 3
604-
val header: Text = "Constraint("
604+
val header: Text = "Constraint(".toText
605605
val uninstVarsText = " uninstVars = " ~
606606
Text(uninstVars map (_.toText(printer)), ", ") ~ ";"
607607
val constrainedText =
@@ -626,7 +626,7 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
626626
Text(ups.map(_.toText(printer)), ", ")
627627
Text(deps, "\n")
628628
}
629-
Text.lines(List(header, uninstVarsText, constrainedText, boundsText, orderingText, ")"))
629+
Text.lines(List(header, uninstVarsText, constrainedText, boundsText, orderingText, ")".toText))
630630
}
631631

632632
override def toString: String = {

compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ object TreePickler {
2222

2323
case class Hole(idx: Int, args: List[tpd.Tree]) extends tpd.TermTree {
2424
override def fallbackToText(printer: Printer): Text =
25-
s"[[$idx|" ~~ printer.toTextGlobal(args, ", ") ~~ "]]"
25+
s"[[$idx|".toText ~~ printer.toTextGlobal(args, ", ") ~~ "]]"
2626
}
2727
}
2828

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ object Formatting {
138138
seen.record(super.ParamRefNameString(param), param)
139139

140140
override def toTextRef(tp: SingletonType): Text = tp match {
141-
case tp: SkolemType => seen.record(tp.repr.toString, tp)
141+
case tp: SkolemType => seen.record(tp.repr.toString, tp).toText
142142
case _ => super.toTextRef(tp)
143143
}
144144
}

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

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
3232
else {
3333
if (ctx.toTextRecursions >= maxToTextRecursions)
3434
recursionLimitExceeded()
35-
"..."
35+
"...".toText
3636
}
3737

3838
protected def recursionLimitExceeded() = {
@@ -138,7 +138,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
138138
else
139139
toTextPrefix(tp.prefix) ~ selectionString(tp)
140140
case tp: TermParamRef =>
141-
ParamRefNameString(tp) ~ ".type"
141+
ParamRefNameString(tp) ~ ".type".toText
142142
case tp: TypeParamRef =>
143143
ParamRefNameString(tp) ~ lambdaHash(tp.binder)
144144
case tp: SingletonType =>
@@ -152,21 +152,21 @@ class PlainPrinter(_ctx: Context) extends Printer {
152152
case tp: RecType =>
153153
try {
154154
openRecs = tp :: openRecs
155-
"{" ~ selfRecName(openRecs.length) ~ " => " ~ toTextGlobal(tp.parent) ~ "}"
155+
"{" ~ selfRecName(openRecs.length).toText ~ " => " ~ toTextGlobal(tp.parent) ~ "}"
156156
}
157157
finally openRecs = openRecs.tail
158158
case AndType(tp1, tp2) =>
159159
changePrec(AndPrec) { toText(tp1) ~ " & " ~ toText(tp2) }
160160
case OrType(tp1, tp2) =>
161161
changePrec(OrPrec) { toText(tp1) ~ " | " ~ toText(tp2) }
162162
case tp: ErrorType =>
163-
s"<error ${tp.msg.msg}>"
163+
s"<error ${tp.msg.msg}>".toText
164164
case tp: WildcardType =>
165-
if (tp.optBounds.exists) "(?" ~ toTextRHS(tp.bounds) ~ ")" else "?"
165+
if (tp.optBounds.exists) "(?" ~ toTextRHS(tp.bounds) ~ ")" else "?".toText
166166
case NoType =>
167-
"<notype>"
167+
"<notype>".toText
168168
case NoPrefix =>
169-
"<noprefix>"
169+
"<noprefix>".toText
170170
case tp: MethodType =>
171171
changePrec(GlobalPrec) {
172172
(if (tp.isImplicitMethod) "(implicit " else "(") ~ paramsText(tp) ~
@@ -222,10 +222,10 @@ class PlainPrinter(_ctx: Context) extends Printer {
222222

223223
/** If -uniqid is set, the hashcode of the lambda type, after a # */
224224
protected def lambdaHash(pt: LambdaType): Text =
225-
if (ctx.settings.uniqid.value)
225+
(if (ctx.settings.uniqid.value)
226226
try "#" + pt.hashCode
227227
catch { case ex: NullPointerException => "" }
228-
else ""
228+
else "").toText
229229

230230
/** If -uniqid is set, the unique id of symbol, after a # */
231231
protected def idString(sym: Symbol): String =
@@ -259,19 +259,19 @@ class PlainPrinter(_ctx: Context) extends Printer {
259259
case tp: TermRef =>
260260
toTextPrefix(tp.prefix) ~ selectionString(tp)
261261
case tp: ThisType =>
262-
nameString(tp.cls) + ".this"
262+
(nameString(tp.cls) + ".this").toText
263263
case SuperType(thistpe: SingletonType, _) =>
264264
toTextRef(thistpe).map(_.replaceAll("""\bthis$""", "super"))
265265
case SuperType(thistpe, _) =>
266266
"Super(" ~ toTextGlobal(thistpe) ~ ")"
267267
case tp @ ConstantType(value) =>
268268
toText(value)
269269
case pref: TermParamRef =>
270-
nameString(pref.binder.paramNames(pref.paramNum))
270+
nameString(pref.binder.paramNames(pref.paramNum)).toText
271271
case tp: RecThis =>
272272
val idx = openRecs.reverse.indexOf(tp.binder)
273-
if (idx >= 0) selfRecName(idx + 1)
274-
else "{...}.this" // TODO move underlying type to an addendum, e.g. ... z3 ... where z3: ...
273+
(if (idx >= 0) selfRecName(idx + 1)
274+
else "{...}.this").toText // TODO move underlying type to an addendum, e.g. ... z3 ... where z3: ...
275275
case tp: SkolemType =>
276276
if (homogenizedView) toText(tp.info) else toText(tp.repr)
277277
}
@@ -280,7 +280,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
280280
/** The string representation of this type used as a prefix */
281281
protected def toTextPrefix(tp: Type): Text = controlled {
282282
homogenize(tp) match {
283-
case NoPrefix => ""
283+
case NoPrefix => "".toText
284284
case tp: SingletonType => toTextRef(tp) ~ "."
285285
case tp => trimPrefix(toTextLocal(tp)) ~ "#"
286286
}
@@ -297,7 +297,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
297297
*/
298298
protected def toTextRHS(optType: Option[Type]): Text = optType match {
299299
case Some(tp) => toTextRHS(tp)
300-
case None => "?"
300+
case None => "?".toText
301301
}
302302

303303
/** String representation of a definition's type following its name */
@@ -315,7 +315,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
315315
if (tparams.isEmpty) Text() else ("[" ~ dclsText(tparams) ~ "]").close
316316
val selfText: Text = selfInfo match {
317317
case NoType => Text()
318-
case sym: Symbol if !sym.isCompleted => "this: ? =>"
318+
case sym: Symbol if !sym.isCompleted => "this: ? =>".toText
319319
case _ => "this: " ~ atPrec(InfixPrec) { toText(tp.selfType) } ~ " =>"
320320
}
321321
val trueDecls = otherDecls.filterNot(treatAsTypeArg)
@@ -379,7 +379,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
379379

380380
/** String representation of symbol's flags */
381381
protected def toTextFlags(sym: Symbol): Text =
382-
Text(sym.flagsUNSAFE.flagStrings map stringToText, " ")
382+
Text(sym.flagsUNSAFE.flagStrings.map(_.toText), " ")
383383

384384
/** String representation of symbol's variance or "" if not applicable */
385385
protected def varianceString(sym: Symbol): String = varianceString(sym.variance)
@@ -398,17 +398,17 @@ class PlainPrinter(_ctx: Context) extends Printer {
398398

399399
private def dclTextWithInfo(sym: Symbol, info: Option[Type]): Text =
400400
(toTextFlags(sym) ~~ keyString(sym) ~~
401-
(varianceString(sym) ~ nameString(sym)) ~ toTextRHS(info)).close
401+
(varianceString(sym) ~ nameString(sym).toText) ~ toTextRHS(info)).close
402402

403403
def toText(sym: Symbol): Text =
404-
(kindString(sym) ~~ {
404+
(kindString(sym).toText ~~ {
405405
if (sym.isAnonymousClass) toText(sym.info.parents, " with ") ~ "{...}"
406-
else if (hasMeaninglessName(sym)) simpleNameString(sym.owner) + idString(sym)
407-
else nameString(sym)
406+
else if (hasMeaninglessName(sym)) (simpleNameString(sym.owner) + idString(sym)).toText
407+
else nameString(sym).toText
408408
}).close
409409

410410
def locationText(sym: Symbol): Text =
411-
if (!sym.exists) ""
411+
if (!sym.exists) "".toText
412412
else {
413413
val ownr = sym.effectiveOwner
414414
if (ownr.isClass && !isEmptyPrefix(ownr)) " in " ~ toText(ownr) else Text()
@@ -418,18 +418,18 @@ class PlainPrinter(_ctx: Context) extends Printer {
418418
(toText(sym) ~ locationText(sym)).close
419419

420420
def extendedLocationText(sym: Symbol): Text =
421-
if (!sym.exists) ""
421+
if (!sym.exists) "".toText
422422
else {
423423
def recur(ownr: Symbol, innerLocation: String): Text = {
424424
def nextOuter(innerKind: String): Text =
425425
recur(ownr.effectiveOwner,
426426
if (!innerLocation.isEmpty) innerLocation
427427
else s" in an anonymous $innerKind")
428428
def showLocation(ownr: Symbol, where: String): Text =
429-
innerLocation ~ " " ~ where ~ " " ~ toText(ownr)
429+
innerLocation ~ " ".toText ~ where ~ " " ~ toText(ownr)
430430
if (ownr.isAnonymousClass) nextOuter("class")
431431
else if (ownr.isAnonymousFunction) nextOuter("function")
432-
else if (isEmptyPrefix(ownr)) ""
432+
else if (isEmptyPrefix(ownr)) "".toText
433433
else if (ownr.isLocalDummy) showLocation(ownr.owner, "locally defined in")
434434
else if (ownr.isTerm && !ownr.is(Module | Method)) showLocation(ownr, "in the initalizer of")
435435
else showLocation(ownr, "in")
@@ -461,7 +461,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
461461
case _ => literalText(String.valueOf(const.value))
462462
}
463463

464-
def toText(annot: Annotation): Text = s"@${annot.symbol.name}" // for now
464+
def toText(annot: Annotation): Text = s"@${annot.symbol.name}".toText // for now
465465

466466
protected def escapedString(str: String): String = str flatMap escapedChar
467467

@@ -476,9 +476,9 @@ class PlainPrinter(_ctx: Context) extends Printer {
476476
def toTextElem(elem: Any): Text = elem match {
477477
case elem: Showable => elem.toText(this)
478478
case elem: List[_] => "List(" ~ Text(elem map toTextElem, ",") ~ ")"
479-
case elem => elem.toString
479+
case elem => elem.toString.toText
480480
}
481-
val nodeName = node.productPrefix
481+
val nodeName = node.productPrefix.toText
482482
val elems =
483483
Text(node.productIterator.map(toTextElem).toList, ", ")
484484
val tpSuffix =
@@ -498,13 +498,13 @@ class PlainPrinter(_ctx: Context) extends Printer {
498498
"SearchSuccess: " ~ toText(result.ref) ~ " via " ~ toText(result.tree)
499499
case result: SearchFailure =>
500500
result.reason match {
501-
case _: NoMatchingImplicits => "No Matching Implicit"
502-
case _: DivergingImplicit => "Diverging Implicit"
503-
case _: ShadowedImplicit => "Shadowed Implicit"
501+
case _: NoMatchingImplicits => "No Matching Implicit".toText
502+
case _: DivergingImplicit => "Diverging Implicit".toText
503+
case _: ShadowedImplicit => "Shadowed Implicit".toText
504504
case result: AmbiguousImplicits =>
505505
"Ambiguous Implicit: " ~ toText(result.alt1.ref) ~ " and " ~ toText(result.alt2.ref)
506506
case _ =>
507-
"?Unknown Implicit Result?" + result.getClass
507+
s"?Unknown Implicit Result?${result.getClass}".toText
508508
}
509509
}
510510

@@ -515,7 +515,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
515515
case Ident(name) :: Nil => name.show
516516
case _ => "{...}"
517517
}
518-
s"import $exprStr.$selectorStr"
518+
s"import $exprStr.$selectorStr".toText
519519
}
520520

521521

@@ -533,15 +533,16 @@ class PlainPrinter(_ctx: Context) extends Printer {
533533
def plain = this
534534

535535
protected def keywordStr(text: String): String = coloredStr(text, SyntaxHighlighting.KeywordColor)
536-
protected def keywordText(text: String): Text = coloredStr(text, SyntaxHighlighting.KeywordColor)
536+
protected def keywordText(text: String): Text = keywordStr(text).toText
537537
protected def valDefText(text: Text): Text = coloredText(text, SyntaxHighlighting.ValDefColor)
538538
protected def typeText(text: Text): Text = coloredText(text, SyntaxHighlighting.TypeColor)
539539
protected def literalText(text: Text): Text = coloredText(text, SyntaxHighlighting.LiteralColor)
540+
protected def literalText(string: String): Text = literalText(string.toText)
540541
protected def stringText(text: Text): Text = coloredText(text, SyntaxHighlighting.StringColor)
542+
protected def stringText(string: String): Text = stringText(string.toText)
541543

542544
private def coloredStr(text: String, color: String): String =
543545
if (ctx.useColors) color + text + SyntaxHighlighting.NoColor else text
544546
private def coloredText(text: Text, color: String): Text =
545547
if (ctx.useColors) color ~ text ~ SyntaxHighlighting.NoColor else text
546548
}
547-

0 commit comments

Comments
 (0)