Skip to content

Commit f24b763

Browse files
committed
[Effects handling] Simplify away getKindForFunctionBody().
Determining whether a given function or closure is throwing got a lot simpler since this code was written. Do the obvious thing.
1 parent fbbb2bb commit f24b763

File tree

1 file changed

+11
-26
lines changed

1 file changed

+11
-26
lines changed

lib/Sema/TypeCheckEffects.cpp

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -855,26 +855,6 @@ class Context {
855855
};
856856

857857
private:
858-
static Kind getKindForFunctionBody(Type type, unsigned numArgs) {
859-
/// Determine whether calling a function of the specified type with the
860-
/// specified number of arguments would throw.
861-
if (!type) return Kind::Handled;
862-
863-
assert(numArgs > 0);
864-
while (true) {
865-
auto fnType = type->getAs<AnyFunctionType>();
866-
if (!fnType) return Kind::Handled;
867-
868-
if (fnType->getExtInfo().isThrowing())
869-
return Kind::Handled;
870-
871-
if (--numArgs == 0)
872-
return Kind::NonThrowingFunction;
873-
874-
type = fnType->getResult();
875-
}
876-
}
877-
878858
static Context getContextForPatternBinding(PatternBindingDecl *pbd) {
879859
if (!pbd->isStatic() && pbd->getDeclContext()->isTypeContext()) {
880860
return Context(Kind::IVarInitializer);
@@ -924,8 +904,7 @@ class Context {
924904
}
925905
}
926906

927-
return Context(getKindForFunctionBody(
928-
D->getInterfaceType(), D->getNumCurryLevels()));
907+
return Context(D->hasThrows() ? Kind::Handled : Kind::NonThrowingFunction);
929908
}
930909

931910
static Context forDeferBody() {
@@ -948,10 +927,16 @@ class Context {
948927
}
949928

950929
static Context forClosure(AbstractClosureExpr *E) {
951-
auto kind = getKindForFunctionBody(E->getType(), 1);
952-
if (kind != Kind::Handled && isa<AutoClosureExpr>(E))
953-
kind = Kind::NonThrowingAutoClosure;
954-
return Context(kind);
930+
// Determine whether the closure has throwing function type.
931+
bool closureTypeThrows = true;
932+
if (auto closureType = E->getType()) {
933+
if (auto fnType = closureType->getAs<AnyFunctionType>())
934+
closureTypeThrows = fnType->isThrowing();
935+
}
936+
937+
return Context(closureTypeThrows ? Kind::Handled
938+
: isa<AutoClosureExpr>(E) ? Kind::NonThrowingAutoClosure
939+
: Kind::NonThrowingFunction);
955940
}
956941

957942
static Context forCatchPattern(CaseStmt *S) {

0 commit comments

Comments
 (0)