-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
* def values = $values.clone | ||
* def valueOf($name: String) = $name match { | ||
* case "e_0" => e_0 | ||
|
@@ -117,9 +117,11 @@ object DesugarEnums { | |
val rawEnumClassRef = rawRef(enumClass.typeRef) | ||
extension (tpe: NamedType) def ofRawEnum = AppliedTypeTree(ref(tpe), rawEnumClassRef) | ||
|
||
val lazyFlagOpt = if enumCompanion.owner.isStatic then EmptyFlags else Lazy | ||
val privateValuesDef = ValDef(nme.DOLLAR_VALUES, TypeTree(), ArrayLiteral(enumValues, rawEnumClassRef)) | ||
.withFlags(Private | Synthetic | lazyFlagOpt) | ||
val privateValuesDef = | ||
val uncheckedValues = | ||
Annotated(ArrayLiteral(enumValues, rawEnumClassRef), New(ref(defn.UncheckedAnnot.typeRef))) | ||
ValDef(nme.DOLLAR_VALUES, TypeTree(), uncheckedValues) | ||
.withFlags(Private | Synthetic) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, that makes sense! |
||
|
||
val valuesDef = | ||
DefDef(nme.values, Nil, Nil, defn.ArrayType.ofRawEnum, valuesDot(nme.clone_)) | ||
|
@@ -170,7 +172,6 @@ object DesugarEnums { | |
* def ordinal = _$ordinal // if `E` does not derive from `java.lang.Enum` | ||
* def enumLabel = $name // if `E` does not derive from `java.lang.Enum` | ||
* def enumLabel = this.name // if `E` derives from `java.lang.Enum` | ||
* $values.register(this) | ||
* } | ||
*/ | ||
private def enumValueCreator(using Context) = { | ||
|
@@ -307,8 +308,8 @@ object DesugarEnums { | |
case name: TermName => (ordinal, name) :: seenCases | ||
case _ => seenCases | ||
if definesLookups then | ||
val companionRef = ref(enumCompanion.termRef) | ||
val cachedValues = cases.reverse.map((i, name) => (i, Select(companionRef, name))) | ||
val thisRef = This(EmptyTypeIdent) | ||
val cachedValues = cases.reverse.map((i, name) => (i, Select(thisRef, name))) | ||
(ordinal, enumLookupMethods(EnumConstraints(minKind, maxKind, cachedValues))) | ||
else | ||
ctx.tree.pushAttachment(EnumCaseCount, (ordinal + 1, minKind, maxKind, cases)) | ||
|
There was a problem hiding this comment.
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?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 fromE
companion object must be safe because it is already constructed, so we can either select fromthis
which means$values
does not have to be lazy, or we have to select fromE
which is under construction so it has to be lazy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @liufengyun
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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 :).
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened https://github.com/lampepfl/dotty-feature-requests/issues/135