Skip to content

Don't use forwarders in closure implementation #4446

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

Closed
allanrenucci opened this issue May 2, 2018 · 2 comments
Closed

Don't use forwarders in closure implementation #4446

allanrenucci opened this issue May 2, 2018 · 2 comments

Comments

@allanrenucci
Copy link
Contributor

class Foo {
  (x: Int) => x + 1
}

A closure that is not a SAM will desugar to something like this in Dotty:

class Foo {
  def apply(x: Int) = x + 1

  class $anon extends Function1[Int, Int] {
    def apply(x: Int) = Foo.this.apply(x)
  }
  new $anon
}

and this in scalac:

class Foo {
  class $anon extends Function1[Int, Int] {
    def apply(x: Int) =  x + 1
  }
  new $anon
}

The issue with the Dotty desugaring, is that $anon will capture a reference to the outer class Foo that is not needed

@allanrenucci
Copy link
Contributor Author

allanrenucci commented May 2, 2018

Here is an example where the captured outer reference prevents a partial function from being serialisable:

class Foo {
  def foo: PartialFunction[Int, Int] = { case x => x + 1 }
}

object Test {
  def serializeDeserialize[T <: AnyRef](obj: T): T = {
    import java.io._
    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 adder = serializeDeserialize((new Foo).foo)
    assert(adder(1) == 2)
  }
}
Exception in thread "main" java.io.NotSerializableException: Foo
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
	at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
	at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
	at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
	at Test$.serializeDeserialize(Test.scala:10)
	at Test$.main(Test.scala:16)
	at Test.main(Test.scala)

@allanrenucci
Copy link
Contributor Author

Fixing #4438 would make this fix trivial

@allanrenucci allanrenucci changed the title Don't use forwarder in closure implementation Don't use forwarders in closure implementation May 2, 2018
allanrenucci added a commit to dotty-staging/dotty that referenced this issue May 30, 2018
allanrenucci added a commit to dotty-staging/dotty that referenced this issue Jun 1, 2018
allanrenucci added a commit to dotty-staging/dotty that referenced this issue Jun 1, 2018
allanrenucci added a commit to dotty-staging/dotty that referenced this issue Jun 1, 2018
allanrenucci added a commit to dotty-staging/dotty that referenced this issue Jun 3, 2018
allanrenucci added a commit to dotty-staging/dotty that referenced this issue Jun 4, 2018
allanrenucci added a commit to dotty-staging/dotty that referenced this issue Jun 6, 2018
allanrenucci added a commit that referenced this issue Jun 13, 2018
Fix #4446: Inline implementation of PF methods into its anonymous class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant