Skip to content

Commit 3283701

Browse files
Merge pull request #5729 from tuvior/decompiler-adjustments
Decompiler: empty parens, type variance
2 parents ce0f527 + fdf8139 commit 3283701

21 files changed

+117
-94
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/SymbolOpsImpl.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ trait SymbolOpsImpl extends scala.tasty.reflect.SymbolOps with CoreImpl {
9898

9999
def TypeSymbolDeco(symbol: TypeSymbol): TypeSymbolAPI = new TypeSymbolAPI {
100100
def tree(implicit ctx: Context): TypeDef = FromSymbol.typeDefFromSym(symbol)
101+
102+
def isTypeParam(implicit ctx: Context): Boolean = symbol.isTypeParam
101103
}
102104

103105
object ClassSymbol extends ClassSymbolModule {

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,9 @@ trait Printers
590590
else if (flags.is(Flags.Abstract)) this += highlightKeyword("abstract class ", color) += highlightTypeDef(name, color)
591591
else this += highlightKeyword("class ", color) += highlightTypeDef(name, color)
592592

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

609-
def printParent(parent: TermOrTypeTree): Unit = parent match {
610+
def printParent(parent: TermOrTypeTree, needEmptyParens: Boolean = false): Unit = parent match {
610611
case IsTypeTree(parent) =>
611612
printTypeTree(parent)
612613
case IsTerm(Term.TypeApply(fun, targs)) =>
613614
printParent(fun)
615+
case IsTerm(Term.Apply(fun@Term.Apply(_,_), args)) =>
616+
printParent(fun, true)
617+
if (!args.isEmpty || needEmptyParens)
618+
inParens(printTrees(args, ", "))
614619
case IsTerm(Term.Apply(fun, args)) =>
615620
printParent(fun)
616-
inParens(printTrees(args, ", "))
621+
if (!args.isEmpty || needEmptyParens)
622+
inParens(printTrees(args, ", "))
617623
case IsTerm(Term.Select(Term.New(tpt), _)) =>
618624
printTypeTree(tpt)
619625
case IsTerm(parent) =>
@@ -691,7 +697,7 @@ trait Printers
691697
case IsTypeDef(tdef @ TypeDef(name, rhs)) =>
692698
printDefAnnotations(tdef)
693699
this += highlightKeyword("type ", color)
694-
printTargDef(tdef, isMember = true)
700+
printTargDef((tdef, tdef), isMember = true)
695701

696702
case IsValDef(vdef @ ValDef(name, tpt, rhs)) =>
697703
printDefAnnotations(vdef)
@@ -754,7 +760,7 @@ trait Printers
754760
printProtectedOrPrivate(ddef)
755761

756762
this += highlightKeyword("def ", color) += highlightValDef((if (isConstructor) "this" else name), color)
757-
printTargsDefs(targs)
763+
printTargsDefs(targs.zip(targs))
758764
val it = argss.iterator
759765
while (it.hasNext)
760766
printArgsDefs(it.next())
@@ -1125,13 +1131,13 @@ trait Printers
11251131
this
11261132
}
11271133

1128-
def printTargsDefs(targs: List[TypeDef]): Unit = {
1134+
def printTargsDefs(targs: List[(TypeDef, TypeDef)], isDef:Boolean = true): Unit = {
11291135
if (!targs.isEmpty) {
1130-
def printSeparated(list: List[TypeDef]): Unit = list match {
1136+
def printSeparated(list: List[(TypeDef, TypeDef)]): Unit = list match {
11311137
case Nil =>
1132-
case x :: Nil => printTargDef(x)
1138+
case x :: Nil => printTargDef(x, isDef = isDef)
11331139
case x :: xs =>
1134-
printTargDef(x)
1140+
printTargDef(x, isDef = isDef)
11351141
this += ", "
11361142
printSeparated(xs)
11371143
}
@@ -1140,9 +1146,19 @@ trait Printers
11401146
}
11411147
}
11421148

1143-
def printTargDef(arg: TypeDef, isMember: Boolean = false): Buffer = {
1144-
this += arg.name
1145-
arg.rhs match {
1149+
def printTargDef(arg: (TypeDef, TypeDef), isMember: Boolean = false, isDef:Boolean = true): Buffer = {
1150+
val (argDef, argCons) = arg
1151+
1152+
if (isDef) {
1153+
if (argDef.symbol.flags.is(Flags.Covariant)) {
1154+
this += highlightValDef("+", color)
1155+
} else if (argDef.symbol.flags.is(Flags.Contravariant)) {
1156+
this += highlightValDef("-", color)
1157+
}
1158+
}
1159+
1160+
this += argCons.name
1161+
argCons.rhs match {
11461162
case IsTypeBoundsTree(rhs) => printBoundsTree(rhs)
11471163
case rhs @ WildcardTypeTree() =>
11481164
printTypeOrBound(rhs.tpe)
@@ -1412,7 +1428,7 @@ trait Printers
14121428
printTypeTree(result)
14131429

14141430
case TypeTree.LambdaTypeTree(tparams, body) =>
1415-
printTargsDefs(tparams)
1431+
printTargsDefs(tparams.zip(tparams), isDef = false)
14161432
this += highlightTypeDef(" => ", color)
14171433
printTypeOrBoundsTree(body)
14181434

@@ -1576,7 +1592,10 @@ trait Printers
15761592
val Annotation(ref, args) = annot
15771593
this += "@"
15781594
printTypeTree(ref)
1579-
inParens(printTrees(args, ", "))
1595+
if (args.isEmpty)
1596+
this
1597+
else
1598+
inParens(printTrees(args, ", "))
15801599
}
15811600

15821601
def printDefAnnotations(definition: Definition): Buffer = {

library/src/scala/tasty/reflect/SymbolOps.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ trait SymbolOps extends Core {
124124
trait TypeSymbolAPI {
125125
/** TypeDef tree of this definition. */
126126
def tree(implicit ctx: Context): TypeDef
127+
128+
def isTypeParam(implicit ctx: Context): Boolean
127129
}
128130
implicit def TypeSymbolDeco(symbol: TypeSymbol): TypeSymbolAPI
129131

tests/pos/i2104b.decompiled

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/** Decompiled from out/posTestFromTasty/pos/i2104b/Cons.class */
2-
trait Cons[H, T]() extends java.lang.Object
1+
/** Decompiled from out/posTestFromTasty/pos/i2104b/Cons.tasty */
2+
trait Cons[+H, +T]() extends java.lang.Object
33
object Cons {
44
def apply[H, T](h: H, t: T): Cons[H, T] = scala.Predef.???
55
def unapply[H, T](t: Cons[H, T]): scala.Option[Pair[H, T]] = scala.Predef.???
66
}
7-
/** Decompiled from out/posTestFromTasty/pos/i2104b/Pair.class */
7+
/** Decompiled from out/posTestFromTasty/pos/i2104b/Pair.tasty */
88
case class Pair[A, B](_1: A, _2: B) {
99
override def hashCode(): scala.Int = {
1010
var acc: scala.Int = 2479866
@@ -13,13 +13,13 @@ case class Pair[A, B](_1: A, _2: B) {
1313
scala.runtime.Statics.finalizeHash(acc, 2)
1414
}
1515
override def equals(x$0: scala.Any): scala.Boolean = Pair.this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match {
16-
case x$0: Pair[A, B] @scala.unchecked() =>
16+
case x$0: Pair[A, B] @scala.unchecked =>
1717
Pair.this._1.==(x$0._1).&&(Pair.this._2.==(x$0._2))
1818
case _ =>
1919
false
2020
})
2121
override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(Pair.this)
22-
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[Pair[A, B] @scala.unchecked()]
22+
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[Pair[A, B] @scala.unchecked]
2323
override def productArity: scala.Int = 2
2424
override def productPrefix: java.lang.String = "Pair"
2525
override def productElement(n: scala.Int): scala.Any = n match {
@@ -31,11 +31,11 @@ case class Pair[A, B](_1: A, _2: B) {
3131
throw new java.lang.IndexOutOfBoundsException(n.toString())
3232
}
3333
}
34-
object Pair extends scala.AnyRef()
35-
/** Decompiled from out/posTestFromTasty/pos/i2104b/Test.class */
34+
object Pair extends scala.AnyRef
35+
/** Decompiled from out/posTestFromTasty/pos/i2104b/Test.tasty */
3636
object Test {
3737
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 {
3838
case Cons(scala.Some(i), scala.None) =>
3939
()
4040
}
41-
}
41+
}

tests/pos/i4678.decompiled

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/** Decompiled from out/posTestFromTasty/pos/i4678/Foo.tasty */
22
class Foo() {
3-
val x: scala.Int = (1: @annot1() @annot2() @annot3() @annot4() @annot5())
3+
val x: scala.Int = (1: @annot1 @annot2 @annot3 @annot4 @annot5)
44
}
55
/** Decompiled from out/posTestFromTasty/pos/i4678/annot1.tasty */
6-
class annot1() extends scala.annotation.Annotation()
6+
class annot1() extends scala.annotation.Annotation
77
/** Decompiled from out/posTestFromTasty/pos/i4678/annot2.tasty */
8-
class annot2() extends scala.annotation.Annotation()
8+
class annot2() extends scala.annotation.Annotation
99
/** Decompiled from out/posTestFromTasty/pos/i4678/annot3.tasty */
10-
class annot3() extends scala.annotation.Annotation()
10+
class annot3() extends scala.annotation.Annotation
1111
/** Decompiled from out/posTestFromTasty/pos/i4678/annot4.tasty */
12-
class annot4() extends scala.annotation.Annotation()
12+
class annot4() extends scala.annotation.Annotation
1313
/** Decompiled from out/posTestFromTasty/pos/i4678/annot5.tasty */
14-
class annot5() extends scala.annotation.Annotation()
14+
class annot5() extends scala.annotation.Annotation

tests/pos/simpleAnnot.decompiled

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/** Decompiled from out/posTestFromTasty/pos/simpleAnnot/Foo.class */
1+
/** Decompiled from out/posTestFromTasty/pos/simpleAnnot/Foo.tasty */
22
class Foo() {
3-
@annot() type A = scala.Int
4-
@annot() val a: scala.Int = scala.Predef.???
5-
val b: scala.Int @annot() = scala.Predef.???
6-
def c(x: scala.Int): scala.Int = (x: @annot())
3+
@annot type A = scala.Int
4+
@annot val a: scala.Int = scala.Predef.???
5+
val b: scala.Int @annot = scala.Predef.???
6+
def c(x: scala.Int): scala.Int = (x: @annot)
77
}
8-
/** Decompiled from out/posTestFromTasty/pos/simpleAnnot/annot.class */
9-
class annot() extends scala.annotation.Annotation()
8+
/** Decompiled from out/posTestFromTasty/pos/simpleAnnot/annot.tasty */
9+
class annot() extends scala.annotation.Annotation
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
/** Decompiled from out/posTestFromTasty/pos/simpleCaseClass-1/A.class */
1+
/** Decompiled from out/posTestFromTasty/pos/simpleCaseClass-1/A.tasty */
22
case class A() {
33
override def hashCode(): scala.Int = 1914112431
44
override def equals(x$0: scala.Any): scala.Boolean = A.this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match {
5-
case x$0: A @scala.unchecked() =>
5+
case x$0: A @scala.unchecked =>
66
true
77
case _ =>
88
false
99
})
1010
override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(A.this)
11-
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[A @scala.unchecked()]
11+
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[A @scala.unchecked]
1212
override def productArity: scala.Int = 0
1313
override def productPrefix: java.lang.String = "A"
1414
override def productElement(n: scala.Int): scala.Any = n match {
1515
case _ =>
1616
throw new java.lang.IndexOutOfBoundsException(n.toString())
1717
}
1818
}
19-
object A extends scala.Function0[A]
19+
object A extends scala.Function0[A]
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
/** Decompiled from out/posTestFromTasty/pos/simpleCaseClass-2/A.class */
1+
/** Decompiled from out/posTestFromTasty/pos/simpleCaseClass-2/A.tasty */
22
case class A(x: scala.Int) {
33
override def hashCode(): scala.Int = {
44
var acc: scala.Int = 65
55
acc = scala.runtime.Statics.mix(acc, A.this.x)
66
scala.runtime.Statics.finalizeHash(acc, 1)
77
}
88
override def equals(x$0: scala.Any): scala.Boolean = A.this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match {
9-
case x$0: A @scala.unchecked() =>
9+
case x$0: A @scala.unchecked =>
1010
A.this.x.==(x$0.x)
1111
case _ =>
1212
false
1313
})
1414
override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(A.this)
15-
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[A @scala.unchecked()]
15+
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[A @scala.unchecked]
1616
override def productArity: scala.Int = 1
1717
override def productPrefix: java.lang.String = "A"
1818
override def productElement(n: scala.Int): scala.Any = n match {
@@ -22,4 +22,4 @@ case class A(x: scala.Int) {
2222
throw new java.lang.IndexOutOfBoundsException(n.toString())
2323
}
2424
}
25-
object A extends scala.Function1[scala.Int, A]
25+
object A extends scala.Function1[scala.Int, A]
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
/** Decompiled from out/posTestFromTasty/pos/simpleCaseClass-3/A.class */
1+
/** Decompiled from out/posTestFromTasty/pos/simpleCaseClass-3/A.tasty */
22
case class A[T](x: T) {
33
override def hashCode(): scala.Int = {
44
var acc: scala.Int = 65
55
acc = scala.runtime.Statics.mix(acc, scala.runtime.Statics.anyHash(A.this.x))
66
scala.runtime.Statics.finalizeHash(acc, 1)
77
}
88
override def equals(x$0: scala.Any): scala.Boolean = A.this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match {
9-
case x$0: A[T] @scala.unchecked() =>
9+
case x$0: A[T] @scala.unchecked =>
1010
A.this.x.==(x$0.x)
1111
case _ =>
1212
false
1313
})
1414
override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(A.this)
15-
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[A[T] @scala.unchecked()]
15+
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[A[T] @scala.unchecked]
1616
override def productArity: scala.Int = 1
1717
override def productPrefix: java.lang.String = "A"
1818
override def productElement(n: scala.Int): scala.Any = n match {
@@ -22,4 +22,4 @@ case class A[T](x: T) {
2222
throw new java.lang.IndexOutOfBoundsException(n.toString())
2323
}
2424
}
25-
object A extends scala.AnyRef()
25+
object A extends scala.AnyRef

tests/pos/simpleCaseObject.decompiled

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
/** Decompiled from out/posTestFromTasty/pos/simpleCaseObject/foo/Foo.class */
1+
/** Decompiled from out/posTestFromTasty/pos/simpleCaseObject/foo/Foo.tasty */
22
package foo {
33
case object Foo {
44
override def hashCode(): scala.Int = 1045991777
55
override def toString(): java.lang.String = "Foo"
6-
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[foo.Foo.type @scala.unchecked()]
6+
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[foo.Foo.type @scala.unchecked]
77
override def productArity: scala.Int = 0
88
override def productPrefix: java.lang.String = "Foo"
99
override def productElement(n: scala.Int): scala.Any = n match {
1010
case _ =>
1111
throw new java.lang.IndexOutOfBoundsException(n.toString())
1212
}
1313
}
14-
}
14+
}

tests/pos/simpleClass-2.decompiled

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/** Decompiled from out/posTestFromTasty/pos/simpleClass-2/foo/A.class */
1+
/** Decompiled from out/posTestFromTasty/pos/simpleClass-2/foo/A.tasty */
22
package foo {
3-
class A() extends foo.B()
3+
class A() extends foo.B
44
}
5-
/** Decompiled from out/posTestFromTasty/pos/simpleClass-2/foo/B.class */
5+
/** Decompiled from out/posTestFromTasty/pos/simpleClass-2/foo/B.tasty */
66
package foo {
77
class B()
88
}

tests/pos/simpleClass.decompiled

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/** Decompiled from out/posTestFromTasty/pos/simpleClass/foo/A.class */
1+
/** Decompiled from out/posTestFromTasty/pos/simpleClass/foo/A.tasty */
22
package foo {
33
class A()
44
}
5-
/** Decompiled from out/posTestFromTasty/pos/simpleClass/foo/B.class */
5+
/** Decompiled from out/posTestFromTasty/pos/simpleClass/foo/B.tasty */
66
package foo {
7-
class B() extends foo.A()
7+
class B() extends foo.A
88
}

tests/pos/simpleSuper.decompiled

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
/** Decompiled from out/posTestFromTasty/pos/simpleSuper/foo/A.class */
1+
/** Decompiled from out/posTestFromTasty/pos/simpleSuper/foo/A.tasty */
22
package foo {
33
class A() {
44
def foo: scala.Int = 1
55
}
66
}
7-
/** Decompiled from out/posTestFromTasty/pos/simpleSuper/foo/B.class */
7+
/** Decompiled from out/posTestFromTasty/pos/simpleSuper/foo/B.tasty */
88
package foo {
99
trait B() extends java.lang.Object {
1010
def foo: scala.Int = 2
1111
}
1212
}
13-
/** Decompiled from out/posTestFromTasty/pos/simpleSuper/foo/C.class */
13+
/** Decompiled from out/posTestFromTasty/pos/simpleSuper/foo/C.tasty */
1414
package foo {
15-
class C() extends foo.A() with foo.B {
15+
class C() extends foo.A with foo.B {
1616
override def foo: scala.Int = super.foo.+(super[B].foo)
1717
}
18-
}
18+
}

tests/pos/t116.decompiled

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/** Decompiled from out/posTestFromTasty/pos/t116/C.class */
1+
/** Decompiled from out/posTestFromTasty/pos/t116/C.tasty */
22
class C() {
33
def this(x: scala.Int) = {
44
this()
5-
class D() extends C()
5+
class D() extends C
66
()
77
}
8-
}
8+
}

0 commit comments

Comments
 (0)