Skip to content

[TypeResolution] Enable local variable packs. #61755

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 1 commit into from
Oct 27, 2022
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
19 changes: 19 additions & 0 deletions lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3549,6 +3549,16 @@ void VarDeclUsageChecker::markStoredOrInOutExpr(Expr *E, unsigned Flags) {
OpaqueValueMap[OEE->getOpaqueValue()] = OEE->getExistentialValue();
return markStoredOrInOutExpr(OEE->getSubExpr(), Flags);
}

// Bind pack references in pack expansions.
if (auto *expansion = dyn_cast<PackExpansionExpr>(E)) {
for (unsigned i = 0; i < expansion->getNumBindings(); ++i) {
auto *opaqueValue = expansion->getOpaqueValues()[i];
auto *packReference = expansion->getBindings()[i];
OpaqueValueMap[opaqueValue] = packReference;
}
return markStoredOrInOutExpr(expansion->getPatternExpr(), Flags);
}

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

// If we see a PackExpansionExpr, record its pack reference bindings.
if (auto *expansion = dyn_cast<PackExpansionExpr>(E)) {
for (unsigned i = 0; i < expansion->getNumBindings(); ++i) {
auto *opaqueValue = expansion->getOpaqueValues()[i];
auto *packReference = expansion->getBindings()[i];
OpaqueValueMap[opaqueValue] = packReference;
}
}

// Visit bindings.
if (auto ove = dyn_cast<OpaqueValueExpr>(E)) {
if (auto mapping = OpaqueValueMap.lookup(ove))
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4223,7 +4223,7 @@ NeverNullType TypeResolver::resolvePackExpansionType(PackExpansionTypeRepr *repr
return ErrorType::get(ctx);

// We might not allow variadic expansions here at all.
if (!options.isPackExpansionSupported()) {
if (!options.isPackExpansionSupported(getDeclContext())) {
diagnose(repr->getLoc(), diag::expansion_not_allowed, pair.first);
return ErrorType::get(ctx);
}
Expand Down
8 changes: 6 additions & 2 deletions lib/Sema/TypeCheckType.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,19 @@ class TypeResolutionOptions {
}

/// Whether pack expansion types are supported in this context.
bool isPackExpansionSupported() const {
bool isPackExpansionSupported(DeclContext *dc) const {
switch (context) {
case Context::FunctionInput:
case Context::VariadicFunctionInput:
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 All @@ -333,7 +338,6 @@ class TypeResolutionOptions {
case Context::InExpression:
case Context::ExplicitCastExpr:
case Context::ForEachStmt:
case Context::PatternBindingDecl:
case Context::EditorPlaceholderExpr:
case Context::ClosureExpr:
case Context::InoutFunctionInput:
Expand Down
7 changes: 7 additions & 0 deletions test/Constraints/pack-expansion-expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ func coerceExpansion<T...>(_ value: T...) {

promoteToOptional(value...)
}

func localValuePack<T...>(_ t: T...) -> (T..., T...) {
let local = t...
let localAnnotated: T... = t...

return (local..., localAnnotated...)
}