Skip to content
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

fix #9179: ensure enum values are singleton with serialisation #9532

Merged
merged 8 commits into from
Aug 17, 2020

Conversation

bishabosha
Copy link
Member

@bishabosha bishabosha commented Aug 11, 2020

depends on #9524 for correct semantics merged

fixes #9179

this also adds the values and valueOf methods to companions of enums parameterised by a type

Copy link
Member

@dottybot dottybot left a comment

Choose a reason for hiding this comment

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

Hello, and thank you for opening this PR! 🎉

All contributors have signed the CLA, thank you! ❤️

Commit Messages

We want to keep history, but for that to actually be useful we have
some rules on how to format our commit messages (relevant xkcd).

Please stick to these guidelines for commit messages:

  1. Separate subject from body with a blank line
  2. When fixing an issue, start your commit message with Fix #<ISSUE-NBR>:
  3. Limit the subject line to 72 characters
  4. Capitalize the subject line
  5. Do not end the subject line with a period
  6. Use the imperative mood in the subject line ("Add" instead of "Added")
  7. Wrap the body at 80 characters
  8. Use the body to explain what and why vs. how

adapted from https://chris.beams.io/posts/git-commit

Have an awesome day! ☀️

@bishabosha bishabosha force-pushed the topic/enum-serialization branch 4 times, most recently from c00bb5d to c69caa2 Compare August 11, 2020 10:15
@bishabosha bishabosha changed the title ensure enum values are singleton with serialisation fix #9179: ensure enum values are singleton with serialisation Aug 11, 2020
@bishabosha
Copy link
Member Author

bishabosha commented Aug 11, 2020

I guess the issue now is that if someone has the wrong parents for their enum, then they get a cryptic error related to an object of the wrong type being passed to the EnumValues.register method, e.g.:

scala> enum Tag[+T]:
     |   case Int extends AnyRef
     | 
2 |  case Int extends AnyRef
  |                         ^
  |              Found:    ($anon.this : Object with runtime.EnumValue {...})
  |              Required: Tag[?]

Edit: this can be overcome by a cast at the register call that will be eliminated and refchecks will sanity check for unsafe casts
Second Edit: asInstanceOf is rejected by init checker

@smarter
Copy link
Member

smarter commented Aug 11, 2020

I guess the issue now is that if someone has the wrong parents for their enum

Can't we do something like:
When typechecking a case, verify that it's a subtype of the enum, if not emit an error and artificially add the enum as a parent of the class anyway to avoid follow-on errors

@bishabosha
Copy link
Member Author

bishabosha commented Aug 11, 2020

When typechecking a case, verify that it's a subtype of the enum, if not emit an error and artificially add the enum as a parent of the class anyway to avoid follow-on errors

for

enum Foo[F[_]]:
  case Bar extends AnyRef

what's the best way to invent type arguments for Foo that are well formed?

as an alternative which I don't really like, I am replacing the template body with an empty list if the parents are bad

@smarter
Copy link
Member

smarter commented Aug 11, 2020

what's the best way to invent type arguments for Foo that are well formed?

Nothing is poly-kinded so Foo[Nothing] should work.

@bishabosha
Copy link
Member Author

@smarter thank you for your suggestion about adding in the parent, it seems to not cause any knock on effects

@bishabosha
Copy link
Member Author

bishabosha commented Aug 12, 2020

I was experimenting with removing the EnumValueSerializationProxy class and was filtering on the companion to look for the .values method, but because it is has no parameters its info is ExprType and so its signature is Signature.NotAMethod, should that be correct?, is filtering by signature not possible? (.values is guaranteed anyway where I am doing this check so not necessary)

@smarter
Copy link
Member

smarter commented Aug 12, 2020

its info is ExprType and so its signature is Signature.NotAMethod, should that be correct?

That's normal yes, and you can filter by .signature == Signature.NotAMethod to find it

@bishabosha
Copy link
Member Author

I added another case to prevent anonymous classes extending an enum if they were defined within a term called $new (where $new isn't synthetic)

docs/docs/reference/enums/desugarEnums.md Outdated Show resolved Hide resolved
@smarter smarter assigned bishabosha and unassigned smarter Aug 14, 2020
@bishabosha bishabosha assigned smarter and unassigned bishabosha Aug 14, 2020
@bishabosha
Copy link
Member Author

rebased

@bishabosha
Copy link
Member Author

bishabosha commented Aug 17, 2020

I'm thinking that adding .values when there are only non-simple enum cases is actually a bug fix, not a new feature, without it java.util.EnumSet crashes whenever you try to construct it:

scala> enum Foo[T] extends java.lang.Enum[Foo[_]] { case A extends Foo[Unit] }
// defined class Foo

scala> java.util.EnumSet.allOf(classOf[Foo[?]])
java.lang.ClassCastException: class rs$line$1$Foo not an enum
	at java.util.EnumSet.noneOf(EnumSet.java:112)
	at java.util.EnumSet.allOf(EnumSet.java:131)
	at rs$line$2$.<init>(rs$line$2:1)
	at rs$line$2$.<clinit>(rs$line$2)
	at rs$line$2.res0(rs$line$2)
        ...

and now:

scala> java.util.EnumSet.allOf(classOf[Foo[?]])
val res0: java.util.EnumSet[Foo[?]] = [A]

@smarter
Copy link
Member

smarter commented Aug 17, 2020

Agreed (the alternative bugfix would be to reject java.lang.Enum parents for parameterized enums)

@smarter smarter assigned bishabosha and unassigned smarter Aug 17, 2020
@bishabosha bishabosha merged commit f4dfc7d into scala:master Aug 17, 2020
@bishabosha bishabosha deleted the topic/enum-serialization branch August 17, 2020 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Singleton enum cases (marked Serializable) are incorrectly serialized
3 participants