Skip to content

Conversation

@artemcm
Copy link
Contributor

@artemcm artemcm commented Feb 2, 2026

Only top commit is new. Based on top of #86500.

Modify relevant portions of the type-checker and parser to allow, when the LiteralExpressions experimental 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 of EnumElementDecl consistent for clients.

Previously, EnumRawValuesRequest had 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 (ConstantFoldExpression request) to run on the expressions. Constant folding is invoked during the evaluation of EnumRawValuesRequest on 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.
- EnumElementDecl continues to store the raw value expression, which is no longer a LiteralExpr but rather an Expr; however, the getter (getRawValueExpr) continues to return a LiteralExpr by invoking the constant-folding request on the stored value, which is guaranteed to return a cached result from a prior invocation in EnumRawValuesRequest, 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

…expressions

This change adds an experimental feature 'LiteralExpressions` which allows for use of simple binary expressions of integer type composed of literal value operands.

For now, such literal expressions are only supported in initializers of '@section` values.
@artemcm
Copy link
Contributor Author

artemcm commented Feb 2, 2026

@swift-ci test

@artemcm artemcm force-pushed the LiteralExpressionEnumRawValues branch from 1a4da13 to d236b57 Compare February 2, 2026 12:03
@artemcm
Copy link
Contributor Author

artemcm commented Feb 2, 2026

@swift-ci test

@artemcm artemcm force-pushed the LiteralExpressionEnumRawValues branch 3 times, most recently from abc820b to df8df41 Compare February 2, 2026 15:24
@artemcm
Copy link
Contributor Author

artemcm commented Feb 2, 2026

@swift-ci test

… values

Modify relevant portions of the type-checker and parser to allow, when the 'LiteralExpressions' experimental 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 of 'EnumElementDecl' consistent for clients.

Previously, 'EnumRawValuesRequest' had two different "modes" which were discerned based on typechecking 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 ('ConstantFoldExpression' request) to run on the expressions. Constant folding is invoked during the evaluation of 'EnumRawValuesRequest' on 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.
- 'EnumElementDecl' continues to store the raw value expression, which is no longer a 'LiteralExpr' but rather an 'Expr'; however, the getter ('getRawValueExpr') continues to return a 'LiteralExpr' by invoking the constant-folding request on the stored value, which is guaranteed to return a cached result from a prior invocation in 'EnumRawValuesRequest', 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
@artemcm artemcm force-pushed the LiteralExpressionEnumRawValues branch from df8df41 to 8102219 Compare February 2, 2026 16:18
@artemcm
Copy link
Contributor Author

artemcm commented Feb 2, 2026

@swift-ci test

@artemcm artemcm requested a review from xedin February 2, 2026 16:50
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.

1 participant