Skip to content

Commit 12fcc8b

Browse files
committed
Elide this for types in parent list and self annotations
1 parent 523f807 commit 12fcc8b

File tree

1 file changed

+49
-31
lines changed

1 file changed

+49
-31
lines changed

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ trait Printers
433433
(new Buffer).printPattern(pattern).result()
434434

435435
def showTypeOrBounds(tpe: TypeOrBounds)(implicit ctx: Context): String =
436-
(new Buffer).printTypeOrBound(tpe).result()
436+
(new Buffer).printTypeOrBound(tpe)(None).result()
437437

438438
def showConstant(const: Constant)(implicit ctx: Context): String =
439439
(new Buffer).printConstant(const).result()
@@ -516,7 +516,7 @@ trait Printers
516516
def lineBreak(): String = "\n" + (" " * indent)
517517
def doubleLineBreak(): String = "\n\n" + (" " * indent)
518518

519-
def printTree(tree: Tree): Buffer = tree match {
519+
def printTree(tree: Tree)(implicit elideThis: Option[Symbol] = None): Buffer = tree match {
520520
case PackageObject(body)=>
521521
printTree(body) // Print package object
522522

@@ -582,19 +582,19 @@ trait Printers
582582

583583
def printParent(parent: Tree /* Term | TypeTree */, needEmptyParens: Boolean = false): Unit = parent match {
584584
case IsTypeTree(parent) =>
585-
printTypeTree(parent)
585+
printTypeTree(parent)(Some(cdef.symbol))
586586
case IsTerm(Term.TypeApply(fun, targs)) =>
587587
printParent(fun)
588588
case IsTerm(Term.Apply(fun@Term.Apply(_,_), args)) =>
589589
printParent(fun, true)
590590
if (!args.isEmpty || needEmptyParens)
591-
inParens(printTrees(args, ", "))
591+
inParens(printTrees(args, ", ")(Some(cdef.symbol)))
592592
case IsTerm(Term.Apply(fun, args)) =>
593593
printParent(fun)
594594
if (!args.isEmpty || needEmptyParens)
595-
inParens(printTrees(args, ", "))
595+
inParens(printTrees(args, ", ")(Some(cdef.symbol)))
596596
case IsTerm(Term.Select(Term.IsNew(newTree), _)) =>
597-
printType(newTree.tpe)
597+
printType(newTree.tpe)(Some(cdef.symbol))
598598
case IsTerm(parent) =>
599599
throw new MatchError(parent.show)
600600
}
@@ -646,7 +646,7 @@ trait Printers
646646
indented {
647647
val name1 = if (name == "_") "this" else name
648648
this += " " += highlightValDef(name1, color) += ": "
649-
printTypeTree(tpt)
649+
printTypeTree(tpt)(Some(cdef.symbol))
650650
this += " =>"
651651
}
652652
}
@@ -971,7 +971,7 @@ trait Printers
971971
(flatStats.result(), flatExpr)
972972
}
973973

974-
def printFlatBlock(stats: List[Statement], expr: Term): Buffer = {
974+
def printFlatBlock(stats: List[Statement], expr: Term)(implicit elideThis: Option[Symbol]): Buffer = {
975975
val (stats1, expr1) = flatBlock(stats, expr)
976976
// Remove Term.Lambda nodes, lambdas are printed by their definition
977977
val stats2 = stats1.filter { case Term.Lambda(_, _) => false; case _ => true }
@@ -992,7 +992,7 @@ trait Printers
992992
}
993993
}
994994

995-
def printStats(stats: List[Tree], expr: Tree): Unit = {
995+
def printStats(stats: List[Tree], expr: Tree)(implicit elideThis: Option[Symbol]): Unit = {
996996
def printSeparator(next: Tree): Unit = {
997997
// Avoid accidental application of opening `{` on next line with a double break
998998
def rec(next: Tree): Unit = next match {
@@ -1039,13 +1039,13 @@ trait Printers
10391039
this
10401040
}
10411041

1042-
def printTrees(trees: List[Tree], sep: String): Buffer =
1043-
printList(trees, sep, printTree)
1042+
def printTrees(trees: List[Tree], sep: String)(implicit elideThis: Option[Symbol]): Buffer =
1043+
printList(trees, sep, (t: Tree) => printTree(t))
10441044

1045-
def printTypeTrees(trees: List[TypeTree], sep: String): Buffer =
1046-
printList(trees, sep, printTypeTree)
1045+
def printTypeTrees(trees: List[TypeTree], sep: String)(implicit elideThis: Option[Symbol] = None): Buffer =
1046+
printList(trees, sep, (t: TypeTree) => printTypeTree(t))
10471047

1048-
def printTypes(trees: List[Type], sep: String): Buffer = {
1048+
def printTypes(trees: List[Type], sep: String)(implicit elideThis: Option[Symbol]): Buffer = {
10491049
def printSeparated(list: List[Type]): Unit = list match {
10501050
case Nil =>
10511051
case x :: Nil => printType(x)
@@ -1111,7 +1111,7 @@ trait Printers
11111111
this
11121112
}
11131113

1114-
def printTypesOrBounds(types: List[TypeOrBounds], sep: String): Buffer = {
1114+
def printTypesOrBounds(types: List[TypeOrBounds], sep: String)(implicit elideThis: Option[Symbol]): Buffer = {
11151115
def printSeparated(list: List[TypeOrBounds]): Unit = list match {
11161116
case Nil =>
11171117
case x :: Nil => printTypeOrBound(x)
@@ -1124,7 +1124,7 @@ trait Printers
11241124
this
11251125
}
11261126

1127-
def printTargsDefs(targs: List[(TypeDef, TypeDef)], isDef:Boolean = true): Unit = {
1127+
def printTargsDefs(targs: List[(TypeDef, TypeDef)], isDef:Boolean = true)(implicit elideThis: Option[Symbol]): Unit = {
11281128
if (!targs.isEmpty) {
11291129
def printSeparated(list: List[(TypeDef, TypeDef)]): Unit = list match {
11301130
case Nil =>
@@ -1139,7 +1139,7 @@ trait Printers
11391139
}
11401140
}
11411141

1142-
def printTargDef(arg: (TypeDef, TypeDef), isMember: Boolean = false, isDef:Boolean = true): Buffer = {
1142+
def printTargDef(arg: (TypeDef, TypeDef), isMember: Boolean = false, isDef:Boolean = true)(implicit elideThis: Option[Symbol]): Buffer = {
11431143
val (argDef, argCons) = arg
11441144

11451145
if (isDef) {
@@ -1189,7 +1189,7 @@ trait Printers
11891189
}
11901190
}
11911191

1192-
def printArgsDefs(args: List[ValDef]): Unit = inParens {
1192+
def printArgsDefs(args: List[ValDef])(implicit elideThis: Option[Symbol]): Unit = inParens {
11931193
args match {
11941194
case Nil =>
11951195
case arg :: _ =>
@@ -1209,7 +1209,7 @@ trait Printers
12091209
printSeparated(args)
12101210
}
12111211

1212-
def printAnnotations(trees: List[Term]): Buffer = {
1212+
def printAnnotations(trees: List[Term])(implicit elideThis: Option[Symbol]): Buffer = {
12131213
def printSeparated(list: List[Term]): Unit = list match {
12141214
case Nil =>
12151215
case x :: Nil => printAnnotation(x)
@@ -1222,7 +1222,7 @@ trait Printers
12221222
this
12231223
}
12241224

1225-
def printParamDef(arg: ValDef): Unit = {
1225+
def printParamDef(arg: ValDef)(implicit elideThis: Option[Symbol]): Unit = {
12261226
val name = arg.name
12271227
arg.symbol.owner match {
12281228
case IsDefSymbol(sym) if sym.name == "<init>" =>
@@ -1260,7 +1260,7 @@ trait Printers
12601260
indented {
12611261
caseDef.rhs match {
12621262
case Term.Block(stats, expr) =>
1263-
printStats(stats, expr)
1263+
printStats(stats, expr)(None)
12641264
case body =>
12651265
this += lineBreak()
12661266
printTree(body)
@@ -1334,7 +1334,7 @@ trait Printers
13341334
this += highlightLiteral("'" + v.name, color)
13351335
}
13361336

1337-
def printTypeOrBoundsTree(tpt: Tree /*TypeTree | TypeBoundsTree*/): Buffer = tpt match {
1337+
def printTypeOrBoundsTree(tpt: Tree)(implicit elideThis: Option[Symbol] = None): Buffer = tpt match {
13381338
case TypeBoundsTree(lo, hi) =>
13391339
this += "_ >: "
13401340
printTypeTree(lo)
@@ -1346,7 +1346,15 @@ trait Printers
13461346
printTypeTree(tpt)
13471347
}
13481348

1349-
def printTypeTree(tree: TypeTree): Buffer = tree match {
1349+
/** Print type tree
1350+
*
1351+
* @param elideThis Shoud printing elide `C.this` for the given class `C`?
1352+
* None means no eliding.
1353+
*
1354+
* Self type annotation and types in parent list should elide current class
1355+
* prefix `C.this` to avoid type checking errors.
1356+
*/
1357+
def printTypeTree(tree: TypeTree)(implicit elideThis: Option[Symbol] = None): Buffer = tree match {
13501358
case TypeTree.Inferred() =>
13511359
// TODO try to move this logic into `printType`
13521360
def printTypeAndAnnots(tpe: Type): Buffer = tpe match {
@@ -1432,7 +1440,7 @@ trait Printers
14321440

14331441
}
14341442

1435-
def printTypeOrBound(tpe: TypeOrBounds): Buffer = tpe match {
1443+
def printTypeOrBound(tpe: TypeOrBounds)(implicit elideThis: Option[Symbol]): Buffer = tpe match {
14361444
case tpe@TypeBounds(lo, hi) =>
14371445
this += "_ >: "
14381446
printType(lo)
@@ -1441,7 +1449,15 @@ trait Printers
14411449
case IsType(tpe) => printType(tpe)
14421450
}
14431451

1444-
def printType(tpe: Type): Buffer = tpe match {
1452+
/** Print type
1453+
*
1454+
* @param elideThis Shoud printing elide `C.this` for the given class `C`?
1455+
* None means no eliding.
1456+
*
1457+
* Self type annotation and types in parent list should elide current class
1458+
* prefix `C.this` to avoid type checking errors.
1459+
*/
1460+
def printType(tpe: Type)(implicit elideThis: Option[Symbol] = None): Buffer = tpe match {
14451461
case Type.ConstantType(const) =>
14461462
printConstant(const)
14471463

@@ -1458,6 +1474,8 @@ trait Printers
14581474
case IsType(prefix @ Type.SymRef(IsClassSymbol(_), _)) =>
14591475
printType(prefix)
14601476
this += "#"
1477+
case IsType(Type.ThisType(Type.SymRef(cdef, _)))
1478+
if elideThis.nonEmpty && cdef == elideThis.get =>
14611479
case IsType(prefix) =>
14621480
printType(prefix)
14631481
this += "."
@@ -1590,7 +1608,7 @@ trait Printers
15901608
case PackageDef(name, _) => this += highlightTypeDef(name, color)
15911609
}
15921610

1593-
def printAnnotation(annot: Term): Buffer = {
1611+
def printAnnotation(annot: Term)(implicit elideThis: Option[Symbol]): Buffer = {
15941612
val Annotation(ref, args) = annot
15951613
this += "@"
15961614
printTypeTree(ref)
@@ -1600,7 +1618,7 @@ trait Printers
16001618
inParens(printTrees(args, ", "))
16011619
}
16021620

1603-
def printDefAnnotations(definition: Definition): Buffer = {
1621+
def printDefAnnotations(definition: Definition)(implicit elideThis: Option[Symbol]): Buffer = {
16041622
val annots = definition.symbol.annots.filter {
16051623
case Annotation(annot, _) =>
16061624
annot.tpe match {
@@ -1615,7 +1633,7 @@ trait Printers
16151633
else this
16161634
}
16171635

1618-
def printRefinement(tpe: Type): Buffer = {
1636+
def printRefinement(tpe: Type)(implicit elideThis: Option[Symbol]): Buffer = {
16191637
def printMethodicType(tp: TypeOrBounds): Unit = tp match {
16201638
case tp @ Type.MethodType(paramNames, params, res) =>
16211639
inParens(printMethodicTypeParams(paramNames, params))
@@ -1655,7 +1673,7 @@ trait Printers
16551673
this += lineBreak() += "}"
16561674
}
16571675

1658-
def printMethodicTypeParams(paramNames: List[String], params: List[TypeOrBounds]): Unit = {
1676+
def printMethodicTypeParams(paramNames: List[String], params: List[TypeOrBounds])(implicit elideThis: Option[Symbol]): Unit = {
16591677
def printInfo(info: TypeOrBounds) = info match {
16601678
case IsTypeBounds(info) => printBounds(info)
16611679
case IsType(info) =>
@@ -1676,7 +1694,7 @@ trait Printers
16761694
printSeparated(paramNames.zip(params))
16771695
}
16781696

1679-
def printBoundsTree(bounds: TypeBoundsTree): Buffer = {
1697+
def printBoundsTree(bounds: TypeBoundsTree)(implicit elideThis: Option[Symbol]): Buffer = {
16801698
bounds.low match {
16811699
case TypeTree.Inferred() =>
16821700
case low =>
@@ -1691,7 +1709,7 @@ trait Printers
16911709
}
16921710
}
16931711

1694-
def printBounds(bounds: TypeBounds): Buffer = {
1712+
def printBounds(bounds: TypeBounds)(implicit elideThis: Option[Symbol]): Buffer = {
16951713
this += " >: "
16961714
printType(bounds.low)
16971715
this += " <: "

0 commit comments

Comments
 (0)