Skip to content

Decompiler: empty parens, type variance #5729

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 4 commits into from
Jan 30, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ trait SymbolOpsImpl extends scala.tasty.reflect.SymbolOps with CoreImpl {

def TypeSymbolDeco(symbol: TypeSymbol): TypeSymbolAPI = new TypeSymbolAPI {
def tree(implicit ctx: Context): TypeDef = FromSymbol.typeDefFromSym(symbol)

def isTypeParam(implicit ctx: Context): Boolean = symbol.isTypeParam
}

object ClassSymbol extends ClassSymbolModule {
Expand Down
47 changes: 33 additions & 14 deletions library/src/scala/tasty/reflect/Printers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,9 @@ trait Printers
else if (flags.is(Flags.Abstract)) this += highlightKeyword("abstract class ", color) += highlightTypeDef(name, color)
else this += highlightKeyword("class ", color) += highlightTypeDef(name, color)

val typeParams = stats.collect { case IsTypeDef(targ) => targ }.filter(_.symbol.isTypeParam).zip(targs)
if (!flags.is(Flags.Object)) {
printTargsDefs(targs)
printTargsDefs(typeParams)
val it = argss.iterator
while (it.hasNext)
printArgsDefs(it.next())
Expand All @@ -606,14 +607,19 @@ trait Printers
if (parents1.nonEmpty)
this += highlightKeyword(" extends ", color)

def printParent(parent: TermOrTypeTree): Unit = parent match {
def printParent(parent: TermOrTypeTree, needEmptyParens: Boolean = false): Unit = parent match {
case IsTypeTree(parent) =>
printTypeTree(parent)
case IsTerm(Term.TypeApply(fun, targs)) =>
printParent(fun)
case IsTerm(Term.Apply([email protected](_,_), args)) =>
printParent(fun, true)
if (!args.isEmpty || needEmptyParens)
inParens(printTrees(args, ", "))
case IsTerm(Term.Apply(fun, args)) =>
printParent(fun)
inParens(printTrees(args, ", "))
if (!args.isEmpty || needEmptyParens)
inParens(printTrees(args, ", "))
case IsTerm(Term.Select(Term.New(tpt), _)) =>
printTypeTree(tpt)
case IsTerm(parent) =>
Expand Down Expand Up @@ -691,7 +697,7 @@ trait Printers
case IsTypeDef(tdef @ TypeDef(name, rhs)) =>
printDefAnnotations(tdef)
this += highlightKeyword("type ", color)
printTargDef(tdef, isMember = true)
printTargDef((tdef, tdef), isMember = true)

case IsValDef(vdef @ ValDef(name, tpt, rhs)) =>
printDefAnnotations(vdef)
Expand Down Expand Up @@ -754,7 +760,7 @@ trait Printers
printProtectedOrPrivate(ddef)

this += highlightKeyword("def ", color) += highlightValDef((if (isConstructor) "this" else name), color)
printTargsDefs(targs)
printTargsDefs(targs.zip(targs))
val it = argss.iterator
while (it.hasNext)
printArgsDefs(it.next())
Expand Down Expand Up @@ -1125,13 +1131,13 @@ trait Printers
this
}

def printTargsDefs(targs: List[TypeDef]): Unit = {
def printTargsDefs(targs: List[(TypeDef, TypeDef)], isDef:Boolean = true): Unit = {
if (!targs.isEmpty) {
def printSeparated(list: List[TypeDef]): Unit = list match {
def printSeparated(list: List[(TypeDef, TypeDef)]): Unit = list match {
case Nil =>
case x :: Nil => printTargDef(x)
case x :: Nil => printTargDef(x, isDef = isDef)
case x :: xs =>
printTargDef(x)
printTargDef(x, isDef = isDef)
this += ", "
printSeparated(xs)
}
Expand All @@ -1140,9 +1146,19 @@ trait Printers
}
}

def printTargDef(arg: TypeDef, isMember: Boolean = false): Buffer = {
this += arg.name
arg.rhs match {
def printTargDef(arg: (TypeDef, TypeDef), isMember: Boolean = false, isDef:Boolean = true): Buffer = {
val (argDef, argCons) = arg

if (isDef) {
if (argDef.symbol.flags.is(Flags.Covariant)) {
this += highlightValDef("+", color)
} else if (argDef.symbol.flags.is(Flags.Contravariant)) {
this += highlightValDef("-", color)
}
}

this += argCons.name
argCons.rhs match {
case IsTypeBoundsTree(rhs) => printBoundsTree(rhs)
case rhs @ WildcardTypeTree() =>
printTypeOrBound(rhs.tpe)
Expand Down Expand Up @@ -1412,7 +1428,7 @@ trait Printers
printTypeTree(result)

case TypeTree.LambdaTypeTree(tparams, body) =>
printTargsDefs(tparams)
printTargsDefs(tparams.zip(tparams), isDef = false)
this += highlightTypeDef(" => ", color)
printTypeOrBoundsTree(body)

Expand Down Expand Up @@ -1576,7 +1592,10 @@ trait Printers
val Annotation(ref, args) = annot
this += "@"
printTypeTree(ref)
inParens(printTrees(args, ", "))
if (args.isEmpty)
this
else
inParens(printTrees(args, ", "))
}

def printDefAnnotations(definition: Definition): Buffer = {
Expand Down
2 changes: 2 additions & 0 deletions library/src/scala/tasty/reflect/SymbolOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ trait SymbolOps extends Core {
trait TypeSymbolAPI {
/** TypeDef tree of this defintion. */
def tree(implicit ctx: Context): TypeDef

def isTypeParam(implicit ctx: Context): Boolean
}
implicit def TypeSymbolDeco(symbol: TypeSymbol): TypeSymbolAPI

Expand Down
16 changes: 8 additions & 8 deletions tests/pos/i2104b.decompiled
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/** Decompiled from out/posTestFromTasty/pos/i2104b/Cons.class */
trait Cons[H, T]() extends java.lang.Object
/** Decompiled from out/posTestFromTasty/pos/i2104b/Cons.tasty */
trait Cons[+H, +T]() extends java.lang.Object
object Cons {
def apply[H, T](h: H, t: T): Cons[H, T] = scala.Predef.???
def unapply[H, T](t: Cons[H, T]): scala.Option[Pair[H, T]] = scala.Predef.???
}
/** Decompiled from out/posTestFromTasty/pos/i2104b/Pair.class */
/** Decompiled from out/posTestFromTasty/pos/i2104b/Pair.tasty */
case class Pair[A, B](_1: A, _2: B) {
override def hashCode(): scala.Int = {
var acc: scala.Int = 2479866
Expand All @@ -13,13 +13,13 @@ case class Pair[A, B](_1: A, _2: B) {
scala.runtime.Statics.finalizeHash(acc, 2)
}
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() =>
case x$0: Pair[A, B] @scala.unchecked =>
Pair.this._1.==(x$0._1).&&(Pair.this._2.==(x$0._2))
case _ =>
false
})
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 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 {
Expand All @@ -31,11 +31,11 @@ case class Pair[A, B](_1: A, _2: B) {
throw new java.lang.IndexOutOfBoundsException(n.toString())
}
}
object Pair extends scala.AnyRef()
/** Decompiled from out/posTestFromTasty/pos/i2104b/Test.class */
object Pair extends scala.AnyRef
/** Decompiled from out/posTestFromTasty/pos/i2104b/Test.tasty */
object Test {
def main(args: scala.Array[scala.Predef.String]): scala.Unit = Cons.apply[scala.Option[scala.Int], scala.None.type](scala.Option.apply[scala.Int](1), scala.None) match {
case Cons(scala.Some(i), scala.None) =>
()
}
}
}
12 changes: 6 additions & 6 deletions tests/pos/i4678.decompiled
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/** Decompiled from out/posTestFromTasty/pos/i4678/Foo.tasty */
class Foo() {
val x: scala.Int = (1: @annot1() @annot2() @annot3() @annot4() @annot5())
val x: scala.Int = (1: @annot1 @annot2 @annot3 @annot4 @annot5)
}
/** Decompiled from out/posTestFromTasty/pos/i4678/annot1.tasty */
class annot1() extends scala.annotation.Annotation()
class annot1() extends scala.annotation.Annotation
/** Decompiled from out/posTestFromTasty/pos/i4678/annot2.tasty */
class annot2() extends scala.annotation.Annotation()
class annot2() extends scala.annotation.Annotation
/** Decompiled from out/posTestFromTasty/pos/i4678/annot3.tasty */
class annot3() extends scala.annotation.Annotation()
class annot3() extends scala.annotation.Annotation
/** Decompiled from out/posTestFromTasty/pos/i4678/annot4.tasty */
class annot4() extends scala.annotation.Annotation()
class annot4() extends scala.annotation.Annotation
/** Decompiled from out/posTestFromTasty/pos/i4678/annot5.tasty */
class annot5() extends scala.annotation.Annotation()
class annot5() extends scala.annotation.Annotation
14 changes: 7 additions & 7 deletions tests/pos/simpleAnnot.decompiled
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/** Decompiled from out/posTestFromTasty/pos/simpleAnnot/Foo.class */
/** Decompiled from out/posTestFromTasty/pos/simpleAnnot/Foo.tasty */
class Foo() {
@annot() type A = scala.Int
@annot() val a: scala.Int = scala.Predef.???
val b: scala.Int @annot() = scala.Predef.???
def c(x: scala.Int): scala.Int = (x: @annot())
@annot type A = scala.Int
@annot val a: scala.Int = scala.Predef.???
val b: scala.Int @annot = scala.Predef.???
def c(x: scala.Int): scala.Int = (x: @annot)
}
/** Decompiled from out/posTestFromTasty/pos/simpleAnnot/annot.class */
class annot() extends scala.annotation.Annotation()
/** Decompiled from out/posTestFromTasty/pos/simpleAnnot/annot.tasty */
class annot() extends scala.annotation.Annotation
8 changes: 4 additions & 4 deletions tests/pos/simpleCaseClass-1.decompiled
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/** Decompiled from out/posTestFromTasty/pos/simpleCaseClass-1/A.class */
/** Decompiled from out/posTestFromTasty/pos/simpleCaseClass-1/A.tasty */
case class A() {
override def hashCode(): scala.Int = 1914112431
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() =>
case x$0: A @scala.unchecked =>
true
case _ =>
false
})
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 canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[A @scala.unchecked]
override def productArity: scala.Int = 0
override def productPrefix: java.lang.String = "A"
override def productElement(n: scala.Int): scala.Any = n match {
case _ =>
throw new java.lang.IndexOutOfBoundsException(n.toString())
}
}
object A extends scala.Function0[A]
object A extends scala.Function0[A]
8 changes: 4 additions & 4 deletions tests/pos/simpleCaseClass-2.decompiled
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/** Decompiled from out/posTestFromTasty/pos/simpleCaseClass-2/A.class */
/** Decompiled from out/posTestFromTasty/pos/simpleCaseClass-2/A.tasty */
case class A(x: scala.Int) {
override def hashCode(): scala.Int = {
var acc: scala.Int = 65
acc = scala.runtime.Statics.mix(acc, A.this.x)
scala.runtime.Statics.finalizeHash(acc, 1)
}
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() =>
case x$0: A @scala.unchecked =>
A.this.x.==(x$0.x)
case _ =>
false
})
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 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 {
Expand All @@ -22,4 +22,4 @@ case class A(x: scala.Int) {
throw new java.lang.IndexOutOfBoundsException(n.toString())
}
}
object A extends scala.Function1[scala.Int, A]
object A extends scala.Function1[scala.Int, A]
8 changes: 4 additions & 4 deletions tests/pos/simpleCaseClass-3.decompiled
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/** Decompiled from out/posTestFromTasty/pos/simpleCaseClass-3/A.class */
/** Decompiled from out/posTestFromTasty/pos/simpleCaseClass-3/A.tasty */
case class A[T](x: T) {
override def hashCode(): scala.Int = {
var acc: scala.Int = 65
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 = A.this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match {
case x$0: A[T] @scala.unchecked() =>
case x$0: A[T] @scala.unchecked =>
A.this.x.==(x$0.x)
case _ =>
false
})
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 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 {
Expand All @@ -22,4 +22,4 @@ case class A[T](x: T) {
throw new java.lang.IndexOutOfBoundsException(n.toString())
}
}
object A extends scala.AnyRef()
object A extends scala.AnyRef
6 changes: 3 additions & 3 deletions tests/pos/simpleCaseObject.decompiled
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/** Decompiled from out/posTestFromTasty/pos/simpleCaseObject/foo/Foo.class */
/** Decompiled from out/posTestFromTasty/pos/simpleCaseObject/foo/Foo.tasty */
package foo {
case object Foo {
override def hashCode(): scala.Int = 1045991777
override def toString(): java.lang.String = "Foo"
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[foo.Foo.type @scala.unchecked()]
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[foo.Foo.type @scala.unchecked]
override def productArity: scala.Int = 0
override def productPrefix: java.lang.String = "Foo"
override def productElement(n: scala.Int): scala.Any = n match {
case _ =>
throw new java.lang.IndexOutOfBoundsException(n.toString())
}
}
}
}
6 changes: 3 additions & 3 deletions tests/pos/simpleClass-2.decompiled
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/** Decompiled from out/posTestFromTasty/pos/simpleClass-2/foo/A.class */
/** Decompiled from out/posTestFromTasty/pos/simpleClass-2/foo/A.tasty */
package foo {
class A() extends foo.B()
class A() extends foo.B
}
/** Decompiled from out/posTestFromTasty/pos/simpleClass-2/foo/B.class */
/** Decompiled from out/posTestFromTasty/pos/simpleClass-2/foo/B.tasty */
package foo {
class B()
}
6 changes: 3 additions & 3 deletions tests/pos/simpleClass.decompiled
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/** Decompiled from out/posTestFromTasty/pos/simpleClass/foo/A.class */
/** Decompiled from out/posTestFromTasty/pos/simpleClass/foo/A.tasty */
package foo {
class A()
}
/** Decompiled from out/posTestFromTasty/pos/simpleClass/foo/B.class */
/** Decompiled from out/posTestFromTasty/pos/simpleClass/foo/B.tasty */
package foo {
class B() extends foo.A()
class B() extends foo.A
}
10 changes: 5 additions & 5 deletions tests/pos/simpleSuper.decompiled
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/** Decompiled from out/posTestFromTasty/pos/simpleSuper/foo/A.class */
/** Decompiled from out/posTestFromTasty/pos/simpleSuper/foo/A.tasty */
package foo {
class A() {
def foo: scala.Int = 1
}
}
/** Decompiled from out/posTestFromTasty/pos/simpleSuper/foo/B.class */
/** Decompiled from out/posTestFromTasty/pos/simpleSuper/foo/B.tasty */
package foo {
trait B() extends java.lang.Object {
def foo: scala.Int = 2
}
}
/** Decompiled from out/posTestFromTasty/pos/simpleSuper/foo/C.class */
/** Decompiled from out/posTestFromTasty/pos/simpleSuper/foo/C.tasty */
package foo {
class C() extends foo.A() with foo.B {
class C() extends foo.A with foo.B {
override def foo: scala.Int = super.foo.+(super[B].foo)
}
}
}
6 changes: 3 additions & 3 deletions tests/pos/t116.decompiled
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/** Decompiled from out/posTestFromTasty/pos/t116/C.class */
/** Decompiled from out/posTestFromTasty/pos/t116/C.tasty */
class C() {
def this(x: scala.Int) = {
this()
class D() extends C()
class D() extends C
()
}
}
}
Loading