Skip to content

Commit 3beb095

Browse files
committed
Fix assert in type substitution cloner
We might have to replace opaque archetypes to satisfy equality of types
1 parent 70f5fd0 commit 3beb095

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

include/swift/SIL/TypeSubstCloner.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
109109

110110
assert(Subs.empty() ||
111111
SubstCalleeSILType ==
112-
Callee->getType().substGenericArgs(AI.getModule(), Subs));
112+
Callee->getType().substGenericArgs(AI.getModule(), Subs) ||
113+
(Cloner.ReplacingOpaqueArchetypes &&
114+
SubstCalleeSILType ==
115+
Cloner.getOpType(Callee->getType().substGenericArgs(
116+
AI.getModule(), Subs))));
113117
}
114118

115119
ArrayRef<SILValue> getArguments() const {
@@ -155,12 +159,14 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
155159
TypeSubstCloner(SILFunction &To,
156160
SILFunction &From,
157161
SubstitutionMap ApplySubs,
158-
bool Inlining = false)
162+
bool Inlining = false,
163+
bool ReplacingOpaqueArchetypes = false)
159164
: SILClonerWithScopes<ImplClass>(To, Inlining),
160165
SwiftMod(From.getModule().getSwiftModule()),
161166
SubsMap(ApplySubs),
162167
Original(From),
163-
Inlining(Inlining) {
168+
Inlining(Inlining),
169+
ReplacingOpaqueArchetypes(ReplacingOpaqueArchetypes) {
164170
}
165171

166172
protected:
@@ -381,6 +387,8 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
381387
SILFunction &Original;
382388
/// True, if used for inlining.
383389
bool Inlining;
390+
/// True if replacing opaque result archetypes.
391+
bool ReplacingOpaqueArchetypes;
384392
};
385393

386394
} // end namespace swift

lib/SILOptimizer/Transforms/SpecializeOpaqueArchetypes.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#define DEBUG_TYPE "opaque-archetype-specializer"
22

3+
#include "swift/AST/Types.h"
34
#include "swift/SIL/SILFunction.h"
45
#include "swift/SIL/TypeSubstCloner.h"
56
#include "swift/SIL/SILInstruction.h"
@@ -25,7 +26,8 @@ class OpaqueSpecializerCloner
2526
friend class SILInstructionVisitor<OpaqueSpecializerCloner>;
2627

2728
OpaqueSpecializerCloner(SubstitutionMap opaqueArchetypeSubs, SILFunction &fun)
28-
: SuperTy(fun, fun, opaqueArchetypeSubs) {
29+
: SuperTy(fun, fun, opaqueArchetypeSubs, false /*Inlining*/,
30+
true /*ReplacingOpaqueArchetypes*/) {
2931
entryBlock = fun.getEntryBlock();
3032
cloneFromBlock = entryBlock->split(entryBlock->begin());
3133
}
@@ -49,7 +51,8 @@ class OpaqueSpecializerCloner
4951
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
5052
auto origResult = Inst->getOperand();
5153
auto clonedResult = getOpValue(Inst->getOperand());
52-
if (clonedResult->getType().getASTType() != origResult->getType().getASTType())
54+
if (clonedResult->getType().getASTType() !=
55+
origResult->getType().getASTType())
5356
clonedResult = getBuilder().createUncheckedRefCast(
5457
RegularLocation::getAutoGeneratedLocation(), clonedResult,
5558
origResult->getType());

test/SILOptimizer/specialize_opaque_type_archetypes.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,25 @@ public func usePair() {
206206
useP(x.first.myValue())
207207
useP(x.second.myValue())
208208
}
209+
210+
public protocol P3 {
211+
associatedtype AT
212+
func foo() -> AT
213+
}
214+
215+
public struct Adapter<T: P3>: P3 {
216+
var inner: T
217+
public func foo() -> some P3 {
218+
return inner
219+
}
220+
}
221+
222+
// Don't assert.
223+
// CHECK-LABEL: sil {{.*}} @$s1A7AdapterVyxGAA2P3A2aEP3foo2ATQzyFTW
224+
// CHECK: [[F:%.*]] = function_ref @$s1A7AdapterV3fooQryF
225+
// CHECK: apply [[F]]<τ_0_0>(%0, %1) : $@convention(method) <τ_0_0 where τ_0_0 : P3> (@in_guaranteed Adapter<τ_0_0>) -> @out @_opaqueReturnTypeOf("$s1A7AdapterV3fooQryF", 0)
226+
extension P3 {
227+
public func foo() -> some P3 {
228+
return Adapter(inner: self)
229+
}
230+
}

0 commit comments

Comments
 (0)