Skip to content

[6.0🍒] AST: fix isWrittenWithConstraints #74390

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 14, 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
17 changes: 12 additions & 5 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1809,18 +1809,25 @@ bool ExtensionDecl::isWrittenWithConstraints() const {
typeSig->getRequirementsWithInverses(typeReqs, typeInverseReqs);

// If the (non-inverse) requirements are different between the extension and
// the original type, it's written with constraints. Note that
// the extension can only add requirements, so we need only check the size
// (not the specific requirements).
if (extReqs.size() > typeReqs.size()) {
// the original type, it's written with constraints.
if (extReqs.size() != typeReqs.size()) {
return true;
}

assert(extReqs.size() == typeReqs.size());
// 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;
}

Expand Down
15 changes: 15 additions & 0 deletions validation-test/IRGen/issue-72719.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %target-swift-frontend -emit-ir -g %s > /dev/null

// https://github.com/apple/swift/issues/72719

protocol D {}
struct U: D, Equatable {}
class Q<T> {}
class R<V, E: D & Equatable> {}
extension R where E == U {
struct S<X> {}
static func a<T>(_: T) -> R {
let x = Q<S<T>>()
fatalError()
}
}