Skip to content

Commit e1d2198

Browse files
authored
[TypeResolution] Ban local variable packs (#66622)
* Ban explicit local variable packs * Ban inferred local variable packs
1 parent a77cc30 commit e1d2198

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

lib/Sema/TypeCheckStorage.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,13 @@ const PatternBindingEntry *PatternBindingEntryRequest::evaluate(
473473
return &pbe;
474474
}
475475

476+
// Local variable packs are not allowed.
477+
if (binding->getDeclContext()->isLocalContext() &&
478+
binding->getInit(entryNumber)->getType()->is<PackExpansionType>()) {
479+
binding->diagnose(diag::expansion_not_allowed,
480+
binding->getInit(entryNumber)->getType());
481+
}
482+
476483
// A pattern binding at top level is not allowed to pick up another decl's
477484
// opaque result type as its type by type inference.
478485
if (!binding->getDeclContext()->isLocalContext() &&

lib/Sema/TypeCheckType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4618,7 +4618,7 @@ NeverNullType TypeResolver::resolvePackExpansionType(PackExpansionTypeRepr *repr
46184618
}
46194619

46204620
// We might not allow variadic expansions here at all.
4621-
if (!options.isPackExpansionSupported(getDeclContext())) {
4621+
if (!options.isPackExpansionSupported()) {
46224622
diagnose(repr->getLoc(), diag::expansion_not_allowed, result);
46234623
return ErrorType::get(ctx);
46244624
}

lib/Sema/TypeCheckType.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,20 +335,15 @@ class TypeResolutionOptions {
335335
}
336336

337337
/// Whether pack expansion types are supported in this context.
338-
bool isPackExpansionSupported(DeclContext *dc) const {
338+
bool isPackExpansionSupported() const {
339339
switch (context) {
340340
case Context::FunctionInput:
341341
case Context::VariadicFunctionInput:
342342
case Context::PackElement:
343343
case Context::TupleElement:
344344
case Context::GenericArgument:
345345
return true;
346-
347-
// Local variable packs are supported, but property packs
348-
// are not.
349346
case Context::PatternBindingDecl:
350-
return !dc->isTypeContext();
351-
352347
case Context::None:
353348
case Context::ProtocolGenericArgument:
354349
case Context::Inherited:

test/Constraints/pack-expansion-expressions.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ func coerceExpansion<each T>(_ value: repeat each T) {
3636

3737
func localValuePack<each T>(_ t: repeat each T) -> (repeat each T, repeat each T) {
3838
let local = repeat each t
39+
// expected-error@-1{{pack expansion 'repeat each T' can only appear in a function parameter list, tuple element, or generic argument list}}
3940
let localAnnotated: repeat each T = repeat each t
40-
// expected-error@-1{{value pack expansion can only appear inside a function argument list or tuple element}}
41+
// expected-error@-1{{pack expansion 'repeat each T' can only appear in a function parameter list, tuple element, or generic argument list}}
4142

4243
return (repeat each local, repeat each localAnnotated)
4344
}

0 commit comments

Comments
 (0)