Skip to content

Commit 90ca95c

Browse files
committed
Support pack iteration in closures
1 parent fc74e99 commit 90ca95c

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

lib/AST/ASTWalker.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,11 +1890,9 @@ Stmt *Traversal::visitForEachStmt(ForEachStmt *S) {
18901890
//
18911891
// If for-in is already type-checked, the type-checked version
18921892
// of the sequence is going to be visited as part of `iteratorVar`.
1893-
if (S->getTypeCheckedSequence()) {
1894-
if (auto IteratorVar = S->getIteratorVar()) {
1895-
if (doIt(IteratorVar))
1896-
return nullptr;
1897-
}
1893+
if (auto IteratorVar = S->getIteratorVar()) {
1894+
if (doIt(IteratorVar))
1895+
return nullptr;
18981896

18991897
if (auto NextCall = S->getNextCall()) {
19001898
if ((NextCall = doIt(NextCall)))

lib/AST/Decl.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6893,14 +6893,6 @@ VarDecl::VarDecl(DeclKind kind, bool isStatic, VarDecl::Introducer introducer,
68936893
Type VarDecl::getTypeInContext() const {
68946894
// If we are performing pack iteration, use the generic environment of the
68956895
// pack expansion expression to get the right context of a local variable.
6896-
// if (auto *forEachStmt =
6897-
// dyn_cast_or_null<ForEachStmt>(this->getParentPatternStmt())) {
6898-
// if (auto *expansion =
6899-
// dyn_cast<PackExpansionExpr>(forEachStmt->getParsedSequence())) {
6900-
// return GenericEnvironment::mapTypeIntoContext(
6901-
// expansion->getGenericEnvironment(), getInterfaceType());
6902-
// }
6903-
// }
69046896
if (auto *env = getOpenedElementEnvironment())
69056897
return GenericEnvironment::mapTypeIntoContext(env, getInterfaceType());
69066898

lib/Sema/CSSyntacticElement.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,6 +1874,36 @@ class SyntacticElementSolutionApplication
18741874
}
18751875

18761876
ASTNode visitForEachStmt(ForEachStmt *forEachStmt) {
1877+
1878+
class Walker : public ASTWalker {
1879+
GenericEnvironment *Environment;
1880+
1881+
public:
1882+
Walker(GenericEnvironment *Environment) {
1883+
this->Environment = Environment;
1884+
}
1885+
1886+
PreWalkResult<Stmt *> walkToStmtPre(Stmt *S) override {
1887+
if (isa<ForEachStmt>(S)) {
1888+
return Action::Stop();
1889+
}
1890+
return Action::Continue(S);
1891+
}
1892+
1893+
PreWalkAction walkToDeclPre(Decl *D) override {
1894+
if (auto *decl = dyn_cast<VarDecl>(D)) {
1895+
decl->setOpenedElementEnvironment(Environment);
1896+
}
1897+
if (isa<AbstractFunctionDecl>(D)) {
1898+
return Action::Stop();
1899+
}
1900+
if (isa<NominalTypeDecl>(D)) {
1901+
return Action::Stop();
1902+
}
1903+
return Action::Continue();
1904+
}
1905+
};
1906+
18771907
ConstraintSystem &cs = solution.getConstraintSystem();
18781908

18791909
auto forEachTarget = rewriteTarget(*cs.getTargetFor(forEachStmt));
@@ -1884,6 +1914,13 @@ class SyntacticElementSolutionApplication
18841914
auto body = visit(forEachStmt->getBody()).get<Stmt *>();
18851915
forEachStmt->setBody(cast<BraceStmt>(body));
18861916

1917+
if (auto *expansion = dyn_cast<PackExpansionExpr>(
1918+
forEachStmt->getTypeCheckedSequence())) {
1919+
Walker forEachWalker(expansion->getGenericEnvironment());
1920+
forEachStmt->getPattern()->walk(forEachWalker);
1921+
forEachStmt->getBody()->walk(forEachWalker);
1922+
}
1923+
18871924
// Check to see if the sequence expr is throwing (in async context),
18881925
// if so require the stmt to have a `try`.
18891926
hadError |= diagnoseUnhandledThrowsInAsyncContext(

lib/Sema/TypeCheckStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
14251425
RWS->setCond(E);
14261426
return RWS;
14271427
}
1428-
1428+
14291429
Stmt *visitForEachStmt(ForEachStmt *S) {
14301430
// A special walker to record opened element environment for var decls in a
14311431
// for-each loop.

0 commit comments

Comments
 (0)