Skip to content

Commit cc17aef

Browse files
committed
[Statement checker] Remove redundant state.
Remove the `TheFunc` state, which is used only in a few places and can be trivially recomputed.
1 parent 5dc9b1e commit cc17aef

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

include/swift/AST/AnyFunctionRef.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ class AnyFunctionRef {
5353
}
5454
}
5555

56+
/// Construct an AnyFunctionRef from a decl context that might be
57+
/// some sort of function.
58+
static Optional<AnyFunctionRef> fromDeclContext(DeclContext *dc) {
59+
if (auto fn = dyn_cast<AbstractFunctionDecl>(dc)) {
60+
return AnyFunctionRef(fn);
61+
}
62+
63+
if (auto ace = dyn_cast<AbstractClosureExpr>(dc)) {
64+
return AnyFunctionRef(ace);
65+
}
66+
67+
return None;
68+
}
69+
5670
CaptureInfo getCaptureInfo() const {
5771
if (auto *AFD = TheFunction.dyn_cast<AbstractFunctionDecl *>())
5872
return AFD->getCaptureInfo();

lib/Sema/TypeCheckStmt.cpp

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,12 @@ static void tryDiagnoseUnnecessaryCastOverOptionSet(ASTContext &Ctx,
290290
.fixItRemove(SourceRange(ME->getDotLoc(), E->getEndLoc()));
291291
}
292292

293-
/// Whether the given enclosing function is a "defer" body.
294-
static bool isDefer(Optional<AnyFunctionRef> enclosingFunc) {
295-
if (!enclosingFunc.hasValue()) return false;
296-
auto *FD = dyn_cast_or_null<FuncDecl>
297-
(enclosingFunc.getValue().getAbstractFunctionDecl());
298-
return FD && FD->isDeferBody();
293+
/// Whether the given enclosing context is a "defer" body.
294+
static bool isDefer(DeclContext *dc) {
295+
if (auto *func = dyn_cast<FuncDecl>(dc))
296+
return func->isDeferBody();
297+
298+
return false;
299299
}
300300

301301
/// Check that a labeled statement doesn't shadow another statement with the
@@ -359,8 +359,7 @@ emitUnresolvedLabelDiagnostics(DiagnosticEngine &DE,
359359
static LabeledStmt *findBreakOrContinueStmtTarget(
360360
ASTContext &ctx, SourceFile *sourceFile,
361361
SourceLoc loc, Identifier targetName, SourceLoc targetLoc,
362-
bool isContinue,
363-
Optional<AnyFunctionRef> enclosingFunc,
362+
bool isContinue, DeclContext *dc,
364363
ArrayRef<LabeledStmt *> oldActiveLabeledStmts) {
365364
TopCollection<unsigned, LabeledStmt *> labelCorrections(3);
366365

@@ -415,7 +414,7 @@ static LabeledStmt *findBreakOrContinueStmtTarget(
415414
}
416415

417416
// If we're in a defer, produce a tailored diagnostic.
418-
if (isDefer(enclosingFunc)) {
417+
if (isDefer(dc)) {
419418
ctx.Diags.diagnose(
420419
loc, diag::jump_out_of_defer, isContinue ? "continue": "break");
421420
return nullptr;
@@ -452,10 +451,6 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
452451
public:
453452
ASTContext &Ctx;
454453

455-
/// This is the current function or closure being checked.
456-
/// This is null for top level code.
457-
Optional<AnyFunctionRef> TheFunc;
458-
459454
/// DC - This is the current DeclContext.
460455
DeclContext *DC;
461456

@@ -464,11 +459,13 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
464459

465460
/// The level of loop nesting. 'break' and 'continue' are valid only in scopes
466461
/// where this is greater than one.
462+
/// FIXME: Only required because EnableASTScopeLookup can be false
467463
SmallVector<LabeledStmt*, 2> ActiveLabeledStmts;
468464

469465
/// The destination block for a 'fallthrough' statement. Null if the switch
470466
/// scope depth is zero or if we are checking the final 'case' of the current
471467
/// switch.
468+
/// FIXME: Only required because EnableASTScopeLookup can be false
472469
CaseStmt /*nullable*/ *FallthroughSource = nullptr;
473470
CaseStmt /*nullable*/ *FallthroughDest = nullptr;
474471

@@ -536,20 +533,14 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
536533
}
537534
};
538535

539-
StmtChecker(DeclContext *DC)
540-
: Ctx(DC->getASTContext()), TheFunc(), DC(DC) {
541-
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(DC))
542-
TheFunc = AFD;
543-
else if (auto *CE = dyn_cast<ClosureExpr>(DC))
544-
TheFunc = CE;
545-
}
536+
StmtChecker(DeclContext *DC) : Ctx(DC->getASTContext()), DC(DC) { }
546537

547538
//===--------------------------------------------------------------------===//
548539
// Helper Functions.
549540
//===--------------------------------------------------------------------===//
550541

551542
bool isInDefer() const {
552-
return isDefer(TheFunc);
543+
return isDefer(DC);
553544
}
554545

555546
template<typename StmtTy>
@@ -581,6 +572,8 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
581572
Stmt *visitBraceStmt(BraceStmt *BS);
582573

583574
Stmt *visitReturnStmt(ReturnStmt *RS) {
575+
auto TheFunc = AnyFunctionRef::fromDeclContext(DC);
576+
584577
if (!TheFunc.hasValue()) {
585578
getASTContext().Diags.diagnose(RS->getReturnLoc(),
586579
diag::return_invalid_outside_func);
@@ -703,6 +696,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
703696
}
704697

705698
SmallVector<AnyFunctionType::Yield, 4> buffer;
699+
auto TheFunc = AnyFunctionRef::fromDeclContext(DC);
706700
auto yieldResults = TheFunc->getBodyYieldResults(buffer);
707701

708702
auto yieldExprs = YS->getMutableYields();
@@ -865,7 +859,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
865859
if (auto target = findBreakOrContinueStmtTarget(
866860
getASTContext(), DC->getParentSourceFile(), S->getLoc(),
867861
S->getTargetName(), S->getTargetLoc(), /*isContinue=*/false,
868-
TheFunc, ActiveLabeledStmts)) {
862+
DC, ActiveLabeledStmts)) {
869863
S->setTarget(target);
870864
}
871865

@@ -876,7 +870,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
876870
if (auto target = findBreakOrContinueStmtTarget(
877871
getASTContext(), DC->getParentSourceFile(), S->getLoc(),
878872
S->getTargetName(), S->getTargetLoc(), /*isContinue=*/true,
879-
TheFunc, ActiveLabeledStmts)) {
873+
DC, ActiveLabeledStmts)) {
880874
S->setTarget(target);
881875
}
882876

0 commit comments

Comments
 (0)