Skip to content

Commit 4725fe5

Browse files
Merge pull request #4699 from dotty-staging/decompile-fix-super
Fix printing super
2 parents 0ec70ac + 33a2b9c commit 4725fe5

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-7
lines changed

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,17 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
321321
this += "]"
322322
}
323323

324-
case Term.Super(qual, tptOpt) =>
325-
printTree(qual)
326-
this += ".super"
327-
// TODO use tptOpt?
324+
case Term.Super(qual, idOpt) =>
325+
qual match {
326+
case Term.This(Some(Id(name))) => this += name += "."
327+
case Term.This(None) =>
328+
}
329+
this += "super"
330+
for (id <- idOpt) {
331+
val Id(name) = id
332+
this += "[" += name += "]"
333+
}
334+
this
328335

329336
case Term.Typed(term, tpt) =>
330337
tpt.tpe match {
@@ -442,13 +449,15 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
442449
}
443450

444451
def printStats(stats: List[Tree], expr: Tree): Unit = {
445-
def printSeparator(nextStats: List[Tree]) = {
452+
def printSeparator(next: Tree): Unit = {
446453
// Avoid accidental application of opening `{` on next line with a double break
447-
val next = if (nextStats.isEmpty) expr else nextStats.head
448454
next match {
449455
case Term.Block(DefDef("while$" | "doWhile$", _, _, _, _) :: Nil, _) => this += lineBreak()
450456
case Term.Block(_, _) => this += doubleLineBreak()
451457
case Term.Inlined(_, _, _) => this += doubleLineBreak()
458+
case Term.Select(qual, _, _) => printSeparator(qual)
459+
case Term.Apply(fn, _) => printSeparator(fn)
460+
case Term.TypeApply(fn, _) => printSeparator(fn)
452461
case _ => this += lineBreak()
453462
}
454463
}
@@ -457,7 +466,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
457466
printTree(expr)
458467
case x :: xs =>
459468
printTree(x)
460-
printSeparator(xs)
469+
printSeparator(if (xs.isEmpty) expr else xs.head)
461470
printSeparated(xs)
462471
}
463472

tests/pos/simpleSuper.decompiled

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/** Decompiled from out/posTestFromTasty/pos/simpleSuper/foo/A.class */
2+
package foo {
3+
class A() {
4+
def foo: scala.Int = 1
5+
}
6+
}
7+
/** Decompiled from out/posTestFromTasty/pos/simpleSuper/foo/B.class */
8+
package foo {
9+
trait B() extends java.lang.Object {
10+
def foo: scala.Int = 2
11+
}
12+
}
13+
/** Decompiled from out/posTestFromTasty/pos/simpleSuper/foo/C.class */
14+
package foo {
15+
class C() extends foo.A() with foo.B {
16+
override def foo: scala.Int = super.foo.+(super[B].foo)
17+
}
18+
}

tests/pos/simpleSuper.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package foo
2+
3+
class A {
4+
def foo: Int = 1
5+
}
6+
7+
trait B {
8+
def foo: Int = 2
9+
}
10+
11+
class C extends A with B {
12+
override def foo: Int = super.foo + super[B].foo
13+
}

tests/run/t4300.decompiled

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/** Decompiled from out/runTestFromTasty/run/t4300/A.class */
2+
trait A() extends java.lang.Object {
3+
def f(): scala.Unit = scala.Predef.println("A")
4+
}
5+
/** Decompiled from out/runTestFromTasty/run/t4300/B.class */
6+
class B() extends A {
7+
def b(): scala.Unit = super[A].f()
8+
trait C() extends java.lang.Object {
9+
def c(): scala.Unit = B.super[A].f()
10+
}
11+
def g(): scala.Unit = scala.Predef.intWrapper(0).until(1).foreach[scala.Unit](((i: scala.Int) => super[A].f()))
12+
def h(): scala.Unit = scala.Predef.intWrapper(0).until(1).foreach[scala.Unit](((i: scala.Int) => B.super[A].f()))
13+
override def f(): scala.Unit = scala.Predef.println("B")
14+
}
15+
/** Decompiled from out/runTestFromTasty/run/t4300/Test.class */
16+
object Test {
17+
def main(args: scala.Array[scala.Predef.String]): scala.Unit = {
18+
val b: B = new B()
19+
b.b()
20+
21+
{
22+
final class $anon() extends b.C()
23+
(new $anon(): b.C)
24+
}.c()
25+
b.g()
26+
b.h()
27+
}
28+
}

0 commit comments

Comments
 (0)