@@ -829,9 +829,6 @@ class Context {
829
829
// / A rethrowing function.
830
830
RethrowingFunction,
831
831
832
- // / A non-throwing autoclosure.
833
- NonThrowingAutoClosure,
834
-
835
832
// / A default argument expression.
836
833
DefaultArgument,
837
834
@@ -864,12 +861,26 @@ class Context {
864
861
}
865
862
866
863
Kind TheKind;
864
+ Optional<AnyFunctionRef> Function;
867
865
bool IsNonExhaustiveCatch = false ;
868
866
bool DiagnoseErrorOnTry = false ;
869
867
DeclContext *RethrowsDC = nullptr ;
870
868
InterpolatedStringLiteralExpr *InterpolatedString = nullptr ;
871
869
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
+ }
873
884
874
885
public:
875
886
static Context getHandled () {
@@ -883,7 +894,7 @@ class Context {
883
894
884
895
static Context forFunction (AbstractFunctionDecl *D) {
885
896
if (D->getAttrs ().hasAttribute <RethrowsAttr>()) {
886
- Context result (Kind::RethrowingFunction);
897
+ Context result (Kind::RethrowingFunction, AnyFunctionRef (D) );
887
898
result.RethrowsDC = D;
888
899
return result;
889
900
}
@@ -904,7 +915,8 @@ class Context {
904
915
}
905
916
}
906
917
907
- return Context (D->hasThrows () ? Kind::Handled : Kind::NonThrowingFunction);
918
+ return Context (D->hasThrows () ? Kind::Handled : Kind::NonThrowingFunction,
919
+ AnyFunctionRef (D));
908
920
}
909
921
910
922
static Context forDeferBody () {
@@ -935,8 +947,8 @@ class Context {
935
947
}
936
948
937
949
return Context (closureTypeThrows ? Kind::Handled
938
- : isa<AutoClosureExpr>(E) ? Kind::NonThrowingAutoClosure
939
- : Kind::NonThrowingFunction );
950
+ : Kind::NonThrowingFunction,
951
+ AnyFunctionRef (E) );
940
952
}
941
953
942
954
static Context forCatchPattern (CaseStmt *S) {
@@ -1086,7 +1098,8 @@ class Context {
1086
1098
// Allow the diagnostic to fire on the 'try' if we don't have
1087
1099
// anything else to say.
1088
1100
if (isTryCovered && !reason.isRethrowsCall () &&
1089
- getKind () == Kind::NonThrowingFunction) {
1101
+ getKind () == Kind::NonThrowingFunction &&
1102
+ !isAutoClosure ()) {
1090
1103
DiagnoseErrorOnTry = true ;
1091
1104
return ;
1092
1105
}
@@ -1126,19 +1139,20 @@ class Context {
1126
1139
return ;
1127
1140
}
1128
1141
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
+
1129
1150
diagnoseThrowInLegalContext (Diags, E, isTryCovered, reason,
1130
1151
diag::throw_in_nonthrowing_function,
1131
1152
diag::throwing_call_unhandled,
1132
1153
diag::tryless_throwing_call_unhandled);
1133
1154
return ;
1134
1155
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
-
1142
1156
case Kind::EnumElementInitializer:
1143
1157
diagnoseThrowInIllegalContext (Diags, E, " an enum case raw value" );
1144
1158
return ;
@@ -1184,7 +1198,6 @@ class Context {
1184
1198
}
1185
1199
return ;
1186
1200
1187
- case Kind::NonThrowingAutoClosure:
1188
1201
case Kind::EnumElementInitializer:
1189
1202
case Kind::GlobalVarInitializer:
1190
1203
case Kind::IVarInitializer:
0 commit comments