Skip to content

enum values array is constructed from field references #9677

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 2 commits into from
Sep 2, 2020

Conversation

bishabosha
Copy link
Member

This patches a regression introduced in #9628 where
defining an enum local to a method can cause an infinite loop
at initialisation of its companion.

Instead, we select enum values from "this" and not the companion,
which avoids forcing initialisation of the companion. We then
ascribe the values array with unchecked annotation to avoid
complaints from the init checker.

This patches a regression introduced in scala#9628 where
defining an enum local to a method can cause an infinite loop
at initialisation of its companion.

Instead, we select enum values from "this" and not the companion,
which avoids forcing initialisation of the companion. We then
ascribe the values array with unchecked annotation to avoid
complaints from the init checker.
val uncheckedValues =
Annotated(ArrayLiteral(enumValues, rawEnumClassRef), New(ref(defn.UncheckedAnnot.typeRef)))
ValDef(nme.DOLLAR_VALUES, TypeTree(), uncheckedValues)
.withFlags(Private | Synthetic)
Copy link
Member

Choose a reason for hiding this comment

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

The lazyFlagOpt disappeared. Was this a mistake during conflict resolution?

Copy link
Member Author

@bishabosha bishabosha Aug 29, 2020

Choose a reason for hiding this comment

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

no, it was removed because it was only required when the enum values were explicitly selected from the companion, and the companion was a local lazy val. Now they are selected from this which doesn't force the companion to be initialised.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, that makes sense!

@@ -104,7 +104,7 @@ object DesugarEnums {

/** The following lists of definitions for an enum type E and known value cases e_0, ..., e_n:
*
* private val $values = Array[E](e_0,...,e_n)(ClassTag[E](classOf[E]))
* private val $values = Array[E](e_0,...,e_n)(ClassTag[E](classOf[E])): @unchecked
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment explaining the interaction with the init checker. Is this a known limitation of the init checker? Is it something that can be fixed and if so is there an issue for it?

Copy link
Member Author

@bishabosha bishabosha Sep 2, 2020

Choose a reason for hiding this comment

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

The issue is that the init checker will not try to prove that new E {...} is safe for performance reasons, so all enum cases that call $new creator method can't be checked. The init checker however knows that selecting from E companion object must be safe because it is already constructed, so we can either select from this which means $values does not have to be lazy, or we have to select from E which is under construction so it has to be lazy.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

we also have this comment #9665 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

OK, as long as this is all properly documented and referenced in the code, I'm happy :).

Copy link
Contributor

Choose a reason for hiding this comment

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

In the long run, we might introduce type annotations and/or eagerly check small classes, both could be helpful to avoid @unchecked in such cases. Initially, we want to keep the checker stupid to see how it works in the wild and make sure the core algorithm is reliable.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe an issue should be opened for that (either here or on dotty-feature-requests) so that Jamie can link to it in the code?

Copy link
Member Author

Choose a reason for hiding this comment

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

@bishabosha bishabosha merged commit e1f89fb into scala:master Sep 2, 2020
@bishabosha bishabosha deleted the fix-enum-values branch September 2, 2020 11:46
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
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.

5 participants