Commit Graph

6 Commits

Author SHA1 Message Date
Artem Chikin 27a02602b9 [Literal Expressions] Reject references to publicly visible let bindings
A `let` binding declared `public`, `package`, or `open` participates in its module's ABI surface as a symbol — clients link against the declaration, not against its value, so the author is free to change the value in a future release. Folding such a reference in a literal expression would risk "baking" a module-private value into client code and remove the author's room to evolve the binding. Reject these references with a new diagnostic that reports the offending access level. `internal`, `fileprivate`, and `private` bindings continue to fold, as do Clang-imported constants.
2026-05-08 14:23:28 +01:00
Artem Chikin 0d39a2b257 [Literal Expressions] Use arithmetic shift for signed >>
The expression folder previously applied `APInt::lshr` for every `>>` regardless of signedness. For a negative signed operand this yields a large positive value and diverges from Swift runtime behavior, which performs an arithmetic right shift that preserves the sign bit.

Dispatch to `APInt::ashr` when the operand type is signed, and retain `APInt::lshr` for unsigned operands.
2026-05-08 14:23:24 +01:00
Artem Chikin 04be54c2b9 [Literal Expressions] Add support for wrapping arithmetic and masking shift operators 2026-05-08 10:52:06 +01:00
Artem Chikin b62713d36b [Literal Expressions] Support Int128 and UInt128 2026-05-08 10:20:47 +01:00
Artem Chikin 1e5ba5fca8 [Literal Expressions] Add support for literal expressions in enum raw 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
2026-02-10 09:43:07 +00:00
Artem Chikin 0bbd35242c [Literal Expressions] Add support for simple constant-folded literal 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.
2026-02-09 11:35:45 +00:00