Open
Description
Description
Attempting to expand a value pack in an array literal yields a compiler error message:
Value pack expansion can only appear inside a function argument list or tuple element
This seems contrary to the specification for value pack expansion in SE-0393:
in any position that naturally accepts a list of expressions, including comma-separated lists and top-level expressions. This includes the following:
...
- The elements of an array literal, e.g. [repeat each value]
Steps to reproduce
func f<each V: StringProtocol>(_ v: repeat each V) -> [any StringProtocol] {
[repeat each v] // Value pack expansion can only appear inside a function argument list or tuple element
}
Expected behavior
The value pack expansion [repeat each v]
expands to a literal where each parameter v
is enumerated into the array expression.
Environment
- swift-driver version: 1.85 Apple Swift version 5.9 (swiftlang-5.9.0.120.7 clang-1500.0.34.3)
- Xcode Version 15.0 beta 3 (15A5195k)
- Deployment target: iOS 17.0
Additional context
https://forums.swift.org/t/value-parameter-pack-expansion-in-array-literal/66047
Metadata
Metadata
Assignees
Labels
Area → compiler: The SIL generation stageFeature → expressions → literals: Array literalsA deviation from expected or documented behavior. Also: expected but undesirable behavior.The Swift compiler itselfFeature: expressionsFeature: generic declarations and typesFeature → expressions: Literals such as an integer or string literalFeature → expressions: Pack expansion expressionsFeature → generics: Parameter packsArea → compiler: Semantic analysis