Skip to content

Commit 8b018eb

Browse files
committed
Only elide this in self type annotation
1 parent 788d015 commit 8b018eb

File tree

1 file changed

+62
-47
lines changed

1 file changed

+62
-47
lines changed

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

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
@@ -1037,7 +1037,7 @@ trait Printers
10371037
printList(trees, sep, printTree)
10381038

10391039
def printTypeTrees(trees: List[TypeTree], sep: String): Buffer =
1040-
printList(trees, sep, printTypeTree)
1040+
printList(trees, sep, (t: TypeTree) => printTypeTree(t))
10411041

10421042
def printTypes(trees: List[Type], sep: String): Buffer = {
10431043
def printSeparated(list: List[Type]): Unit = list match {
@@ -1118,12 +1118,12 @@ trait Printers
11181118
this
11191119
}
11201120

1121-
def printTypesOrBounds(types: List[TypeOrBounds], sep: String): Buffer = {
1121+
def printTypesOrBounds(types: List[TypeOrBounds], sep: String, selfIn: Option[Symbol] = None): Buffer = {
11221122
def printSeparated(list: List[TypeOrBounds]): Unit = list match {
11231123
case Nil =>
1124-
case x :: Nil => printTypeOrBound(x)
1124+
case x :: Nil => printTypeOrBound(x, selfIn)
11251125
case x :: xs =>
1126-
printTypeOrBound(x)
1126+
printTypeOrBound(x, selfIn)
11271127
this += sep
11281128
printSeparated(xs)
11291129
}
@@ -1341,19 +1341,26 @@ trait Printers
13411341
this += highlightLiteral("'" + v.name, color)
13421342
}
13431343

1344-
def printTypeOrBoundsTree(tpt: TypeOrBoundsTree): Buffer = tpt match {
1344+
def printTypeOrBoundsTree(tpt: TypeOrBoundsTree, selfIn: Option[Symbol] = None): Buffer = tpt match {
13451345
case TypeBoundsTree(lo, hi) =>
13461346
this += "_ >: "
1347-
printTypeTree(lo)
1347+
printTypeTree(lo, selfIn)
13481348
this += " <: "
1349-
printTypeTree(hi)
1349+
printTypeTree(hi, selfIn)
13501350
case tpt @ WildcardTypeTree() =>
1351-
printTypeOrBound(tpt.tpe)
1351+
printTypeOrBound(tpt.tpe, selfIn)
13521352
case IsTypeTree(tpt) =>
1353-
printTypeTree(tpt)
1353+
printTypeTree(tpt, selfIn)
13541354
}
13551355

1356-
def printTypeTree(tree: TypeTree): Buffer = tree match {
1356+
/** Print type tree
1357+
*
1358+
* @param selfIn The current type is a self type in the indicated class.
1359+
* NoSymbol means the current type is not a self type annotation.
1360+
* Self type annotation should elide current class prefix to avoid
1361+
* cyclic type checking error.
1362+
*/
1363+
def printTypeTree(tree: TypeTree, selfIn: Option[Symbol] = None): Buffer = tree match {
13571364
case TypeTree.Inferred() =>
13581365
// TODO try to move this logic into `printType`
13591366
def printTypeAndAnnots(tpe: Type): Buffer = tpe match {
@@ -1363,25 +1370,25 @@ trait Printers
13631370
printAnnotation(annot)
13641371
case Type.SymRef(IsClassSymbol(sym), _) if sym.fullName == "scala.runtime.Null$" || sym.fullName == "scala.runtime.Nothing$" =>
13651372
// scala.runtime.Null$ and scala.runtime.Nothing$ are not modules, those are their actual names
1366-
printType(tpe)
1373+
printType(tpe, selfIn)
13671374
case tpe @ Type.SymRef(IsClassSymbol(sym), _) if sym.name.endsWith("$") =>
1368-
printType(tpe)
1375+
printType(tpe, selfIn)
13691376
this += ".type"
13701377
case tpe @ Type.SymRef(sym, _) if sym.isTerm =>
1371-
printType(tpe)
1378+
printType(tpe, selfIn)
13721379
this += ".type"
1373-
case tpe => printType(tpe)
1380+
case tpe => printType(tpe, selfIn)
13741381
}
13751382
printTypeAndAnnots(tree.tpe)
13761383

13771384
case TypeTree.Ident(name) =>
1378-
printType(tree.tpe)
1385+
printType(tree.tpe, selfIn)
13791386

13801387
case TypeTree.Select(qual, name) =>
13811388
printTree(qual) += "." += highlightTypeDef(name, color)
13821389

13831390
case TypeTree.Projection(qual, name) =>
1384-
printTypeTree(qual) += "#" += highlightTypeDef(name, color)
1391+
printTypeTree(qual, selfIn) += "#" += highlightTypeDef(name, color)
13851392

13861393
case TypeTree.Singleton(ref) =>
13871394
printTree(ref)
@@ -1391,35 +1398,35 @@ trait Printers
13911398
}
13921399

13931400
case TypeTree.Refined(tpt, refinements) =>
1394-
printTypeTree(tpt)
1401+
printTypeTree(tpt, selfIn)
13951402
inBlock(printTrees(refinements, "; "))
13961403

13971404
case TypeTree.Applied(tpt, args) =>
1398-
printTypeTree(tpt)
1405+
printTypeTree(tpt, selfIn)
13991406
inSquare(printTypeOrBoundsTrees(args, ", "))
14001407

14011408
case TypeTree.Annotated(tpt, annot) =>
14021409
val Annotation(ref, args) = annot
14031410
ref.tpe match {
14041411
case Types.RepeatedAnnotation() =>
14051412
val Types.Sequence(tp) = tpt.tpe
1406-
printType(tp)
1413+
printType(tp, selfIn)
14071414
this += highlightTypeDef("*", color)
14081415
case _ =>
1409-
printTypeTree(tpt)
1416+
printTypeTree(tpt, selfIn)
14101417
this += " "
14111418
printAnnotation(annot)
14121419
}
14131420

14141421
case TypeTree.And(left, right) =>
1415-
printTypeTree(left)
1422+
printTypeTree(left, selfIn)
14161423
this += highlightTypeDef(" & ", color)
1417-
printTypeTree(right)
1424+
printTypeTree(right, selfIn)
14181425

14191426
case TypeTree.Or(left, right) =>
1420-
printTypeTree(left)
1427+
printTypeTree(left, selfIn)
14211428
this += highlightTypeDef(" | ", color)
1422-
printTypeTree(right)
1429+
printTypeTree(right, selfIn)
14231430

14241431
case TypeTree.MatchType(bound, selector, cases) =>
14251432
printTypeTree(selector)
@@ -1428,48 +1435,56 @@ trait Printers
14281435

14291436
case TypeTree.ByName(result) =>
14301437
this += highlightTypeDef("=> ", color)
1431-
printTypeTree(result)
1438+
printTypeTree(result, selfIn)
14321439

14331440
case TypeTree.LambdaTypeTree(tparams, body) =>
14341441
printTargsDefs(tparams.zip(tparams), isDef = false)
14351442
this += highlightTypeDef(" => ", color)
1436-
printTypeOrBoundsTree(body)
1443+
printTypeOrBoundsTree(body, selfIn)
14371444

14381445
case TypeTree.TypeBind(name, _) =>
14391446
this += highlightTypeDef(name, color)
14401447

14411448
case TypeTree.TypeBlock(aliases, tpt) =>
14421449
inBlock {
14431450
printTrees(aliases, lineBreak())
1444-
printTypeTree(tpt)
1451+
printTypeTree(tpt, selfIn)
14451452
}
14461453

14471454
case _ =>
14481455
throw new MatchError(tree.show)
14491456

14501457
}
14511458

1452-
def printTypeOrBound(tpe: TypeOrBounds): Buffer = tpe match {
1459+
def printTypeOrBound(tpe: TypeOrBounds, selfIn: Option[Symbol] = None): Buffer = tpe match {
14531460
case tpe@TypeBounds(lo, hi) =>
14541461
this += "_ >: "
1455-
printType(lo)
1462+
printType(lo, selfIn)
14561463
this += " <: "
1457-
printType(hi)
1458-
case IsType(tpe) => printType(tpe)
1464+
printType(hi, selfIn)
1465+
case IsType(tpe) => printType(tpe, selfIn)
14591466
}
14601467

1461-
def printType(tpe: Type): Buffer = tpe match {
1468+
/** Print type
1469+
*
1470+
* @param selfIn The current type is a self type in the indicated class.
1471+
* Self type annotation should elide current class prefix to avoid
1472+
* cyclic type checking error.
1473+
*/
1474+
def printType(tpe: Type, selfIn: Option[Symbol] = None): Buffer = tpe match {
14621475
case Type.ConstantType(const) =>
14631476
printConstant(const)
14641477

14651478
case Type.SymRef(sym, prefix) if sym.isType =>
14661479
prefix match {
14671480
case Types.EmptyPrefix() =>
14681481
case IsType(prefix @ Type.SymRef(IsClassSymbol(_), _)) =>
1469-
printType(prefix)
1482+
printType(prefix, selfIn)
14701483
this += "#"
1484+
case IsType(Type.ThisType(Type.SymRef(cdef, _)))
1485+
if selfIn.nonEmpty && cdef == selfIn.get =>
14711486
case IsType(prefix) =>
1472-
printType(prefix)
1487+
printType(prefix, selfIn)
14731488
this += "."
14741489
}
14751490
this += highlightTypeDef(sym.name.stripSuffix("$"), color)
@@ -1479,7 +1494,7 @@ trait Printers
14791494
case Types.EmptyPrefix() =>
14801495
this += highlightTypeDef(sym.name, color)
14811496
case _ =>
1482-
printTypeOrBound(prefix)
1497+
printTypeOrBound(prefix, selfIn)
14831498
if (sym.name != "package")
14841499
this += "." += highlightTypeDef(sym.name, color)
14851500
this
@@ -1490,7 +1505,7 @@ trait Printers
14901505
case Type.ThisType(Types.EmptyPackage()) =>
14911506
this += highlightTypeDef(name, color)
14921507
case IsType(prefix) =>
1493-
printType(prefix)
1508+
printType(prefix, selfIn)
14941509
if (name != "package")
14951510
this += "." += highlightTypeDef(name, color)
14961511
this
@@ -1501,7 +1516,7 @@ trait Printers
15011516
case Type.TypeRef(name, prefix) =>
15021517
prefix match {
15031518
case NoPrefix() | Type.ThisType(Types.EmptyPackage()) =>
1504-
case IsType(prefix) => printType(prefix) += "."
1519+
case IsType(prefix) => printType(prefix, selfIn) += "."
15051520
}
15061521
if (name.endsWith("$")) this += highlightTypeDef(name.stripSuffix("$"), color) += ".type"
15071522
else this += highlightTypeDef(name, color)
@@ -1514,25 +1529,25 @@ trait Printers
15141529
case Type.TypeRef("<repeated>", Types.ScalaPackage()) =>
15151530
this += "_*"
15161531
case _ =>
1517-
printType(tp)
1532+
printType(tp, selfIn)
15181533
inSquare(printTypesOrBounds(args, ", "))
15191534
}
15201535

15211536
case Type.AnnotatedType(tp, annot) =>
15221537
val Annotation(ref, args) = annot
1523-
printType(tp)
1538+
printType(tp, selfIn)
15241539
this += " "
15251540
printAnnotation(annot)
15261541

15271542
case Type.AndType(left, right) =>
1528-
printType(left)
1543+
printType(left, selfIn)
15291544
this += highlightTypeDef(" & ", color)
1530-
printType(right)
1545+
printType(right, selfIn)
15311546

15321547
case Type.OrType(left, right) =>
1533-
printType(left)
1548+
printType(left, selfIn)
15341549
this += highlightTypeDef(" | ", color)
1535-
printType(right)
1550+
printType(right, selfIn)
15361551

15371552
case Type.MatchType(bound, scrutinee, cases) =>
15381553
printType(scrutinee)
@@ -1561,13 +1576,13 @@ trait Printers
15611576
}
15621577

15631578
case Type.SuperType(thistpe, supertpe) =>
1564-
printType(supertpe)
1579+
printType(supertpe, selfIn)
15651580
this += highlightTypeDef(".super", color)
15661581

15671582
case Type.TypeLambda(paramNames, tparams, body) =>
15681583
inSquare(printMethodicTypeParams(paramNames, tparams))
15691584
this += highlightTypeDef(" => ", color)
1570-
printTypeOrBound(body)
1585+
printTypeOrBound(body, selfIn)
15711586

15721587
case Type.ParamRef(lambda, idx) =>
15731588
lambda match {
@@ -1577,7 +1592,7 @@ trait Printers
15771592
}
15781593

15791594
case Type.RecursiveType(tpe) =>
1580-
printType(tpe)
1595+
printType(tpe, selfIn)
15811596

15821597
case Type.RecursiveThis(_) =>
15831598
this += highlightTypeDef("this", color)

0 commit comments

Comments
 (0)