Skip to content

Commit 2cf7b3a

Browse files
authored
Merge pull request #70426 from DougGregor/catch-node-refinement
Tighten CatchNode from an AbstractClosureExpr to a ClosureExpr.
2 parents 5988608 + 05a4a82 commit 2cf7b3a

File tree

5 files changed

+35
-46
lines changed

5 files changed

+35
-46
lines changed

include/swift/AST/CatchNode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace swift {
2525
/// An AST node that represents a point where a thrown error can be caught and
2626
/// or rethrown, which includes functions do...catch statements.
2727
class CatchNode: public llvm::PointerUnion<
28-
AbstractFunctionDecl *, AbstractClosureExpr *, DoCatchStmt *, AnyTryExpr *
28+
AbstractFunctionDecl *, ClosureExpr *, DoCatchStmt *, AnyTryExpr *
2929
> {
3030
public:
3131
using PointerUnion::PointerUnion;

lib/AST/ASTScopeLookup.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,9 @@ static std::pair<CatchNode, const BraceStmtScope *>
684684
getCatchNode(const ASTScopeImpl *scope) {
685685
// Closures introduce a catch scope for errors initiated in their body.
686686
if (auto closureParams = dyn_cast<ClosureParametersScope>(scope)) {
687-
return getCatchNodeBody(
688-
scope, const_cast<AbstractClosureExpr *>(closureParams->closureExpr));
687+
if (auto closure = dyn_cast<ClosureExpr>(closureParams->closureExpr)) {
688+
return getCatchNodeBody(scope, const_cast<ClosureExpr *>(closure));
689+
}
689690
}
690691

691692
// Functions introduce a catch scope for errors initiated in their body.

lib/AST/Decl.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11718,17 +11718,15 @@ CatchNode::getThrownErrorTypeInContext(DeclContext *dc) const {
1171811718
return llvm::None;
1171911719
}
1172011720

11721-
if (auto abstractClosure = dyn_cast<AbstractClosureExpr *>()) {
11722-
if (abstractClosure->getType())
11723-
return abstractClosure->getEffectiveThrownType();
11721+
if (auto closure = dyn_cast<ClosureExpr *>()) {
11722+
if (closure->getType())
11723+
return closure->getEffectiveThrownType();
1172411724

11725-
if (auto closure = llvm::dyn_cast<ClosureExpr>(abstractClosure)) {
11726-
if (Type thrownType = closure->getExplicitThrownType()) {
11727-
if (thrownType->isNever())
11728-
return llvm::None;
11725+
if (Type thrownType = closure->getExplicitThrownType()) {
11726+
if (thrownType->isNever())
11727+
return llvm::None;
1172911728

11730-
return thrownType;
11731-
}
11729+
return thrownType;
1173211730
}
1173311731

1173411732
return llvm::None;
@@ -11786,12 +11784,8 @@ bool ExplicitCaughtTypeRequest::isCached() const {
1178611784
}
1178711785

1178811786
// Closures with explicitly-written thrown types need the result cached.
11789-
if (auto abstractClosure = catchNode.dyn_cast<AbstractClosureExpr *>()) {
11790-
if (auto closure = dyn_cast<ClosureExpr>(abstractClosure)) {
11791-
return closure->ThrownType != nullptr;
11792-
}
11793-
11794-
return false;
11787+
if (auto closure = catchNode.dyn_cast<ClosureExpr *>()) {
11788+
return closure->ThrownType != nullptr;
1179511789
}
1179611790

1179711791
// Do..catch with explicitly-written thrown types need the result cached.
@@ -11817,8 +11811,7 @@ llvm::Optional<Type> ExplicitCaughtTypeRequest::getCachedResult() const {
1181711811
return nonnullTypeOrNone(func->ThrownType.getType());
1181811812
}
1181911813

11820-
if (auto abstractClosure = catchNode.dyn_cast<AbstractClosureExpr *>()) {
11821-
auto closure = cast<ClosureExpr>(abstractClosure);
11814+
if (auto closure = catchNode.dyn_cast<ClosureExpr *>()) {
1182211815
if (closure->ThrownType) {
1182311816
return nonnullTypeOrNone(closure->ThrownType->getInstanceType());
1182411817
}
@@ -11841,8 +11834,7 @@ void ExplicitCaughtTypeRequest::cacheResult(Type type) const {
1184111834
return;
1184211835
}
1184311836

11844-
if (auto abstractClosure = catchNode.dyn_cast<AbstractClosureExpr *>()) {
11845-
auto closure = cast<ClosureExpr>(abstractClosure);
11837+
if (auto closure = catchNode.dyn_cast<ClosureExpr *>()) {
1184611838
if (closure->ThrownType)
1184711839
closure->ThrownType->setType(MetatypeType::get(type));
1184811840
else

lib/Sema/CSSyntacticElement.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -927,10 +927,8 @@ class SyntacticElementConstraintGenerator
927927
if (catchNode) {
928928
// FIXME: Introduce something like getThrownErrorTypeInContext() for the
929929
// constraint solver.
930-
if (auto abstractClosure = catchNode.dyn_cast<AbstractClosureExpr *>()) {
931-
if (auto closure = dyn_cast<ClosureExpr>(abstractClosure)) {
932-
errorType = cs.getClosureType(closure)->getThrownError();
933-
}
930+
if (auto closure = catchNode.dyn_cast<ClosureExpr *>()) {
931+
errorType = cs.getClosureType(closure)->getThrownError();
934932
}
935933

936934
if (!errorType)

lib/Sema/TypeCheckType.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5708,28 +5708,26 @@ Type ExplicitCaughtTypeRequest::evaluate(
57085708
}
57095709

57105710
// Closures
5711-
if (auto abstractClosure = catchNode.dyn_cast<AbstractClosureExpr *>()) {
5712-
if (auto closure = dyn_cast<ClosureExpr>(abstractClosure)) {
5713-
assert(dc == closure && "Key mismatch for explicit caught type request");
5714-
5715-
// Explicit thrown error type.
5716-
if (auto thrownTypeRepr = closure->getExplicitThrownTypeRepr()) {
5717-
if (!ctx.LangOpts.hasFeature(Feature::TypedThrows)) {
5718-
ctx.Diags.diagnose(thrownTypeRepr->getLoc(),
5719-
diag::experimental_typed_throws);
5720-
}
5721-
5722-
return TypeResolution::resolveContextualType(
5723-
thrownTypeRepr, dc,
5724-
TypeResolutionOptions(TypeResolverContext::None),
5725-
/*unboundTyOpener*/ nullptr, PlaceholderType::get,
5726-
/*packElementOpener*/ nullptr);
5711+
if (auto closure = catchNode.dyn_cast<ClosureExpr *>()) {
5712+
assert(dc == closure && "Key mismatch for explicit caught type request");
5713+
5714+
// Explicit thrown error type.
5715+
if (auto thrownTypeRepr = closure->getExplicitThrownTypeRepr()) {
5716+
if (!ctx.LangOpts.hasFeature(Feature::TypedThrows)) {
5717+
ctx.Diags.diagnose(thrownTypeRepr->getLoc(),
5718+
diag::experimental_typed_throws);
57275719
}
57285720

5729-
// Explicit 'throws' implies that this throws 'any Error'.
5730-
if (closure->getThrowsLoc().isValid()) {
5731-
return ctx.getErrorExistentialType();
5732-
}
5721+
return TypeResolution::resolveContextualType(
5722+
thrownTypeRepr, dc,
5723+
TypeResolutionOptions(TypeResolverContext::None),
5724+
/*unboundTyOpener*/ nullptr, PlaceholderType::get,
5725+
/*packElementOpener*/ nullptr);
5726+
}
5727+
5728+
// Explicit 'throws' implies that this throws 'any Error'.
5729+
if (closure->getThrowsLoc().isValid()) {
5730+
return ctx.getErrorExistentialType();
57335731
}
57345732

57355733
// Thrown error type will be inferred.

0 commit comments

Comments
 (0)