Skip to content

Commit 0d87cd3

Browse files
committed
Add SingleValueStmtExpr to SyntacticElementContext
And change `getBody` to `getStmt` to reflect the fact that we always rewrite a statement.
1 parent f8e72ca commit 0d87cd3

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

lib/Sema/CSSyntacticElement.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ ElementInfo makeElement(ASTNode node, ConstraintLocator *locator,
353353
}
354354

355355
struct SyntacticElementContext
356-
: public llvm::PointerUnion<AbstractFunctionDecl *, AbstractClosureExpr *> {
356+
: public llvm::PointerUnion<AbstractFunctionDecl *, AbstractClosureExpr *,
357+
SingleValueStmtExpr *> {
357358
// Inherit the constructors from PointerUnion.
358359
using PointerUnion::PointerUnion;
359360

@@ -373,11 +374,18 @@ struct SyntacticElementContext
373374
return {func};
374375
}
375376

377+
static SyntacticElementContext
378+
forSingleValueStmtExpr(SingleValueStmtExpr *SVE) {
379+
return {SVE};
380+
}
381+
376382
DeclContext *getAsDeclContext() const {
377383
if (auto *fn = this->dyn_cast<AbstractFunctionDecl *>()) {
378384
return fn;
379385
} else if (auto *closure = this->dyn_cast<AbstractClosureExpr *>()) {
380386
return closure;
387+
} else if (auto *SVE = dyn_cast<SingleValueStmtExpr *>()) {
388+
return SVE->getDeclContext();
381389
} else {
382390
llvm_unreachable("unsupported kind");
383391
}
@@ -406,11 +414,13 @@ struct SyntacticElementContext
406414
}
407415
}
408416

409-
BraceStmt *getBody() const {
417+
Stmt *getStmt() const {
410418
if (auto *fn = this->dyn_cast<AbstractFunctionDecl *>()) {
411419
return fn->getBody();
412420
} else if (auto *closure = this->dyn_cast<AbstractClosureExpr *>()) {
413421
return closure->getBody();
422+
} else if (auto *SVE = dyn_cast<SingleValueStmtExpr *>()) {
423+
return SVE->getStmt();
414424
} else {
415425
llvm_unreachable("unsupported kind");
416426
}
@@ -1772,17 +1782,17 @@ class SyntacticElementSolutionApplication
17721782
#undef UNSUPPORTED_STMT
17731783

17741784
public:
1775-
/// Apply solution to the closure and return updated body.
1776-
ASTNode apply() {
1777-
auto body = visit(context.getBody());
1785+
/// Apply the solution to the context and return updated statement.
1786+
Stmt *apply() {
1787+
auto body = visit(context.getStmt());
17781788

17791789
// Since local functions can capture variables that are declared
17801790
// after them, let's type-check them after all of the pattern
17811791
// bindings have been resolved by applying solution to the body.
17821792
for (auto *func : LocalFuncs)
17831793
TypeChecker::typeCheckDecl(func);
17841794

1785-
return body;
1795+
return body ? body.get<Stmt *>() : nullptr;
17861796
}
17871797
};
17881798

@@ -1799,7 +1809,7 @@ class ResultBuilderRewriter : public SyntacticElementSolutionApplication {
17991809
Transform(transform) {}
18001810

18011811
bool apply() {
1802-
auto body = visit(context.getBody());
1812+
auto body = visit(context.getStmt());
18031813

18041814
if (!body || hadError)
18051815
return true;
@@ -2193,13 +2203,12 @@ bool ConstraintSystem::applySolutionToBody(Solution &solution,
21932203
solution, SyntacticElementContext::forFunctionRef(fn), resultTy,
21942204
rewriteTarget);
21952205

2196-
auto body = application.apply();
2206+
auto *body = application.apply();
21972207

21982208
if (!body || application.hadError)
21992209
return true;
22002210

2201-
fn.setTypecheckedBody(castToStmt<BraceStmt>(body),
2202-
fn.hasSingleExpressionBody());
2211+
fn.setTypecheckedBody(cast<BraceStmt>(body), fn.hasSingleExpressionBody());
22032212
return false;
22042213
}
22052214

0 commit comments

Comments
 (0)