Skip to content

Fix printing of secondary constructors #4695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions library/src/scala/tasty/util/ShowSourceCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,17 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
if (flags.isInline) this += "inline "
if (flags.isOverride) this += "override "

this += "def " += name
val isConstructor = name == "<init>"

this += "def " += (if (isConstructor) "this" else name)
printTargsDefs(targs)
val it = argss.iterator
while (it.hasNext)
printArgsDefs(it.next())
this += ": "
printTypeTree(tpt)
if (!isConstructor) {
this += ": "
printTypeTree(tpt)
}
rhs match {
case Some(tree) =>
this += " = "
Expand Down Expand Up @@ -265,7 +269,10 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
printTree(expr)

case Term.Apply(fn, args) =>
printTree(fn)
fn match {
case Term.Select(Term.This(_), "<init>", _) => this += "this" // call to constructor inside a constructor
case _ => printTree(fn)
}
this += "("
printTrees(args, ", ")
this += ")"
Expand Down
8 changes: 8 additions & 0 deletions tests/pos/t116.decompiled
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** Decompiled from out/posTestFromTasty/pos/t116/C.class */
class C() {
def this(x: scala.Int) = {
this()
class D() extends C()
()
}
}