Skip to content

Concurrency: Fix default type witness for AsyncSequence.Failure #74445

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 2 commits into from
Jun 17, 2024
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
53 changes: 25 additions & 28 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,8 @@ bool ExtensionDecl::hasValidParent() const {
return getDeclContext()->canBeParentOfExtension();
}

/// Does the extension's generic signature impose additional generic requirements
/// not stated on the extended nominal type itself?
bool ExtensionDecl::isConstrainedExtension() const {
auto nominal = getExtendedNominal();
if (!nominal)
Expand All @@ -1786,12 +1788,26 @@ bool ExtensionDecl::isConstrainedExtension() const {
return !typeSig->isEqual(extSig);
}

/// Is the extension written as an unconstrained extension? This is not the same
/// as isConstrainedExtension() in the case where the extended nominal type has
/// inverse requirements, because an extension of such a type introduces default
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's an example of an inverse requirement?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slava wrote a code example a few lines below this using ~Copyable

/// conformance requirements unless they're suppressed on the extension.
///
/// enum Optional<Wrapped> where Wrapped: ~Copyable {}
///
/// extension Optional {}
/// --> isConstrainedExtension(): true
/// --> isWrittenWithConstraints(): false
///
/// extension Optional where Wrapped: ~Copyable {}
/// --> isConstrainedExtension(): false
/// --> isWrittenWithConstraints(): true
bool ExtensionDecl::isWrittenWithConstraints() const {
auto nominal = getExtendedNominal();
if (!nominal)
return false;

// If there's no generic signature, then it's written without constraints.
// If there's no generic signature, then it's unconstrained.
CanGenericSignature extSig = getGenericSignature().getCanonicalSignature();
if (!extSig)
return false;
Expand All @@ -1808,37 +1824,18 @@ bool ExtensionDecl::isWrittenWithConstraints() const {
SmallVector<InverseRequirement, 2> typeInverseReqs;
typeSig->getRequirementsWithInverses(typeReqs, typeInverseReqs);

// If the (non-inverse) requirements are different between the extension and
// If the non-inverse requirements are different between the extension and
// the original type, it's written with constraints.
if (extReqs.size() != typeReqs.size()) {
if (extReqs != typeReqs)
return true;
}

// In case of equal number of constraints, we have to check the specific
// requirements. Extensions can end up with fewer requirements than the type
// extended, due to a same-type requirement in the extension.
//
// This mirrors the 'same' check in `ASTMangler::gatherGenericSignatureParts`
for (size_t i = 0; i < extReqs.size(); i++) {
if (extReqs[i] != typeReqs[i])
return true;
}

// If the type has no inverse requirements, there are no extra constraints
// to write.
if (typeInverseReqs.empty()) {
assert(extInverseReqs.empty() && "extension retroactively added inverse?");
return false;
}

// If the extension has no inverse requirements, then there are no constraints
// that need to be written down.
if (extInverseReqs.empty()) {
return false;
}
// If the extension has inverse requirements, then it's written with
// constraints.
if (!extInverseReqs.empty())
return true;

// We have inverses that need to be written out.
return true;
// Otherwise, the extension is written as an unconstrained extension.
return false;
}

bool ExtensionDecl::isInSameDefiningModule() const {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Concurrency/AsyncSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public protocol AsyncSequence<Element, Failure> {

/// The type of errors produced when iteration over the sequence fails.
@available(SwiftStdlib 6.0, *)
associatedtype Failure: Error = AsyncIterator.Failure
associatedtype Failure: Error = any Error
where AsyncIterator.Failure == Failure

/// Creates the asynchronous iterator that produces elements of this
Expand Down