Skip to content

Commit d864ceb

Browse files
committed
[TypeResolution] Enable local variable packs.
1 parent d09ea98 commit d864ceb

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,6 +3549,16 @@ void VarDeclUsageChecker::markStoredOrInOutExpr(Expr *E, unsigned Flags) {
35493549
OpaqueValueMap[OEE->getOpaqueValue()] = OEE->getExistentialValue();
35503550
return markStoredOrInOutExpr(OEE->getSubExpr(), Flags);
35513551
}
3552+
3553+
// Bind pack references in pack expansions.
3554+
if (auto *expansion = dyn_cast<PackExpansionExpr>(E)) {
3555+
for (unsigned i = 0; i < expansion->getNumBindings(); ++i) {
3556+
auto *opaqueValue = expansion->getOpaqueValues()[i];
3557+
auto *packReference = expansion->getBindings()[i];
3558+
OpaqueValueMap[opaqueValue] = packReference;
3559+
}
3560+
return markStoredOrInOutExpr(expansion->getPatternExpr(), Flags);
3561+
}
35523562

35533563
// If this is an OpaqueValueExpr that we've seen a mapping for, jump to the
35543564
// mapped value.
@@ -3626,6 +3636,15 @@ ASTWalker::PreWalkResult<Expr *> VarDeclUsageChecker::walkToExprPre(Expr *E) {
36263636
return Action::SkipChildren(E);
36273637
}
36283638

3639+
// If we see a PackExpansionExpr, record its pack reference bindings.
3640+
if (auto *expansion = dyn_cast<PackExpansionExpr>(E)) {
3641+
for (unsigned i = 0; i < expansion->getNumBindings(); ++i) {
3642+
auto *opaqueValue = expansion->getOpaqueValues()[i];
3643+
auto *packReference = expansion->getBindings()[i];
3644+
OpaqueValueMap[opaqueValue] = packReference;
3645+
}
3646+
}
3647+
36293648
// Visit bindings.
36303649
if (auto ove = dyn_cast<OpaqueValueExpr>(E)) {
36313650
if (auto mapping = OpaqueValueMap.lookup(ove))

lib/Sema/TypeCheckType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4223,7 +4223,7 @@ NeverNullType TypeResolver::resolvePackExpansionType(PackExpansionTypeRepr *repr
42234223
return ErrorType::get(ctx);
42244224

42254225
// We might not allow variadic expansions here at all.
4226-
if (!options.isPackExpansionSupported()) {
4226+
if (!options.isPackExpansionSupported(getDeclContext())) {
42274227
diagnose(repr->getLoc(), diag::expansion_not_allowed, pair.first);
42284228
return ErrorType::get(ctx);
42294229
}

lib/Sema/TypeCheckType.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,19 @@ class TypeResolutionOptions {
311311
}
312312

313313
/// Whether pack expansion types are supported in this context.
314-
bool isPackExpansionSupported() const {
314+
bool isPackExpansionSupported(DeclContext *dc) const {
315315
switch (context) {
316316
case Context::FunctionInput:
317317
case Context::VariadicFunctionInput:
318318
case Context::TupleElement:
319319
case Context::GenericArgument:
320320
return true;
321321

322+
// Local variable packs are supported, but property packs
323+
// are not.
324+
case Context::PatternBindingDecl:
325+
return !dc->isTypeContext();
326+
322327
case Context::None:
323328
case Context::ProtocolGenericArgument:
324329
case Context::Inherited:
@@ -333,7 +338,6 @@ class TypeResolutionOptions {
333338
case Context::InExpression:
334339
case Context::ExplicitCastExpr:
335340
case Context::ForEachStmt:
336-
case Context::PatternBindingDecl:
337341
case Context::EditorPlaceholderExpr:
338342
case Context::ClosureExpr:
339343
case Context::InoutFunctionInput:

test/Constraints/pack-expansion-expressions.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ func coerceExpansion<T...>(_ value: T...) {
3333

3434
promoteToOptional(value...)
3535
}
36+
37+
func localValuePack<T...>(_ t: T...) -> (T..., T...) {
38+
let local = t...
39+
let localAnnotated: T... = t...
40+
41+
return (local..., localAnnotated...)
42+
}

0 commit comments

Comments
 (0)