Skip to content

Commit d64fb39

Browse files
committed
RequirementMachine: Introduce conditional requirements in desugaring
1 parent 48ca87d commit d64fb39

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,23 @@ static void desugarConformanceRequirement(Type subjectType, Type constraintType,
136136
// Fast path.
137137
if (constraintType->is<ProtocolType>()) {
138138
if (!subjectType->isTypeParameter()) {
139-
// FIXME: Check conformance, diagnose redundancy or conflict upstream
139+
// Check if the subject type actually conforms.
140+
auto *protoDecl = constraintType->castTo<ProtocolType>()->getDecl();
141+
auto *module = protoDecl->getParentModule();
142+
auto conformance = module->lookupConformance(subjectType, protoDecl);
143+
if (conformance.isInvalid()) {
144+
// FIXME: Diagnose a conflict.
145+
return;
146+
}
147+
148+
// FIXME: Diagnose a redundancy.
149+
assert(conformance.isConcrete());
150+
auto *concrete = conformance.getConcrete();
151+
152+
// Introduce conditional requirements if the subject type is concrete.
153+
for (auto req : concrete->getConditionalRequirements()) {
154+
desugarRequirement(req, result);
155+
}
140156
return;
141157
}
142158

test/Generics/conditional_requirement_inference_2.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ protocol Sequence {
1515
extension Array : Sequence {}
1616

1717
struct EquatableSequenceBox<T : Sequence> where T.Element : Equatable {
18-
// CHECK: Generic signature: <T, U where T == Array<Array<U>>, U : Equatable>
18+
// CHECK-LABEL: EquatableSequenceBox.withArrayArray@
19+
// CHECK-NEXT: Generic signature: <T, U where T == Array<Array<U>>, U : Equatable>
1920
func withArrayArray<U>(_: U) where T == Array<Array<U>> {}
20-
}
21+
}
22+
23+
// Make sure requirement desugaring handles conditional conformances.
24+
protocol Hashable : Equatable {}
25+
26+
struct Set<Element : Hashable> {}
27+
28+
extension Array : Hashable where Element : Hashable {}
29+
30+
// CHECK-LABEL: doStuff@
31+
// CHECK-NEXT: Generic signature: <U where U : Hashable>
32+
func doStuff<U>(_: Set<Array<U>>) {}

0 commit comments

Comments
 (0)