Skip to content

Commit 1a7eade

Browse files
committed
Fix printing of secondary constructors
1 parent 95ec081 commit 1a7eade

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,17 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
222222
if (flags.isInline) this += "inline "
223223
if (flags.isOverride) this += "override "
224224

225-
this += "def " += name
225+
val isConstructor = name == "<init>"
226+
227+
this += "def " += (if (isConstructor) "this" else name)
226228
printTargsDefs(targs)
227229
val it = argss.iterator
228230
while (it.hasNext)
229231
printArgsDefs(it.next())
230-
this += ": "
231-
printTypeTree(tpt)
232+
if (!isConstructor) {
233+
this += ": "
234+
printTypeTree(tpt)
235+
}
232236
rhs match {
233237
case Some(tree) =>
234238
this += " = "
@@ -265,7 +269,10 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
265269
printTree(expr)
266270

267271
case Term.Apply(fn, args) =>
268-
printTree(fn)
272+
fn match {
273+
case Term.Select(Term.This(_), "<init>", _) => this += "this" // call to constructor inside a constructor
274+
case _ => printTree(fn)
275+
}
269276
this += "("
270277
printTrees(args, ", ")
271278
this += ")"

tests/pos/t116.decompiled

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** Decompiled from out/posTestFromTasty/pos/t116/C.class */
2+
class C() {
3+
def this(x: scala.Int) = {
4+
this()
5+
class D() extends C()
6+
()
7+
}
8+
}

0 commit comments

Comments
 (0)