Skip to content

Fix #10038: Always emit mixin forwarders for readResolve/writeReplace #10051

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 1 commit into from
Oct 21, 2020
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
16 changes: 12 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/MixinOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,23 @@ class MixinOps(cls: ClassSymbol, thisPhase: DenotTransformer)(using Context) {

def needsDisambiguation = competingMethods.exists(x=> !x.is(Deferred)) // multiple implementations are available
def hasNonInterfaceDefinition = competingMethods.exists(!_.owner.is(Trait)) // there is a definition originating from class

// JUnit 4 won't recognize annotated default methods, so always generate a forwarder for them.
def generateJUnitForwarder: Boolean =
meth.annotations.nonEmpty && JUnit4Annotations.exists(annot => meth.hasAnnotation(annot)) &&
ctx.settings.mixinForwarderChoices.isAtLeastJunit

// Similarly, Java serialization won't take into account a readResolve/writeReplace default method.
def generateSerializationForwarder: Boolean =
(meth.name == nme.readResolve || meth.name == nme.writeReplace) && meth.info.paramNamess.flatten.isEmpty

!meth.isConstructor &&
meth.is(Method, butNot = PrivateOrAccessorOrDeferred) &&
(ctx.settings.mixinForwarderChoices.isTruthy || meth.owner.is(Scala2x) || needsDisambiguation || hasNonInterfaceDefinition || needsJUnit4Fix(meth)) &&
(ctx.settings.mixinForwarderChoices.isTruthy || meth.owner.is(Scala2x) || needsDisambiguation || hasNonInterfaceDefinition ||
generateJUnitForwarder || generateSerializationForwarder) &&
isCurrent(meth)
}

private def needsJUnit4Fix(meth: Symbol): Boolean =
meth.annotations.nonEmpty && JUnit4Annotations.exists(annot => meth.hasAnnotation(annot)) &&
ctx.settings.mixinForwarderChoices.isAtLeastJunit

final val PrivateOrAccessor: FlagSet = Private | Accessor
final val PrivateOrAccessorOrDeferred: FlagSet = Private | Accessor | Deferred
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class CompilationTests {
compileFile("tests/run-custom-args/i5256.scala", allowDeepSubtypes),
compileFile("tests/run-custom-args/fors.scala", defaultOptions.and("-source", "3.1")),
compileFile("tests/run-custom-args/no-useless-forwarders.scala", defaultOptions and "-Xmixin-force-forwarders:false"),
compileFile("tests/run-custom-args/defaults-serizaliable-no-forwarders.scala", defaultOptions and "-Xmixin-force-forwarders:false"),
compileFilesInDir("tests/run-custom-args/erased", defaultOptions.and("-Yerased-terms")),
compileFilesInDir("tests/run-deep-subtype", allowDeepSubtypes),
compileFilesInDir("tests/run", defaultOptions)
Expand Down
26 changes: 26 additions & 0 deletions tests/run-custom-args/defaults-serizaliable-no-forwarders.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream}

trait T1 extends Serializable {
def writeReplace(): AnyRef = new SerializationProxy(this.asInstanceOf[C].s)
}
trait T2 {
def readResolve: AnyRef = new C(this.asInstanceOf[SerializationProxy].s.toLowerCase)
}
class C(val s: String) extends T1
class SerializationProxy(val s: String) extends T2 with Serializable

object Test {
def serializeDeserialize[T <: AnyRef](obj: T) = {
val buffer = new ByteArrayOutputStream
val out = new ObjectOutputStream(buffer)
out.writeObject(obj)
val in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray))
in.readObject.asInstanceOf[T]
}

def main(args: Array[String]): Unit = {
val c1 = new C("TEXT")
val c2 = serializeDeserialize(c1)
assert(c2.s == "text")
}
}
26 changes: 26 additions & 0 deletions tests/run/defaults-serizaliable-with-forwarders.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream}

trait T1 extends Serializable {
def writeReplace(): AnyRef = new SerializationProxy(this.asInstanceOf[C].s)
}
trait T2 {
def readResolve: AnyRef = new C(this.asInstanceOf[SerializationProxy].s.toLowerCase)
}
class C(val s: String) extends T1
class SerializationProxy(val s: String) extends T2 with Serializable

object Test {
def serializeDeserialize[T <: AnyRef](obj: T) = {
val buffer = new ByteArrayOutputStream
val out = new ObjectOutputStream(buffer)
out.writeObject(obj)
val in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray))
in.readObject.asInstanceOf[T]
}

def main(args: Array[String]): Unit = {
val c1 = new C("TEXT")
val c2 = serializeDeserialize(c1)
assert(c2.s == "text")
}
}