Skip to content

Commit 2eba8dd

Browse files
authored
Merge pull request swiftlang#36047 from slavapestov/gsb-minimal-requirement-source-fix
GSB: Fix getMinimalConformanceSource() for top-level requirements in a requirement signature
2 parents ff7cf59 + 3d5ca29 commit 2eba8dd

File tree

2 files changed

+66
-23
lines changed

2 files changed

+66
-23
lines changed

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,19 @@ const RequirementSource *RequirementSource::getMinimalConformanceSource(
820820
switch (source->kind) {
821821
case ProtocolRequirement:
822822
case InferredProtocolRequirement: {
823+
// Special handling for top-level requirement signature requirements;
824+
// pretend the root type is the subject type as written in the
825+
// protocol, and not 'Self', so that we can consider this requirement
826+
// self-derived if it depends on one of the conformances that make
827+
// the root type valid.
828+
if (requirementSignatureSelfProto) {
829+
if (source->getProtocolDecl() == requirementSignatureSelfProto &&
830+
source->parent->kind == RequirementSource::RequirementSignatureSelf) {
831+
rootType = source->getAffectedType();
832+
return false;
833+
}
834+
}
835+
823836
// Note that we've seen a protocol requirement.
824837
sawProtocolRequirement = true;
825838

@@ -2144,37 +2157,40 @@ void EquivalenceClass::dump(llvm::raw_ostream &out,
21442157
}, [&]() {
21452158
out << ", ";
21462159
});
2147-
out << "\nConformances:";
2148-
interleave(conformsTo,
2149-
[&](const std::pair<
2150-
ProtocolDecl *,
2151-
std::vector<Constraint<ProtocolDecl *>>> &entry) {
2152-
out << entry.first->getNameStr();
2153-
},
2154-
[&] { out << ", "; });
2155-
out << "\nSame-type constraints:";
2156-
llvm::interleave(
2157-
sameTypeConstraints,
2158-
[&](const Constraint<Type> &constraint) {
2159-
out << "\n " << constraint.getSubjectDependentType({})
2160-
<< " == " << constraint.value;
2160+
out << "\n";
2161+
out << "Conformance constraints:\n";
2162+
for (auto entry : conformsTo) {
2163+
out << " " << entry.first->getNameStr() << "\n";
2164+
for (auto constraint : entry.second) {
2165+
constraint.source->dump(out, &builder->getASTContext().SourceMgr, 4);
2166+
if (constraint.source->isDerivedRequirement())
2167+
out << " [derived]";
2168+
out << "\n";
2169+
}
2170+
}
2171+
2172+
out << "Same-type constraints:\n";
2173+
for (auto constraint : sameTypeConstraints) {
2174+
out << " " << constraint.getSubjectDependentType({})
2175+
<< " == " << constraint.value << "\n";
2176+
constraint.source->dump(out, &builder->getASTContext().SourceMgr, 4);
2177+
if (constraint.source->isDerivedRequirement())
2178+
out << " [derived]";
2179+
out << "\n";
2180+
}
21612181

2162-
if (constraint.source->isDerivedRequirement())
2163-
out << " [derived]";
2164-
},
2165-
[&] { out << ", "; });
21662182
if (concreteType)
2167-
out << "\nConcrete type: " << concreteType.getString();
2183+
out << "Concrete type: " << concreteType.getString() << "\n";
21682184
if (superclass)
2169-
out << "\nSuperclass: " << superclass.getString();
2185+
out << "Superclass: " << superclass.getString() << "\n";
21702186
if (layout)
2171-
out << "\nLayout: " << layout.getString();
2187+
out << "Layout: " << layout.getString() << "\n";
21722188

21732189
if (!delayedRequirements.empty()) {
2174-
out << "\nDelayed requirements:";
2190+
out << "Delayed requirements:\n";
21752191
for (const auto &req : delayedRequirements) {
2176-
out << "\n ";
21772192
req.dump(out);
2193+
out << "\n";
21782194
}
21792195
}
21802196

test/Generics/sr13850.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %target-typecheck-verify-swift -debug-generic-signatures 2>&1 | %FileCheck %s
2+
3+
// https://bugs.swift.org/browse/SR-13850
4+
5+
// CHECK: Requirement signature: <Self where Self.A : P2>
6+
protocol P1 {
7+
associatedtype A: P2
8+
}
9+
10+
// CHECK: Requirement signature: <Self>
11+
protocol P2 {
12+
associatedtype A
13+
}
14+
15+
// Neither one of 'P3', 'P4' or 'f()' should have diagnosed
16+
// redundant conformance requirements.
17+
18+
// CHECK: Requirement signature: <Self where Self : P2, Self == Self.A.A, Self.A : P1>
19+
protocol P3 : P2 where Self.A: P1, Self.A.A == Self { }
20+
21+
// CHECK: Requirement signature: <Self where Self.X : P2, Self.X == Self.X.A.A, Self.X.A : P1>
22+
protocol P4 {
23+
associatedtype X where X : P2, X.A: P1, X.A.A == X
24+
}
25+
26+
// CHECK: Generic signature: <T where T : P2, T == T.A.A, T.A : P1>
27+
func f<T : P2>(_: T) where T.A : P1, T.A.A == T { }

0 commit comments

Comments
 (0)