Skip to content

Commit 5596dfe

Browse files
committed
Elide this for types in parent list and self annotations
1 parent 163e35a commit 5596dfe

File tree

1 file changed

+50
-32
lines changed

1 file changed

+50
-32
lines changed

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

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ trait Printers
465465
(new Buffer).printTypeOrBoundsTree(tpt).result()
466466

467467
def showTypeOrBounds(tpe: TypeOrBounds)(implicit ctx: Context): String =
468-
(new Buffer).printTypeOrBound(tpe).result()
468+
(new Buffer).printTypeOrBound(tpe)(None).result()
469469

470470
def showConstant(const: Constant)(implicit ctx: Context): String =
471471
(new Buffer).printConstant(const).result()
@@ -544,7 +544,7 @@ trait Printers
544544
def lineBreak(): String = "\n" + (" " * indent)
545545
def doubleLineBreak(): String = "\n\n" + (" " * indent)
546546

547-
def printTree(tree: Tree): Buffer = tree match {
547+
def printTree(tree: Tree)(implicit elideThis: Option[Symbol] = None): Buffer = tree match {
548548
case PackageObject(body)=>
549549
printTree(body) // Print package object
550550

@@ -609,19 +609,19 @@ trait Printers
609609

610610
def printParent(parent: TermOrTypeTree, needEmptyParens: Boolean = false): Unit = parent match {
611611
case IsTypeTree(parent) =>
612-
printTypeTree(parent)
612+
printTypeTree(parent)(Some(cdef.symbol))
613613
case IsTerm(Term.TypeApply(fun, targs)) =>
614614
printParent(fun)
615615
case IsTerm(Term.Apply(fun@Term.Apply(_,_), args)) =>
616616
printParent(fun, true)
617617
if (!args.isEmpty || needEmptyParens)
618-
inParens(printTrees(args, ", "))
618+
inParens(printTrees(args, ", ")(Some(cdef.symbol)))
619619
case IsTerm(Term.Apply(fun, args)) =>
620620
printParent(fun)
621621
if (!args.isEmpty || needEmptyParens)
622-
inParens(printTrees(args, ", "))
622+
inParens(printTrees(args, ", ")(Some(cdef.symbol)))
623623
case IsTerm(Term.Select(Term.New(tpt), _)) =>
624-
printTypeTree(tpt)
624+
printTypeTree(tpt)(Some(cdef.symbol))
625625
case IsTerm(parent) =>
626626
throw new MatchError(parent.show)
627627
}
@@ -673,7 +673,7 @@ trait Printers
673673
indented {
674674
val name1 = if (name == "_") "this" else name
675675
this += " " += highlightValDef(name1, color) += ": "
676-
printTypeTree(tpt)
676+
printTypeTree(tpt)(Some(cdef.symbol))
677677
this += " =>"
678678
}
679679
}
@@ -966,7 +966,7 @@ trait Printers
966966
(flatStats.result(), flatExpr)
967967
}
968968

969-
def printFlatBlock(stats: List[Statement], expr: Term): Buffer = {
969+
def printFlatBlock(stats: List[Statement], expr: Term)(implicit elideThis: Option[Symbol]): Buffer = {
970970
val (stats1, expr1) = flatBlock(stats, expr)
971971
// Remove Term.Lambda nodes, lambdas are printed by their definition
972972
val stats2 = stats1.filter { case Term.Lambda(_, _) => false; case _ => true }
@@ -987,7 +987,7 @@ trait Printers
987987
}
988988
}
989989

990-
def printStats(stats: List[Tree], expr: Tree): Unit = {
990+
def printStats(stats: List[Tree], expr: Tree)(implicit elideThis: Option[Symbol]): Unit = {
991991
def printSeparator(next: Tree): Unit = {
992992
// Avoid accidental application of opening `{` on next line with a double break
993993
def rec(next: Tree): Unit = next match {
@@ -1033,13 +1033,13 @@ trait Printers
10331033
this
10341034
}
10351035

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

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

1042-
def printTypes(trees: List[Type], sep: String): Buffer = {
1042+
def printTypes(trees: List[Type], sep: String)(implicit elideThis: Option[Symbol]): Buffer = {
10431043
def printSeparated(list: List[Type]): Unit = list match {
10441044
case Nil =>
10451045
case x :: Nil => printType(x)
@@ -1105,7 +1105,7 @@ trait Printers
11051105
this
11061106
}
11071107

1108-
def printTypeOrBoundsTrees(typesTrees: List[TypeOrBoundsTree], sep: String): Buffer = {
1108+
def printTypeOrBoundsTrees(typesTrees: List[TypeOrBoundsTree], sep: String)(implicit elideThis: Option[Symbol]): Buffer = {
11091109
def printSeparated(list: List[TypeOrBoundsTree]): Unit = list match {
11101110
case Nil =>
11111111
case x :: Nil => printTypeOrBoundsTree(x)
@@ -1118,7 +1118,7 @@ trait Printers
11181118
this
11191119
}
11201120

1121-
def printTypesOrBounds(types: List[TypeOrBounds], sep: String): Buffer = {
1121+
def printTypesOrBounds(types: List[TypeOrBounds], sep: String)(implicit elideThis: Option[Symbol]): Buffer = {
11221122
def printSeparated(list: List[TypeOrBounds]): Unit = list match {
11231123
case Nil =>
11241124
case x :: Nil => printTypeOrBound(x)
@@ -1131,7 +1131,7 @@ trait Printers
11311131
this
11321132
}
11331133

1134-
def printTargsDefs(targs: List[(TypeDef, TypeDef)], isDef:Boolean = true): Unit = {
1134+
def printTargsDefs(targs: List[(TypeDef, TypeDef)], isDef:Boolean = true)(implicit elideThis: Option[Symbol]): Unit = {
11351135
if (!targs.isEmpty) {
11361136
def printSeparated(list: List[(TypeDef, TypeDef)]): Unit = list match {
11371137
case Nil =>
@@ -1146,7 +1146,7 @@ trait Printers
11461146
}
11471147
}
11481148

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

11521152
if (isDef) {
@@ -1196,7 +1196,7 @@ trait Printers
11961196
}
11971197
}
11981198

1199-
def printArgsDefs(args: List[ValDef]): Unit = inParens {
1199+
def printArgsDefs(args: List[ValDef])(implicit elideThis: Option[Symbol]): Unit = inParens {
12001200
args match {
12011201
case Nil =>
12021202
case arg :: _ =>
@@ -1216,7 +1216,7 @@ trait Printers
12161216
printSeparated(args)
12171217
}
12181218

1219-
def printAnnotations(trees: List[Term]): Buffer = {
1219+
def printAnnotations(trees: List[Term])(implicit elideThis: Option[Symbol]): Buffer = {
12201220
def printSeparated(list: List[Term]): Unit = list match {
12211221
case Nil =>
12221222
case x :: Nil => printAnnotation(x)
@@ -1229,7 +1229,7 @@ trait Printers
12291229
this
12301230
}
12311231

1232-
def printParamDef(arg: ValDef): Unit = {
1232+
def printParamDef(arg: ValDef)(implicit elideThis: Option[Symbol]): Unit = {
12331233
val name = arg.name
12341234
arg.symbol.owner match {
12351235
case IsDefSymbol(sym) if sym.name == "<init>" =>
@@ -1267,7 +1267,7 @@ trait Printers
12671267
indented {
12681268
caseDef.rhs match {
12691269
case Term.Block(stats, expr) =>
1270-
printStats(stats, expr)
1270+
printStats(stats, expr)(None)
12711271
case body =>
12721272
this += lineBreak()
12731273
printTree(body)
@@ -1341,7 +1341,7 @@ trait Printers
13411341
this += highlightLiteral("'" + v.name, color)
13421342
}
13431343

1344-
def printTypeOrBoundsTree(tpt: TypeOrBoundsTree): Buffer = tpt match {
1344+
def printTypeOrBoundsTree(tpt: TypeOrBoundsTree)(implicit elideThis: Option[Symbol] = None): Buffer = tpt match {
13451345
case TypeBoundsTree(lo, hi) =>
13461346
this += "_ >: "
13471347
printTypeTree(lo)
@@ -1353,7 +1353,15 @@ trait Printers
13531353
printTypeTree(tpt)
13541354
}
13551355

1356-
def printTypeTree(tree: TypeTree): Buffer = tree match {
1356+
/** Print type tree
1357+
*
1358+
* @param elideThis Shoud printing elide `C.this` for the given class `C`?
1359+
* None means no eliding.
1360+
*
1361+
* Self type annotation and types in parent list should elide current class
1362+
* prefix `C.this` to avoid type checking errors.
1363+
*/
1364+
def printTypeTree(tree: TypeTree)(implicit elideThis: Option[Symbol] = None): Buffer = tree match {
13571365
case TypeTree.Inferred() =>
13581366
// TODO try to move this logic into `printType`
13591367
def printTypeAndAnnots(tpe: Type): Buffer = tpe match {
@@ -1449,7 +1457,7 @@ trait Printers
14491457

14501458
}
14511459

1452-
def printTypeOrBound(tpe: TypeOrBounds): Buffer = tpe match {
1460+
def printTypeOrBound(tpe: TypeOrBounds)(implicit elideThis: Option[Symbol]): Buffer = tpe match {
14531461
case tpe@TypeBounds(lo, hi) =>
14541462
this += "_ >: "
14551463
printType(lo)
@@ -1458,7 +1466,15 @@ trait Printers
14581466
case IsType(tpe) => printType(tpe)
14591467
}
14601468

1461-
def printType(tpe: Type): Buffer = tpe match {
1469+
/** Print type
1470+
*
1471+
* @param elideThis Shoud printing elide `C.this` for the given class `C`?
1472+
* None means no eliding.
1473+
*
1474+
* Self type annotation and types in parent list should elide current class
1475+
* prefix `C.this` to avoid type checking errors.
1476+
*/
1477+
def printType(tpe: Type)(implicit elideThis: Option[Symbol] = None): Buffer = tpe match {
14621478
case Type.ConstantType(const) =>
14631479
printConstant(const)
14641480

@@ -1468,6 +1484,8 @@ trait Printers
14681484
case IsType(prefix @ Type.SymRef(IsClassSymbol(_), _)) =>
14691485
printType(prefix)
14701486
this += "#"
1487+
case IsType(Type.ThisType(Type.SymRef(cdef, _)))
1488+
if elideThis.nonEmpty && cdef == elideThis.get =>
14711489
case IsType(prefix) =>
14721490
printType(prefix)
14731491
this += "."
@@ -1600,7 +1618,7 @@ trait Printers
16001618
case PackageDef(name, _) => this += highlightTypeDef(name, color)
16011619
}
16021620

1603-
def printAnnotation(annot: Term): Buffer = {
1621+
def printAnnotation(annot: Term)(implicit elideThis: Option[Symbol]): Buffer = {
16041622
val Annotation(ref, args) = annot
16051623
this += "@"
16061624
printTypeTree(ref)
@@ -1610,7 +1628,7 @@ trait Printers
16101628
inParens(printTrees(args, ", "))
16111629
}
16121630

1613-
def printDefAnnotations(definition: Definition): Buffer = {
1631+
def printDefAnnotations(definition: Definition)(implicit elideThis: Option[Symbol]): Buffer = {
16141632
val annots = definition.symbol.annots.filter {
16151633
case Annotation(annot, _) =>
16161634
annot.tpe match {
@@ -1625,7 +1643,7 @@ trait Printers
16251643
else this
16261644
}
16271645

1628-
def printRefinement(tpe: Type): Buffer = {
1646+
def printRefinement(tpe: Type)(implicit elideThis: Option[Symbol]): Buffer = {
16291647
def printMethodicType(tp: TypeOrBounds): Unit = tp match {
16301648
case tp @ Type.MethodType(paramNames, params, res) =>
16311649
inParens(printMethodicTypeParams(paramNames, params))
@@ -1665,7 +1683,7 @@ trait Printers
16651683
this += lineBreak() += "}"
16661684
}
16671685

1668-
def printMethodicTypeParams(paramNames: List[String], params: List[TypeOrBounds]): Unit = {
1686+
def printMethodicTypeParams(paramNames: List[String], params: List[TypeOrBounds])(implicit elideThis: Option[Symbol]): Unit = {
16691687
def printInfo(info: TypeOrBounds) = info match {
16701688
case IsTypeBounds(info) => printBounds(info)
16711689
case IsType(info) =>
@@ -1686,7 +1704,7 @@ trait Printers
16861704
printSeparated(paramNames.zip(params))
16871705
}
16881706

1689-
def printBoundsTree(bounds: TypeBoundsTree): Buffer = {
1707+
def printBoundsTree(bounds: TypeBoundsTree)(implicit elideThis: Option[Symbol]): Buffer = {
16901708
bounds.low match {
16911709
case TypeTree.Inferred() =>
16921710
case low =>
@@ -1701,7 +1719,7 @@ trait Printers
17011719
}
17021720
}
17031721

1704-
def printBounds(bounds: TypeBounds): Buffer = {
1722+
def printBounds(bounds: TypeBounds)(implicit elideThis: Option[Symbol]): Buffer = {
17051723
this += " >: "
17061724
printType(bounds.low)
17071725
this += " <: "

0 commit comments

Comments
 (0)