Skip to content

Erase existential thrown error types #72194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/Sema/TypeCheckEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,24 @@ static bool isRethrowingDueToAsyncSequence(DeclContext *rethrowsDC) {
return true;
}

/// Type-erase the opened archetypes in the given type, if there is one.
static Type typeEraseOpenedArchetypes(Type type) {
if (!type || !type->hasOpenedExistential())
return type;

const OpenedArchetypeType *root = nullptr;
type.visit([&](Type type) {
if (auto opened = dyn_cast<OpenedArchetypeType>(type.getPointer())) {
root = opened->getRoot();
}
});

if (!root)
return type;

return constraints::typeEraseOpenedArchetypesWithRoot(type, root);
}

/// A type expressing the result of classifying whether a call or function
/// throws or is async.
class Classification {
Expand Down Expand Up @@ -928,7 +946,7 @@ class Classification {

result.ThrowKind = conditionalKind;
result.ThrowReason = reason;
result.ThrownError = thrownError;
result.ThrownError = typeEraseOpenedArchetypes(thrownError);
return result;
}

Expand Down
18 changes: 18 additions & 0 deletions test/Concurrency/async_sequence_existential.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %target-swift-frontend -disable-availability-checking %s -emit-sil -o /dev/null -verify

// RUN: %target-swift-frontend -disable-availability-checking %s -dump-ast 2>&1 | %FileCheck %s

// REQUIRES: concurrency

extension Error {
func printMe() { }
}

func test(seq: any AsyncSequence) async {
// CHECK: "error" interface type="any Error"
do {
for try await _ in seq { }
} catch {
error.printMe()
}
}