Skip to content

Commit a115ad9

Browse files
committed
[AST] Record "separately checked" in ClosureExpr.
Reverse the polarity of the "checked in context" bit for ClosureExpr to "separately checked", which simplifies the AST walker logic (to "should we walk separately type-checked closure bodies?") and eliminates single-expression closures as a separate case to consider.
1 parent 7eae5f2 commit a115ad9

File tree

8 files changed

+38
-39
lines changed

8 files changed

+38
-39
lines changed

include/swift/AST/ASTWalker.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,12 @@ class ASTWalker {
216216
virtual bool shouldWalkIntoLazyInitializers() { return true; }
217217

218218
/// This method configures whether the walker should visit the body of a
219-
/// non-single expression closure.
219+
/// closure that was checked separately from its enclosing expression.
220220
///
221221
/// For work that is performed for every top-level expression, this should
222222
/// be overridden to return false, to avoid duplicating work or visiting
223223
/// bodies of closures that have not yet been type checked.
224-
virtual bool shouldWalkIntoNonSingleExpressionClosure(ClosureExpr *) {
224+
virtual bool shouldWalkIntoSeparatelyCheckedClosure(ClosureExpr *) {
225225
return true;
226226
}
227227

include/swift/AST/Expr.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3784,7 +3784,7 @@ class ClosureExpr : public AbstractClosureExpr {
37843784

37853785
/// The explicitly-specified result type.
37863786
llvm::PointerIntPair<TypeExpr *, 1, bool>
3787-
ExplicitResultTypeAndEnclosingChecked;
3787+
ExplicitResultTypeAndSeparatelyChecked;
37883788

37893789
/// The body of the closure, along with a bit indicating whether it
37903790
/// was originally just a single expression.
@@ -3799,7 +3799,7 @@ class ClosureExpr : public AbstractClosureExpr {
37993799
BracketRange(bracketRange),
38003800
CapturedSelfDecl(capturedSelfDecl),
38013801
ThrowsLoc(throwsLoc), ArrowLoc(arrowLoc), InLoc(inLoc),
3802-
ExplicitResultTypeAndEnclosingChecked(explicitResultType, false),
3802+
ExplicitResultTypeAndSeparatelyChecked(explicitResultType, false),
38033803
Body(nullptr) {
38043804
setParameterList(params);
38053805
Bits.ClosureExpr.HasAnonymousClosureVars = false;
@@ -3854,14 +3854,14 @@ class ClosureExpr : public AbstractClosureExpr {
38543854

38553855
Type getExplicitResultType() const {
38563856
assert(hasExplicitResultType() && "No explicit result type");
3857-
return ExplicitResultTypeAndEnclosingChecked.getPointer()
3857+
return ExplicitResultTypeAndSeparatelyChecked.getPointer()
38583858
->getInstanceType();
38593859
}
38603860
void setExplicitResultType(Type ty);
38613861

38623862
TypeRepr *getExplicitResultTypeRepr() const {
38633863
assert(hasExplicitResultType() && "No explicit result type");
3864-
return ExplicitResultTypeAndEnclosingChecked.getPointer()
3864+
return ExplicitResultTypeAndSeparatelyChecked.getPointer()
38653865
->getTypeRepr();
38663866
}
38673867

@@ -3904,14 +3904,14 @@ class ClosureExpr : public AbstractClosureExpr {
39043904
/// captured non-weakly).
39053905
bool capturesSelfEnablingImplictSelf() const;
39063906

3907-
/// Whether this closure's body was type checked within the enclosing
3908-
/// context.
3909-
bool wasTypeCheckedInEnclosingContext() const {
3910-
return ExplicitResultTypeAndEnclosingChecked.getInt();
3907+
/// Whether this closure's body was type checked separately from its
3908+
/// enclosing expression.
3909+
bool wasSeparatelyTypeChecked() const {
3910+
return ExplicitResultTypeAndSeparatelyChecked.getInt();
39113911
}
39123912

3913-
void setTypeCheckedInEnclosingContext(bool flag = true) {
3914-
ExplicitResultTypeAndEnclosingChecked.setInt(flag);
3913+
void setSeparatelyTypeChecked(bool flag = true) {
3914+
ExplicitResultTypeAndSeparatelyChecked.setInt(flag);
39153915
}
39163916

39173917
static bool classof(const Expr *E) {

lib/AST/ASTWalker.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -815,10 +815,10 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
815815
return nullptr;
816816
}
817817

818-
// Always walk into closures with a single-expression body; for those
819-
// that don't have a single-expression body, ask the walker first.
820-
if (!expr->hasSingleExpressionBody() &&
821-
!Walker.shouldWalkIntoNonSingleExpressionClosure(expr))
818+
// If the closure was separately type checked and we don't want to
819+
// visit separately-checked closure bodies, bail out now.
820+
if (expr->wasSeparatelyTypeChecked() &&
821+
!Walker.shouldWalkIntoSeparatelyCheckedClosure(expr))
822822
return expr;
823823

824824
// Handle other closures.

lib/AST/Expr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1910,7 +1910,7 @@ bool ClosureExpr::capturesSelfEnablingImplictSelf() const {
19101910

19111911
void ClosureExpr::setExplicitResultType(Type ty) {
19121912
assert(ty && !ty->hasTypeVariable());
1913-
ExplicitResultTypeAndEnclosingChecked.getPointer()
1913+
ExplicitResultTypeAndSeparatelyChecked.getPointer()
19141914
->setType(MetatypeType::get(ty));
19151915
}
19161916

lib/Sema/CSClosure.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ SolutionApplicationToFunctionResult ConstraintSystem::applySolution(
333333

334334
fn.setBody(newBody, /*isSingleExpression=*/false);
335335
if (closure) {
336-
closure->setTypeCheckedInEnclosingContext();
337336
solution.setExprTypes(closure);
338337
}
339338

@@ -349,7 +348,6 @@ SolutionApplicationToFunctionResult ConstraintSystem::applySolution(
349348
solution, closure, closureFnType->getResult(), rewriteTarget);
350349
application.visit(fn.getBody());
351350

352-
closure->setTypeCheckedInEnclosingContext();
353351
return SolutionApplicationToFunctionResult::Success;
354352
}
355353

lib/Sema/MiscDiagnostics.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC,
8686

8787
bool walkToDeclPre(Decl *D) override {
8888
if (auto *closure = dyn_cast<ClosureExpr>(D->getDeclContext()))
89-
return closure->wasTypeCheckedInEnclosingContext();
89+
return !closure->wasSeparatelyTypeChecked();
9090
return false;
9191
}
9292

9393
bool walkToTypeReprPre(TypeRepr *T) override { return true; }
9494

95-
bool shouldWalkIntoNonSingleExpressionClosure(ClosureExpr *expr) override {
96-
return expr->wasTypeCheckedInEnclosingContext();
95+
bool shouldWalkIntoSeparatelyCheckedClosure(ClosureExpr *expr) override {
96+
return false;
9797
}
9898

9999
bool shouldWalkIntoTapExpression() override { return false; }
@@ -1290,8 +1290,8 @@ static void diagRecursivePropertyAccess(const Expr *E, const DeclContext *DC) {
12901290
cast<VarDecl>(DRE->getDecl())->isSelfParameter();
12911291
}
12921292

1293-
bool shouldWalkIntoNonSingleExpressionClosure(ClosureExpr *expr) override {
1294-
return expr->wasTypeCheckedInEnclosingContext();
1293+
bool shouldWalkIntoSeparatelyCheckedClosure(ClosureExpr *expr) override {
1294+
return false;
12951295
}
12961296

12971297
bool shouldWalkIntoTapExpression() override { return false; }
@@ -1459,12 +1459,12 @@ static void diagnoseImplicitSelfUseInClosure(const Expr *E,
14591459
// Don't walk into nested decls.
14601460
bool walkToDeclPre(Decl *D) override {
14611461
if (auto *closure = dyn_cast<ClosureExpr>(D->getDeclContext()))
1462-
return closure->wasTypeCheckedInEnclosingContext();
1462+
return !closure->wasSeparatelyTypeChecked();
14631463
return false;
14641464
}
14651465

1466-
bool shouldWalkIntoNonSingleExpressionClosure(ClosureExpr *expr) override {
1467-
return expr->wasTypeCheckedInEnclosingContext();
1466+
bool shouldWalkIntoSeparatelyCheckedClosure(ClosureExpr *expr) override {
1467+
return false;
14681468
}
14691469

14701470
bool shouldWalkIntoTapExpression() override { return false; }
@@ -3265,8 +3265,8 @@ static void checkStmtConditionTrailingClosure(ASTContext &ctx, const Expr *E) {
32653265
public:
32663266
DiagnoseWalker(ASTContext &ctx) : Ctx(ctx) { }
32673267

3268-
bool shouldWalkIntoNonSingleExpressionClosure(ClosureExpr *expr) override {
3269-
return expr->wasTypeCheckedInEnclosingContext();
3268+
bool shouldWalkIntoSeparatelyCheckedClosure(ClosureExpr *expr) override {
3269+
return false;
32703270
}
32713271

32723272
bool shouldWalkIntoTapExpression() override { return false; }
@@ -3415,8 +3415,8 @@ class ObjCSelectorWalker : public ASTWalker {
34153415
ObjCSelectorWalker(const DeclContext *dc, Type selectorTy)
34163416
: Ctx(dc->getASTContext()), DC(dc), SelectorTy(selectorTy) { }
34173417

3418-
bool shouldWalkIntoNonSingleExpressionClosure(ClosureExpr *expr) override {
3419-
return expr->wasTypeCheckedInEnclosingContext();
3418+
bool shouldWalkIntoSeparatelyCheckedClosure(ClosureExpr *expr) override {
3419+
return false;
34203420
}
34213421

34223422
bool shouldWalkIntoTapExpression() override { return false; }
@@ -4155,8 +4155,8 @@ static void diagnoseUnintendedOptionalBehavior(const Expr *E,
41554155
}
41564156
}
41574157

4158-
bool shouldWalkIntoNonSingleExpressionClosure(ClosureExpr *expr) override {
4159-
return expr->wasTypeCheckedInEnclosingContext();
4158+
bool shouldWalkIntoSeparatelyCheckedClosure(ClosureExpr *expr) override {
4159+
return false;
41604160
}
41614161

41624162
bool shouldWalkIntoTapExpression() override { return false; }
@@ -4231,8 +4231,8 @@ static void diagnoseDeprecatedWritableKeyPath(const Expr *E,
42314231
}
42324232
}
42334233

4234-
bool shouldWalkIntoNonSingleExpressionClosure(ClosureExpr *expr) override {
4235-
return expr->wasTypeCheckedInEnclosingContext();
4234+
bool shouldWalkIntoSeparatelyCheckedClosure(ClosureExpr *expr) override {
4235+
return false;
42364236
}
42374237

42384238
bool shouldWalkIntoTapExpression() override { return false; }

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ static void findAvailabilityFixItNodes(SourceRange ReferenceRange,
11291129
if (Expr *ParentExpr = Parent.getAsExpr()) {
11301130
auto *ParentClosure = dyn_cast<ClosureExpr>(ParentExpr);
11311131
if (!ParentClosure ||
1132-
!ParentClosure->wasTypeCheckedInEnclosingContext()) {
1132+
ParentClosure->wasSeparatelyTypeChecked()) {
11331133
return false;
11341134
}
11351135
} else if (auto *ParentStmt = Parent.getAsStmt()) {
@@ -2343,8 +2343,8 @@ class AvailabilityWalker : public ASTWalker {
23432343
return true;
23442344
}
23452345

2346-
bool shouldWalkIntoNonSingleExpressionClosure(ClosureExpr *expr) override {
2347-
return expr->wasTypeCheckedInEnclosingContext();
2346+
bool shouldWalkIntoSeparatelyCheckedClosure(ClosureExpr *expr) override {
2347+
return false;
23482348
}
23492349

23502350
bool shouldWalkIntoTapExpression() override { return false; }

lib/Sema/TypeCheckStmt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ namespace {
143143
// If the closure was type checked within its enclosing context,
144144
// we need to walk into it with a new sequence.
145145
// Otherwise, it'll have been separately type-checked.
146-
if (CE->wasTypeCheckedInEnclosingContext())
146+
if (!CE->wasSeparatelyTypeChecked())
147147
CE->getBody()->walk(ContextualizeClosures(CE));
148148

149149
TypeChecker::computeCaptures(CE);
@@ -1976,6 +1976,7 @@ bool TypeChecker::typeCheckClosureBody(ClosureExpr *closure) {
19761976
if (body) {
19771977
closure->setBody(body, closure->hasSingleExpressionBody());
19781978
}
1979+
closure->setSeparatelyTypeChecked();
19791980
return HadError;
19801981
}
19811982

0 commit comments

Comments
 (0)