@@ -118,10 +118,21 @@ class SILCloner : protected SILVisitor<ImplClass> {
118
118
}
119
119
120
120
SILType getTypeInClonedContext (SILType Ty) {
121
+ auto objectTy = Ty.getSwiftRValueType ();
122
+ // Do not substitute opened existential types, if we do not have any.
123
+ if (!objectTy->hasOpenedExistential ())
124
+ return Ty;
125
+ // Do not substitute opened existential types, if it is not required.
126
+ // This is often the case when cloning basic blocks inside the same
127
+ // function.
128
+ if (OpenedExistentialSubs.empty ())
129
+ return Ty;
130
+
121
131
// Substitute opened existential types, if we have any.
122
- return SILType::getPrimitiveObjectType (
123
- getASTTypeInClonedContext (Ty.getSwiftRValueType ()))
124
- .copyCategory (Ty);
132
+ return Ty.subst (
133
+ Builder.getModule (),
134
+ QueryTypeSubstitutionMapOrIdentity{OpenedExistentialSubs},
135
+ MakeAbstractConformanceForGenericType ());
125
136
}
126
137
SILType getOpType (SILType Ty) {
127
138
Ty = getTypeInClonedContext (Ty);
@@ -138,24 +149,10 @@ class SILCloner : protected SILVisitor<ImplClass> {
138
149
if (OpenedExistentialSubs.empty ())
139
150
return ty->getCanonicalType ();
140
151
141
- return ty.transform (
142
- [&](Type t) -> Type {
143
- if (t->isOpenedExistential ()) {
144
- auto found = OpenedExistentialSubs.find (
145
- t->castTo <ArchetypeType>());
146
- // If an opened existential is supposed to be
147
- // remapped, it is guaranteed by construction
148
- // to be in the OpenedExistentialSubs, because
149
- // a cloner always processes definitions of
150
- // opened existentials before their uses and
151
- // adds found opened existentials definitions
152
- // to the map.
153
- if (found != OpenedExistentialSubs.end ())
154
- return found->second ;
155
- return t;
156
- }
157
- return t;
158
- })->getCanonicalType ();
152
+ return ty.subst (
153
+ QueryTypeSubstitutionMapOrIdentity{OpenedExistentialSubs},
154
+ MakeAbstractConformanceForGenericType ()
155
+ )->getCanonicalType ();
159
156
}
160
157
161
158
CanType getOpASTType (CanType ty) {
0 commit comments