Skip to content

Value pack expansion in array literal #67192

Open
@lhunath

Description

@lhunath

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

SILGenArea → compiler: The SIL generation stagearray literalsFeature → expressions → literals: Array literalsbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfexpressionsFeature: expressionsgenericsFeature: generic declarations and typesliteralsFeature → expressions: Literals such as an integer or string literalpack expansionsFeature → expressions: Pack expansion expressionsparameter packsFeature → generics: Parameter packsswift 5.9type checkerArea → compiler: Semantic analysis

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions