Skip to content

Commit f40de80

Browse files
committed
[Effects handling] Eliminate Context::Kind::NonThrowingAutoClosure.
1 parent f24b763 commit f40de80

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

lib/Sema/TypeCheckEffects.cpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -829,9 +829,6 @@ class Context {
829829
/// A rethrowing function.
830830
RethrowingFunction,
831831

832-
/// A non-throwing autoclosure.
833-
NonThrowingAutoClosure,
834-
835832
/// A default argument expression.
836833
DefaultArgument,
837834

@@ -864,12 +861,26 @@ class Context {
864861
}
865862

866863
Kind TheKind;
864+
Optional<AnyFunctionRef> Function;
867865
bool IsNonExhaustiveCatch = false;
868866
bool DiagnoseErrorOnTry = false;
869867
DeclContext *RethrowsDC = nullptr;
870868
InterpolatedStringLiteralExpr *InterpolatedString = nullptr;
871869

872-
explicit Context(Kind kind) : TheKind(kind) {}
870+
explicit Context(Kind kind, Optional<AnyFunctionRef> function = None)
871+
: TheKind(kind), Function(function) {}
872+
873+
/// Whether this is an autoclosure.
874+
bool isAutoClosure() const {
875+
if (!Function)
876+
return false;
877+
878+
auto closure = Function->getAbstractClosureExpr();
879+
if (!closure)
880+
return false;
881+
882+
return isa<AutoClosureExpr>(closure);
883+
}
873884

874885
public:
875886
static Context getHandled() {
@@ -883,7 +894,7 @@ class Context {
883894

884895
static Context forFunction(AbstractFunctionDecl *D) {
885896
if (D->getAttrs().hasAttribute<RethrowsAttr>()) {
886-
Context result(Kind::RethrowingFunction);
897+
Context result(Kind::RethrowingFunction, AnyFunctionRef(D));
887898
result.RethrowsDC = D;
888899
return result;
889900
}
@@ -904,7 +915,8 @@ class Context {
904915
}
905916
}
906917

907-
return Context(D->hasThrows() ? Kind::Handled : Kind::NonThrowingFunction);
918+
return Context(D->hasThrows() ? Kind::Handled : Kind::NonThrowingFunction,
919+
AnyFunctionRef(D));
908920
}
909921

910922
static Context forDeferBody() {
@@ -935,8 +947,8 @@ class Context {
935947
}
936948

937949
return Context(closureTypeThrows ? Kind::Handled
938-
: isa<AutoClosureExpr>(E) ? Kind::NonThrowingAutoClosure
939-
: Kind::NonThrowingFunction);
950+
: Kind::NonThrowingFunction,
951+
AnyFunctionRef(E));
940952
}
941953

942954
static Context forCatchPattern(CaseStmt *S) {
@@ -1086,7 +1098,8 @@ class Context {
10861098
// Allow the diagnostic to fire on the 'try' if we don't have
10871099
// anything else to say.
10881100
if (isTryCovered && !reason.isRethrowsCall() &&
1089-
getKind() == Kind::NonThrowingFunction) {
1101+
getKind() == Kind::NonThrowingFunction &&
1102+
!isAutoClosure()) {
10901103
DiagnoseErrorOnTry = true;
10911104
return;
10921105
}
@@ -1126,19 +1139,20 @@ class Context {
11261139
return;
11271140
}
11281141

1142+
if (isAutoClosure()) {
1143+
diagnoseThrowInLegalContext(Diags, E, isTryCovered, reason,
1144+
diag::throw_in_nonthrowing_autoclosure,
1145+
diag::throwing_call_in_nonthrowing_autoclosure,
1146+
diag::tryless_throwing_call_in_nonthrowing_autoclosure);
1147+
return;
1148+
}
1149+
11291150
diagnoseThrowInLegalContext(Diags, E, isTryCovered, reason,
11301151
diag::throw_in_nonthrowing_function,
11311152
diag::throwing_call_unhandled,
11321153
diag::tryless_throwing_call_unhandled);
11331154
return;
11341155

1135-
case Kind::NonThrowingAutoClosure:
1136-
diagnoseThrowInLegalContext(Diags, E, isTryCovered, reason,
1137-
diag::throw_in_nonthrowing_autoclosure,
1138-
diag::throwing_call_in_nonthrowing_autoclosure,
1139-
diag::tryless_throwing_call_in_nonthrowing_autoclosure);
1140-
return;
1141-
11421156
case Kind::EnumElementInitializer:
11431157
diagnoseThrowInIllegalContext(Diags, E, "an enum case raw value");
11441158
return;
@@ -1184,7 +1198,6 @@ class Context {
11841198
}
11851199
return;
11861200

1187-
case Kind::NonThrowingAutoClosure:
11881201
case Kind::EnumElementInitializer:
11891202
case Kind::GlobalVarInitializer:
11901203
case Kind::IVarInitializer:

0 commit comments

Comments
 (0)