Skip to content

Fix case class decompilation #4633

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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/tastyreflect/FlagSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class FlagSet(flags: Flags.FlagSet) extends scala.tasty.FlagSet {
if (isInline) flags += "inline"
if (isMacro) flags += "macro"
if (isStatic) flags += "javaStatic"
if (isObject) flags += "module"
if (isObject) flags += "object"
if (isTrait) flags += "trait"
if (isLocal) flags += "local"
if (isSynthetic) flags += "synthetic"
Expand Down
22 changes: 21 additions & 1 deletion library/src/scala/tasty/util/ShowSourceCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,24 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
}
}

def keepDefinition(d: Definition): Boolean = {
val flags = d.flags
def isCaseClassUnOverridableMethod: Boolean = {
// Currently the compiler does not allow overriding some of the methods generated for case classes
d.flags.isSynthetic &&
(d match {
case DefDef("apply" | "unapply", _, _, _, _) if d.owner.flags.isObject => true
case DefDef(n, _, _, _, _) if d.owner.flags.isCase =>
n == "copy" ||
n.matches("copy\\$default\\$[1-9][0-9]*") || // default parameters for the copy method
n.matches("_[1-9][0-9]*") // Getters from Product
case _ => false
})
}
!flags.isParam && !flags.isParamAccessor && !isCaseClassUnOverridableMethod
}
val stats1 = stats.collect {
case stat@Definition() if !stat.flags.isParam && !stat.flags.isParamAccessor => stat
case stat@Definition() if keepDefinition(stat) => stat
case stat@Import(_, _) => stat
case stat@Term() => stat
}
Expand Down Expand Up @@ -721,6 +737,10 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty

case Type.ThisType(tp) =>
printType(tp)
tp match {
case Type.SymRef(cdef @ ClassDef(_, _, _, _, _), _) if !cdef.flags.isObject => this += ".this"
case _ => this
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why special case object here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When refering to the this type of a module we just refere directly to the module itself. For example scala.Predef and not scala.Predef.this.


case _ =>
throw new MatchError(tpe.show)
Expand Down
19 changes: 19 additions & 0 deletions tests/pos/simpleCaseClass-1.decompiled
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/** 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 {
case x$0: A =>
true
case _ =>
false
})
override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(this)
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[A]
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]
1 change: 1 addition & 0 deletions tests/pos/simpleCaseClass-1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
case class A()
25 changes: 25 additions & 0 deletions tests/pos/simpleCaseClass-2.decompiled
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/** Decompiled from out/posTestFromTasty/pos/simpleCaseClass-2/A.class */
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 = this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match {
case x$0: A =>
this.x.==(x$0.x)
case _ =>
false
})
override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(this)
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[A]
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
case _ =>
throw new java.lang.IndexOutOfBoundsException(n.toString())
}
}
object A extends scala.Function1[scala.Int, A]
1 change: 1 addition & 0 deletions tests/pos/simpleCaseClass-2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
case class A(x: Int)
25 changes: 25 additions & 0 deletions tests/pos/simpleCaseClass-3.decompiled
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/** Decompiled from out/posTestFromTasty/pos/simpleCaseClass-3/A.class */
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 = this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match {
case x$0: A[A.this.T] =>
this.x.==(x$0.x)
case _ =>
false
})
override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(this)
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[A[A.this.T]]
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
case _ =>
throw new java.lang.IndexOutOfBoundsException(n.toString())
}
}
object A extends scala.AnyRef
1 change: 1 addition & 0 deletions tests/pos/simpleCaseClass-3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
case class A[T](x: T)