Skip to content

Commit 553e81b

Browse files
Merge pull request #4695 from dotty-staging/fix-printing-second-constructors
Fix printing of secondary constructors
2 parents 6812241 + 1a7eade commit 553e81b

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
@@ -221,13 +221,17 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
221221
if (flags.isInline) this += "inline "
222222
if (flags.isOverride) this += "override "
223223

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

266270
case Term.Apply(fn, args) =>
267-
printTree(fn)
271+
fn match {
272+
case Term.Select(Term.This(_), "<init>", _) => this += "this" // call to constructor inside a constructor
273+
case _ => printTree(fn)
274+
}
268275
this += "("
269276
printTrees(args, ", ")
270277
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)