Skip to content

Singleton enum cases (marked Serializable) are incorrectly serialized #9179

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
akouryy opened this issue Jun 14, 2020 · 1 comment · Fixed by #9532
Closed

Singleton enum cases (marked Serializable) are incorrectly serialized #9179

akouryy opened this issue Jun 14, 2020 · 1 comment · Fixed by #9532
Assignees

Comments

@akouryy
Copy link

akouryy commented Jun 14, 2020

Minimized code

Running twice with 0.25.0-RC1:

import java.io._

enum A:
  case B

object Main extends App:
  val file = File("/tmp/EnumSerialization.dat")

  if file.exists
    val a = ObjectInputStream(FileInputStream(file)).readObject().asInstanceOf[A]
    println(a == A.B)
    a match
      case A.B => // passes exhaustivity check
  else
    ObjectOutputStream(FileOutputStream(file)).writeObject(A.B)

Output

On second execution:

false

[error] java.lang.ExceptionInInitializerError
...
[error] Caused by: scala.MatchError: B (of class A$$anon$1)

Expectation

I expected a == A.B because it is marked Serializable by default.

I think there are two options:

@odersky
Copy link
Contributor

odersky commented Jun 17, 2020

I think we should try to generate readObject and writeObject methods. It's natural for enumerations to be serializable.

@bishabosha bishabosha mentioned this issue Jul 9, 2020
22 tasks
bishabosha added a commit to dotty-staging/dotty that referenced this issue Aug 10, 2020
bishabosha added a commit to dotty-staging/dotty that referenced this issue Aug 10, 2020
bishabosha added a commit to dotty-staging/dotty that referenced this issue Aug 11, 2020
bishabosha added a commit to dotty-staging/dotty that referenced this issue Aug 11, 2020
bishabosha added a commit to dotty-staging/dotty that referenced this issue Aug 11, 2020
bishabosha added a commit to dotty-staging/dotty that referenced this issue Aug 11, 2020
bishabosha added a commit to dotty-staging/dotty that referenced this issue Aug 11, 2020
bishabosha added a commit to dotty-staging/dotty that referenced this issue Aug 11, 2020
bishabosha added a commit to dotty-staging/dotty that referenced this issue Aug 13, 2020
bishabosha added a commit to dotty-staging/dotty that referenced this issue Aug 17, 2020
bishabosha added a commit that referenced this issue Aug 17, 2020
fix #9179: ensure enum values are singleton with serialisation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants