Skip to content

Commit 527f31f

Browse files
committed
AutoDiff: Another ugly hack to work around generics invariant violations
1 parent 73faf4f commit 527f31f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,33 @@ static CanGenericSignature buildDifferentiableGenericSignature(CanGenericSignatu
406406
return t->isEqual(interfaceTy->getRootGenericParam());
407407
}) != genericParams.end()) {
408408
types.insert(interfaceTy->getCanonicalType());
409+
409410
for (auto *proto : at->getConformsTo()) {
410411
reqs.push_back(Requirement(RequirementKind::Conformance,
411412
interfaceTy,
412413
proto->getDeclaredInterfaceType()));
413414
}
415+
416+
// The GSB would add conformance requirements if a nested type
417+
// requirement involving a resolved DependentMemberType was added;
418+
// eg, if you start with <T> and add T.[P]A == Int, it would also
419+
// add the conformance requirement T : P.
420+
//
421+
// This was not an intended behavior on the part of the GSB, and the
422+
// logic here is a complete mess, so just simulate the old behavior
423+
// here.
424+
auto parentTy = interfaceTy;
425+
while (parentTy) {
426+
if (auto memberTy = parentTy->getAs<DependentMemberType>()) {
427+
parentTy = memberTy->getBase();
428+
if (auto *assocTy = memberTy->getAssocType()) {
429+
reqs.push_back(Requirement(RequirementKind::Conformance,
430+
parentTy,
431+
assocTy->getProtocol()->getDeclaredInterfaceType()));
432+
}
433+
} else
434+
parentTy = Type();
435+
}
414436
}
415437
}
416438
return false;

test/AutoDiff/SILOptimizer/generics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-emit-sil -verify %s | %FileCheck %s -check-prefix=CHECK-SIL
1+
// RUN: %target-swift-emit-sil -verify %s -requirement-machine-abstract-signatures=verify | %FileCheck %s -check-prefix=CHECK-SIL
22

33
import _Differentiation
44

0 commit comments

Comments
 (0)