@@ -290,12 +290,12 @@ static void tryDiagnoseUnnecessaryCastOverOptionSet(ASTContext &Ctx,
290
290
.fixItRemove (SourceRange (ME->getDotLoc (), E->getEndLoc ()));
291
291
}
292
292
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 ;
299
299
}
300
300
301
301
// / Check that a labeled statement doesn't shadow another statement with the
@@ -359,8 +359,7 @@ emitUnresolvedLabelDiagnostics(DiagnosticEngine &DE,
359
359
static LabeledStmt *findBreakOrContinueStmtTarget (
360
360
ASTContext &ctx, SourceFile *sourceFile,
361
361
SourceLoc loc, Identifier targetName, SourceLoc targetLoc,
362
- bool isContinue,
363
- Optional<AnyFunctionRef> enclosingFunc,
362
+ bool isContinue, DeclContext *dc,
364
363
ArrayRef<LabeledStmt *> oldActiveLabeledStmts) {
365
364
TopCollection<unsigned , LabeledStmt *> labelCorrections (3 );
366
365
@@ -415,7 +414,7 @@ static LabeledStmt *findBreakOrContinueStmtTarget(
415
414
}
416
415
417
416
// If we're in a defer, produce a tailored diagnostic.
418
- if (isDefer (enclosingFunc )) {
417
+ if (isDefer (dc )) {
419
418
ctx.Diags .diagnose (
420
419
loc, diag::jump_out_of_defer, isContinue ? " continue" : " break" );
421
420
return nullptr ;
@@ -452,10 +451,6 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
452
451
public:
453
452
ASTContext &Ctx;
454
453
455
- // / This is the current function or closure being checked.
456
- // / This is null for top level code.
457
- Optional<AnyFunctionRef> TheFunc;
458
-
459
454
// / DC - This is the current DeclContext.
460
455
DeclContext *DC;
461
456
@@ -464,11 +459,13 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
464
459
465
460
// / The level of loop nesting. 'break' and 'continue' are valid only in scopes
466
461
// / where this is greater than one.
462
+ // / FIXME: Only required because EnableASTScopeLookup can be false
467
463
SmallVector<LabeledStmt*, 2 > ActiveLabeledStmts;
468
464
469
465
// / The destination block for a 'fallthrough' statement. Null if the switch
470
466
// / scope depth is zero or if we are checking the final 'case' of the current
471
467
// / switch.
468
+ // / FIXME: Only required because EnableASTScopeLookup can be false
472
469
CaseStmt /* nullable*/ *FallthroughSource = nullptr ;
473
470
CaseStmt /* nullable*/ *FallthroughDest = nullptr ;
474
471
@@ -536,20 +533,14 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
536
533
}
537
534
};
538
535
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) { }
546
537
547
538
// ===--------------------------------------------------------------------===//
548
539
// Helper Functions.
549
540
// ===--------------------------------------------------------------------===//
550
541
551
542
bool isInDefer () const {
552
- return isDefer (TheFunc );
543
+ return isDefer (DC );
553
544
}
554
545
555
546
template <typename StmtTy>
@@ -581,6 +572,8 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
581
572
Stmt *visitBraceStmt (BraceStmt *BS);
582
573
583
574
Stmt *visitReturnStmt (ReturnStmt *RS) {
575
+ auto TheFunc = AnyFunctionRef::fromDeclContext (DC);
576
+
584
577
if (!TheFunc.hasValue ()) {
585
578
getASTContext ().Diags .diagnose (RS->getReturnLoc (),
586
579
diag::return_invalid_outside_func);
@@ -703,6 +696,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
703
696
}
704
697
705
698
SmallVector<AnyFunctionType::Yield, 4 > buffer;
699
+ auto TheFunc = AnyFunctionRef::fromDeclContext (DC);
706
700
auto yieldResults = TheFunc->getBodyYieldResults (buffer);
707
701
708
702
auto yieldExprs = YS->getMutableYields ();
@@ -865,7 +859,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
865
859
if (auto target = findBreakOrContinueStmtTarget (
866
860
getASTContext (), DC->getParentSourceFile (), S->getLoc (),
867
861
S->getTargetName (), S->getTargetLoc (), /* isContinue=*/ false ,
868
- TheFunc , ActiveLabeledStmts)) {
862
+ DC , ActiveLabeledStmts)) {
869
863
S->setTarget (target);
870
864
}
871
865
@@ -876,7 +870,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
876
870
if (auto target = findBreakOrContinueStmtTarget (
877
871
getASTContext (), DC->getParentSourceFile (), S->getLoc (),
878
872
S->getTargetName (), S->getTargetLoc (), /* isContinue=*/ true ,
879
- TheFunc , ActiveLabeledStmts)) {
873
+ DC , ActiveLabeledStmts)) {
880
874
S->setTarget (target);
881
875
}
882
876
0 commit comments