[Literal Expressions] Add support for literal expressions in enum raw values #86934
+1,063
−274
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Only top commit is new. Based on top of #86500.
Modify relevant portions of the type-checker and parser to allow, when the
LiteralExpressionsexperimental feature is enabled, for arbitrary integer-typed expressions in enum raw value specifiers. These expressions will be type-checked and constant-folded into an integer literal expression, keeping the current interface ofEnumElementDeclconsistent for clients.Previously,
EnumRawValuesRequesthad two different "modes" which were discerned based on type-checking stage (structural|interface), where the former had the request compute all raw values, both user-specified literal expressions and computing increment-derived values as well; the latter would also type-check the user-specified expressions and compute their types.- With the need to have enum case raw values support arbitrary integer expressions, the request (
EnumRawValuesRequest) has been refactored and simplified to always both compute all case raw values and perform type-checking of user-specified raw value expressions. This is done in order to allow the AST-based constant-folding infrastructure (ConstantFoldExpressionrequest) to run on the expressions. Constant folding is invoked during the evaluation ofEnumRawValuesRequeston all user-specified raw value expressions, in order to be able to compute subsequent increment values and ensure the expressions are foldable. If they are not, i.e. if constant folding fails, a relevant diagnostic will be emitted.-
EnumElementDeclcontinues to store the raw value expression, which is no longer aLiteralExprbut rather anExpr; however, the getter (getRawValueExpr) continues to return aLiteralExprby invoking the constant-folding request on the stored value, which is guaranteed to return a cached result from a prior invocation inEnumRawValuesRequest, assuming it succeeded.- Furthermore, the 'structural' request kind was previously not cached, whereas now because the request must always do the complete type-checking work, it is always cached.
Resolves rdar://168005520