@@ -857,6 +857,10 @@ class Context {
857
857
Kind TheKind;
858
858
Optional<AnyFunctionRef> Function;
859
859
bool HandlesErrors = false ;
860
+
861
+ // / Whether error-handling queries should ignore the function context, e.g.,
862
+ // / for autoclosure and rethrows checks.
863
+ bool ErrorHandlingIgnoresFunction = false ;
860
864
bool IsNonExhaustiveCatch = false ;
861
865
bool DiagnoseErrorOnTry = false ;
862
866
InterpolatedStringLiteralExpr *InterpolatedString = nullptr ;
@@ -876,6 +880,9 @@ class Context {
876
880
if (!HandlesErrors)
877
881
return false ;
878
882
883
+ if (ErrorHandlingIgnoresFunction)
884
+ return false ;
885
+
879
886
if (!Function)
880
887
return false ;
881
888
@@ -891,17 +898,16 @@ class Context {
891
898
if (!Function)
892
899
return false ;
893
900
901
+ if (ErrorHandlingIgnoresFunction)
902
+ return false ;
903
+
894
904
auto closure = Function->getAbstractClosureExpr ();
895
905
if (!closure)
896
906
return false ;
897
907
898
908
return isa<AutoClosureExpr>(closure);
899
909
}
900
910
901
- static Context getHandled () {
902
- return Context (/* handlesErrors=*/ true , None);
903
- }
904
-
905
911
static Context forTopLevelCode (TopLevelCodeDecl *D) {
906
912
// Top-level code implicitly handles errors and 'async' calls.
907
913
return Context (/* handlesErrors=*/ true , None);
@@ -976,6 +982,15 @@ class Context {
976
982
return copy;
977
983
}
978
984
985
+ // / Form a subcontext that handles all errors, e.g., for the body of a
986
+ // / do-catch with exhaustive catch clauses.
987
+ Context withHandlesErrors () const {
988
+ Context copy = *this ;
989
+ copy.HandlesErrors = true ;
990
+ copy.ErrorHandlingIgnoresFunction = true ;
991
+ return copy;
992
+ }
993
+
979
994
Kind getKind () const { return TheKind; }
980
995
981
996
bool handlesNothing () const {
@@ -1416,8 +1431,8 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
1416
1431
}
1417
1432
1418
1433
ThrowingKind checkExhaustiveDoBody (DoCatchStmt *S) {
1419
- // This is a handled context.
1420
- ContextScope scope (*this , Context::getHandled ());
1434
+ // This is a context where errors are handled .
1435
+ ContextScope scope (*this , CurContext. withHandlesErrors ());
1421
1436
assert (!Flags.has (ContextFlags::IsInTry) && " do/catch within try?" );
1422
1437
scope.resetCoverageForDoCatch ();
1423
1438
@@ -1474,9 +1489,9 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
1474
1489
if (doThrowingKind != ThrowingKind::Throws &&
1475
1490
CurContext.isRethrows ()) {
1476
1491
// If this catch clause is reachable at all, it's because a function
1477
- // parameter throws. So let's temporarily set our context to Handled so
1478
- // the catch body is allowed to throw.
1479
- CurContext = Context::getHandled ();
1492
+ // parameter throws. So let's temporarily state that the body is allowed
1493
+ // to throw.
1494
+ CurContext = CurContext. withHandlesErrors ();
1480
1495
}
1481
1496
1482
1497
// The catch body just happens in the enclosing context.
@@ -1661,7 +1676,7 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
1661
1676
1662
1677
ShouldRecurse_t checkForceTry (ForceTryExpr *E) {
1663
1678
// Walk the operand. 'try!' handles errors.
1664
- ContextScope scope (*this , Context::getHandled ());
1679
+ ContextScope scope (*this , CurContext. withHandlesErrors ());
1665
1680
scope.enterTry ();
1666
1681
1667
1682
E->getSubExpr ()->walk (*this );
@@ -1675,7 +1690,7 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
1675
1690
1676
1691
ShouldRecurse_t checkOptionalTry (OptionalTryExpr *E) {
1677
1692
// Walk the operand. 'try?' handles errors.
1678
- ContextScope scope (*this , Context::getHandled ());
1693
+ ContextScope scope (*this , CurContext. withHandlesErrors ());
1679
1694
scope.enterTry ();
1680
1695
1681
1696
E->getSubExpr ()->walk (*this );
0 commit comments