From 53ce976a30620ba8c145c4f423c66b6e235c0d39 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 20 Jun 2018 13:58:43 +0200 Subject: [PATCH] Print this id and filter object lazy val --- .../src/scala/tasty/util/ShowSourceCode.scala | 15 ++++++--- tests/pos/i2104b.decompiled | 10 +++--- tests/pos/simpleCaseClass-1.decompiled | 4 +-- tests/pos/simpleCaseClass-2.decompiled | 8 ++--- tests/pos/simpleCaseClass-3.decompiled | 8 ++--- tests/pos/simpleMatchRef.decompiled | 2 +- tests/pos/t704.decompiled | 32 +++++++++++++++++++ 7 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 tests/pos/t704.decompiled diff --git a/library/src/scala/tasty/util/ShowSourceCode.scala b/library/src/scala/tasty/util/ShowSourceCode.scala index 629dcd66879b..b9f613c69716 100644 --- a/library/src/scala/tasty/util/ShowSourceCode.scala +++ b/library/src/scala/tasty/util/ShowSourceCode.scala @@ -275,7 +275,13 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty printConstant(const) case Term.This(id) => - this += "this" // TODO add id + id match { + case Some(x) => + val Id(name) = x + this += name.stripSuffix("$") += "." + case None => + } + this += "this" case Term.New(tpt) => this += "new " @@ -343,12 +349,13 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty printTree(rhs) case Term.Block(stats0, expr) => - def isLoopEntryPoint(tree: Tree): Boolean = tree match { + def shouldNotBePrinted(tree: Tree): Boolean = tree match { case Term.Apply(Term.Ident("while$" | "doWhile$"), _) => true + case tree @ ValDef(_, _, _) => tree.flags.isObject case _ => false } - val stats = stats0.filterNot(isLoopEntryPoint) + val stats = stats0.filterNot(shouldNotBePrinted) expr match { case Term.Lambda(_, _) => @@ -362,7 +369,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty case _ => this += "{" val (stats1, expr1) = - if (isLoopEntryPoint(expr)) (stats.init, stats.last) + if (shouldNotBePrinted(expr)) (stats.init, stats.last) else (stats, expr) indented { printStats(stats1, expr1) diff --git a/tests/pos/i2104b.decompiled b/tests/pos/i2104b.decompiled index ead2eb68cd7c..00b2461d1e69 100644 --- a/tests/pos/i2104b.decompiled +++ b/tests/pos/i2104b.decompiled @@ -12,21 +12,21 @@ case class Pair[A, B](_1: A, _2: B) { acc = scala.runtime.Statics.mix(acc, scala.runtime.Statics.anyHash(Pair.this._2)) scala.runtime.Statics.finalizeHash(acc, 2) } - override def equals(x$0: scala.Any): scala.Boolean = this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match { + override def equals(x$0: scala.Any): scala.Boolean = Pair.this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match { case x$0: Pair[A, B] @scala.unchecked() => - this._1.==(x$0._1).&&(this._2.==(x$0._2)) + Pair.this._1.==(x$0._1).&&(Pair.this._2.==(x$0._2)) case _ => false }) - override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(this) + override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(Pair.this) override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[Pair[A, B] @scala.unchecked()] override def productArity: scala.Int = 2 override def productPrefix: java.lang.String = "Pair" override def productElement(n: scala.Int): scala.Any = n match { case 0 => - this._1 + Pair.this._1 case 1 => - this._2 + Pair.this._2 case _ => throw new java.lang.IndexOutOfBoundsException(n.toString()) } diff --git a/tests/pos/simpleCaseClass-1.decompiled b/tests/pos/simpleCaseClass-1.decompiled index 70907f274884..deed4f778eb3 100644 --- a/tests/pos/simpleCaseClass-1.decompiled +++ b/tests/pos/simpleCaseClass-1.decompiled @@ -1,13 +1,13 @@ /** Decompiled from out/posTestFromTasty/pos/simpleCaseClass-1/A.class */ case class A() { override def hashCode(): scala.Int = 1914112431 - override def equals(x$0: scala.Any): scala.Boolean = this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match { + override def equals(x$0: scala.Any): scala.Boolean = A.this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match { case x$0: A @scala.unchecked() => true case _ => false }) - override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(this) + override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(A.this) override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[A @scala.unchecked()] override def productArity: scala.Int = 0 override def productPrefix: java.lang.String = "A" diff --git a/tests/pos/simpleCaseClass-2.decompiled b/tests/pos/simpleCaseClass-2.decompiled index 52f88b666d72..4156a6447b10 100644 --- a/tests/pos/simpleCaseClass-2.decompiled +++ b/tests/pos/simpleCaseClass-2.decompiled @@ -5,19 +5,19 @@ case class A(x: scala.Int) { acc = scala.runtime.Statics.mix(acc, A.this.x) scala.runtime.Statics.finalizeHash(acc, 1) } - override def equals(x$0: scala.Any): scala.Boolean = this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match { + override def equals(x$0: scala.Any): scala.Boolean = A.this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match { case x$0: A @scala.unchecked() => - this.x.==(x$0.x) + A.this.x.==(x$0.x) case _ => false }) - override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(this) + override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(A.this) override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[A @scala.unchecked()] override def productArity: scala.Int = 1 override def productPrefix: java.lang.String = "A" override def productElement(n: scala.Int): scala.Any = n match { case 0 => - this._1 + A.this._1 case _ => throw new java.lang.IndexOutOfBoundsException(n.toString()) } diff --git a/tests/pos/simpleCaseClass-3.decompiled b/tests/pos/simpleCaseClass-3.decompiled index cf6fe29cf6c5..1dfef3b7f431 100644 --- a/tests/pos/simpleCaseClass-3.decompiled +++ b/tests/pos/simpleCaseClass-3.decompiled @@ -5,19 +5,19 @@ case class A[T](x: T) { acc = scala.runtime.Statics.mix(acc, scala.runtime.Statics.anyHash(A.this.x)) scala.runtime.Statics.finalizeHash(acc, 1) } - override def equals(x$0: scala.Any): scala.Boolean = this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match { + override def equals(x$0: scala.Any): scala.Boolean = A.this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match { case x$0: A[T] @scala.unchecked() => - this.x.==(x$0.x) + A.this.x.==(x$0.x) case _ => false }) - override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(this) + override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(A.this) override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[A[T] @scala.unchecked()] override def productArity: scala.Int = 1 override def productPrefix: java.lang.String = "A" override def productElement(n: scala.Int): scala.Any = n match { case 0 => - this._1 + A.this._1 case _ => throw new java.lang.IndexOutOfBoundsException(n.toString()) } diff --git a/tests/pos/simpleMatchRef.decompiled b/tests/pos/simpleMatchRef.decompiled index 3f07fe83bc65..ffbd69186c53 100644 --- a/tests/pos/simpleMatchRef.decompiled +++ b/tests/pos/simpleMatchRef.decompiled @@ -2,7 +2,7 @@ class Foo() { val X: scala.Int = scala.Predef.??? def foo(x: scala.Any): scala.Unit = x match { - case this.X => + case Foo.this.X => scala.Predef.println("a") case Y => scala.Predef.println("b") diff --git a/tests/pos/t704.decompiled b/tests/pos/t704.decompiled new file mode 100644 index 000000000000..64f50b9e9d27 --- /dev/null +++ b/tests/pos/t704.decompiled @@ -0,0 +1,32 @@ +/** Decompiled from out/posTestFromTasty/pos/t704/C.class */ +class C() extends E +/** Decompiled from out/posTestFromTasty/pos/t704/D.class */ +trait D() extends java.lang.Object { + val x: java.lang.String = "xxxx should appear twice" + object xxxx { + scala.Console.println(D.this.x) + } + def get_xxxx: scala.AnyRef = D.this.xxxx +} +/** Decompiled from out/posTestFromTasty/pos/t704/E.class */ +trait E() extends java.lang.Object with D { + def f(): scala.Unit = { + val y: java.lang.String = "yyyy should appear twice" + object yyyy { + val x1: scala.AnyRef = E.this.get_xxxx + scala.Console.println(y) + } + + { + yyyy + () + } + } +} +/** Decompiled from out/posTestFromTasty/pos/t704/Go.class */ +object Go extends D { + def main(args: scala.Array[scala.Predef.String]): scala.Unit = { + new C().f() + new C().f() + } +}