Skip to content

[TypeResolution] Ban local variable packs #66622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/Sema/TypeCheckStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,13 @@ const PatternBindingEntry *PatternBindingEntryRequest::evaluate(
return &pbe;
}

// Local variable packs are not allowed.
if (binding->getDeclContext()->isLocalContext() &&
binding->getInit(entryNumber)->getType()->is<PackExpansionType>()) {
binding->diagnose(diag::expansion_not_allowed,
binding->getInit(entryNumber)->getType());
}

// A pattern binding at top level is not allowed to pick up another decl's
// opaque result type as its type by type inference.
if (!binding->getDeclContext()->isLocalContext() &&
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4617,7 +4617,7 @@ NeverNullType TypeResolver::resolvePackExpansionType(PackExpansionTypeRepr *repr
}

// We might not allow variadic expansions here at all.
if (!options.isPackExpansionSupported(getDeclContext())) {
if (!options.isPackExpansionSupported()) {
diagnose(repr->getLoc(), diag::expansion_not_allowed, result);
return ErrorType::get(ctx);
}
Expand Down
7 changes: 1 addition & 6 deletions lib/Sema/TypeCheckType.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,20 +335,15 @@ class TypeResolutionOptions {
}

/// Whether pack expansion types are supported in this context.
bool isPackExpansionSupported(DeclContext *dc) const {
bool isPackExpansionSupported() const {
switch (context) {
case Context::FunctionInput:
case Context::VariadicFunctionInput:
case Context::PackElement:
case Context::TupleElement:
case Context::GenericArgument:
return true;

// Local variable packs are supported, but property packs
// are not.
case Context::PatternBindingDecl:
return !dc->isTypeContext();

case Context::None:
case Context::ProtocolGenericArgument:
case Context::Inherited:
Expand Down
3 changes: 2 additions & 1 deletion test/Constraints/pack-expansion-expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ func coerceExpansion<each T>(_ value: repeat each T) {

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

return (repeat each local, repeat each localAnnotated)
}
Expand Down