Skip to content

Commit 9d73a09

Browse files
Merge pull request #4696 from dotty-staging/print-self-types
Print self types
2 parents 553e81b + b12285e commit 9d73a09

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

library/src/scala/tasty/util/ShowSourceCode.scala

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,34 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
146146
case stat@Import(_, _) => stat
147147
case stat@Term() => stat
148148
}
149-
if (stats1.nonEmpty) {
149+
150+
def printBody(printSelf: Boolean) = {
150151
this += " {"
151152
indented {
153+
if (printSelf) {
154+
val Some(ValDef(name, tpt, _)) = self
155+
indented {
156+
val name1 = if (name == "_") "this" else name
157+
this += " " += name1 += ": "
158+
printTypeTree(tpt)
159+
this += " =>"
160+
}
161+
}
152162
this += lineBreak()
153163
printTrees(stats1, lineBreak())
154164
}
155165
this += lineBreak() += "}"
156166
}
167+
self match {
168+
case Some(ValDef(_, TypeTree.Singleton(_), _)) =>
169+
if (stats1.nonEmpty)
170+
printBody(printSelf = false)
171+
case Some(ValDef(_, _, _)) =>
172+
printBody(printSelf = true)
173+
case _ =>
174+
if (stats1.nonEmpty)
175+
printBody(printSelf = false)
176+
}
157177
this
158178

159179
case tdef @ TypeDef(name, rhs) =>
@@ -875,10 +895,18 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
875895
printType(tp)
876896

877897
case Type.ThisType(tp) =>
878-
printType(tp)
879898
tp match {
880-
case Type.SymRef(cdef @ ClassDef(_, _, _, _, _), _) if !cdef.flags.isObject => this += ".this"
881-
case _ => this
899+
case Type.SymRef(cdef @ ClassDef(name, _, _, _, _), prefix) if !cdef.flags.isObject =>
900+
def printPrefix(prefix: TypeOrBounds): Unit = prefix match {
901+
case Type.SymRef(ClassDef(name, _, _, _, _), prefix2) =>
902+
printPrefix(prefix2)
903+
this += name += "."
904+
case _ =>
905+
}
906+
printPrefix(prefix)
907+
this += name
908+
this += ".this"
909+
case _ => printType(tp)
882910
}
883911

884912
case Type.TypeLambda(paramNames, tparams, body) =>

tests/pos/selftypes.decompiled

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/** Decompiled from out/posTestFromTasty/pos/selftypes/selftypes.class */
2+
object selftypes {
3+
trait A() extends java.lang.Object { self: selftypes.AB =>
4+
type AA = scala.List[this.BX]
5+
class AX()
6+
}
7+
trait B() extends java.lang.Object { self: selftypes.AB =>
8+
type BB = B.this.AA
9+
class BX()
10+
}
11+
class AB() extends selftypes.A with selftypes.B
12+
}

0 commit comments

Comments
 (0)