Skip to content

Commit 523f807

Browse files
authored
Merge pull request #72194 from DougGregor/thrown-error-opened-archetypes
Erase existential thrown error types
2 parents 78273b2 + 11e7a69 commit 523f807

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/Sema/TypeCheckEffects.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,24 @@ static bool isRethrowingDueToAsyncSequence(DeclContext *rethrowsDC) {
800800
return true;
801801
}
802802

803+
/// Type-erase the opened archetypes in the given type, if there is one.
804+
static Type typeEraseOpenedArchetypes(Type type) {
805+
if (!type || !type->hasOpenedExistential())
806+
return type;
807+
808+
const OpenedArchetypeType *root = nullptr;
809+
type.visit([&](Type type) {
810+
if (auto opened = dyn_cast<OpenedArchetypeType>(type.getPointer())) {
811+
root = opened->getRoot();
812+
}
813+
});
814+
815+
if (!root)
816+
return type;
817+
818+
return constraints::typeEraseOpenedArchetypesWithRoot(type, root);
819+
}
820+
803821
/// A type expressing the result of classifying whether a call or function
804822
/// throws or is async.
805823
class Classification {
@@ -928,7 +946,7 @@ class Classification {
928946

929947
result.ThrowKind = conditionalKind;
930948
result.ThrowReason = reason;
931-
result.ThrownError = thrownError;
949+
result.ThrownError = typeEraseOpenedArchetypes(thrownError);
932950
return result;
933951
}
934952

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-swift-frontend -disable-availability-checking %s -emit-sil -o /dev/null -verify
2+
3+
// RUN: %target-swift-frontend -disable-availability-checking %s -dump-ast 2>&1 | %FileCheck %s
4+
5+
// REQUIRES: concurrency
6+
7+
extension Error {
8+
func printMe() { }
9+
}
10+
11+
func test(seq: any AsyncSequence) async {
12+
// CHECK: "error" interface type="any Error"
13+
do {
14+
for try await _ in seq { }
15+
} catch {
16+
error.printMe()
17+
}
18+
}

0 commit comments

Comments
 (0)