@@ -1045,18 +1045,6 @@ Type TypeBase::adjustSuperclassMemberDeclType(const ValueDecl *baseDecl,
1045
1045
// Replacing opaque result archetypes with their underlying types
1046
1046
// ===----------------------------------------------------------------------===//
1047
1047
1048
- static std::optional<std::pair<ArchetypeType *, OpaqueTypeArchetypeType *>>
1049
- getArchetypeAndRootOpaqueArchetype (Type maybeOpaqueType) {
1050
- auto archetype = dyn_cast<ArchetypeType>(maybeOpaqueType.getPointer ());
1051
- if (!archetype)
1052
- return std::nullopt;
1053
- auto opaqueRoot = dyn_cast<OpaqueTypeArchetypeType>(archetype->getRoot ());
1054
- if (!opaqueRoot)
1055
- return std::nullopt;
1056
-
1057
- return std::make_pair (archetype, opaqueRoot);
1058
- }
1059
-
1060
1048
OpaqueSubstitutionKind
1061
1049
ReplaceOpaqueTypesWithUnderlyingTypes::shouldPerformSubstitution (
1062
1050
OpaqueTypeDecl *opaque) const {
@@ -1194,19 +1182,20 @@ ReplaceOpaqueTypesWithUnderlyingTypes::ReplaceOpaqueTypesWithUnderlyingTypes(
1194
1182
1195
1183
Type ReplaceOpaqueTypesWithUnderlyingTypes::
1196
1184
operator ()(SubstitutableType *maybeOpaqueType) const {
1197
- auto archetypeAndRoot = getArchetypeAndRootOpaqueArchetype (maybeOpaqueType);
1198
- if (!archetypeAndRoot )
1185
+ auto *archetype = dyn_cast<OpaqueTypeArchetypeType> (maybeOpaqueType);
1186
+ if (!archetype )
1199
1187
return maybeOpaqueType;
1200
1188
1201
- auto archetype = archetypeAndRoot->first ;
1202
- auto opaqueRoot = archetypeAndRoot->second ;
1189
+ auto *genericEnv = archetype->getGenericEnvironment ();
1190
+ auto *decl = genericEnv->getOpaqueTypeDecl ();
1191
+ auto outerSubs = genericEnv->getOpaqueSubstitutions ();
1203
1192
1204
- auto substitutionKind = shouldPerformSubstitution (opaqueRoot-> getDecl () );
1193
+ auto substitutionKind = shouldPerformSubstitution (decl );
1205
1194
if (substitutionKind == OpaqueSubstitutionKind::DontSubstitute) {
1206
1195
return maybeOpaqueType;
1207
1196
}
1208
1197
1209
- auto subs = opaqueRoot-> getDecl () ->getUniqueUnderlyingTypeSubstitutions ();
1198
+ auto subs = decl ->getUniqueUnderlyingTypeSubstitutions ();
1210
1199
// If the body of the opaque decl providing decl has not been type checked we
1211
1200
// don't have a underlying substitution.
1212
1201
if (!subs.has_value ())
@@ -1238,11 +1227,11 @@ operator()(SubstitutableType *maybeOpaqueType) const {
1238
1227
// for its type arguments. We perform this substitution after checking for
1239
1228
// visibility, since we do not want the result of the visibility check to
1240
1229
// depend on the substitutions previously applied.
1241
- auto substTy = partialSubstTy.subst (opaqueRoot-> getSubstitutions () );
1230
+ auto substTy = partialSubstTy.subst (outerSubs );
1242
1231
1243
1232
// If the type changed, but still contains opaque types, recur.
1244
1233
if (!substTy->isEqual (maybeOpaqueType) && substTy->hasOpaqueArchetype ()) {
1245
- SeenDecl seenKey (opaqueRoot-> getDecl (), opaqueRoot-> getSubstitutions () );
1234
+ SeenDecl seenKey (decl, outerSubs );
1246
1235
if (auto *alreadySeen = this ->seenDecls ) {
1247
1236
// Detect substitution loops. If we find one, just bounce the original
1248
1237
// type back to the caller. This substitution will fail at runtime
@@ -1312,8 +1301,8 @@ operator()(CanType maybeOpaqueType, Type replacementType,
1312
1301
ProtocolDecl *protocol) const {
1313
1302
auto abstractRef = ProtocolConformanceRef (protocol);
1314
1303
1315
- auto archetypeAndRoot = getArchetypeAndRootOpaqueArchetype (maybeOpaqueType);
1316
- if (!archetypeAndRoot ) {
1304
+ auto archetype = dyn_cast<OpaqueTypeArchetypeType> (maybeOpaqueType);
1305
+ if (!archetype ) {
1317
1306
if (maybeOpaqueType->isTypeParameter () ||
1318
1307
maybeOpaqueType->is <ArchetypeType>())
1319
1308
return abstractRef;
@@ -1327,15 +1316,16 @@ operator()(CanType maybeOpaqueType, Type replacementType,
1327
1316
llvm_unreachable (" origType should have been an opaque type or type parameter" );
1328
1317
}
1329
1318
1330
- auto archetype = archetypeAndRoot->first ;
1331
- auto opaqueRoot = archetypeAndRoot->second ;
1319
+ auto *genericEnv = archetype->getGenericEnvironment ();
1320
+ auto *decl = genericEnv->getOpaqueTypeDecl ();
1321
+ auto outerSubs = genericEnv->getOpaqueSubstitutions ();
1332
1322
1333
- auto substitutionKind = shouldPerformSubstitution (opaqueRoot-> getDecl () );
1323
+ auto substitutionKind = shouldPerformSubstitution (decl );
1334
1324
if (substitutionKind == OpaqueSubstitutionKind::DontSubstitute) {
1335
1325
return abstractRef;
1336
1326
}
1337
1327
1338
- auto subs = opaqueRoot-> getDecl () ->getUniqueUnderlyingTypeSubstitutions ();
1328
+ auto subs = decl ->getUniqueUnderlyingTypeSubstitutions ();
1339
1329
// If the body of the opaque decl providing decl has not been type checked we
1340
1330
// don't have a underlying substitution.
1341
1331
if (!subs.has_value ())
@@ -1366,16 +1356,16 @@ operator()(CanType maybeOpaqueType, Type replacementType,
1366
1356
// for its type arguments. We perform this substitution after checking for
1367
1357
// visibility, since we do not want the result of the visibility check to
1368
1358
// depend on the substitutions previously applied.
1369
- auto substTy = partialSubstTy.subst (opaqueRoot-> getSubstitutions () );
1359
+ auto substTy = partialSubstTy.subst (outerSubs );
1370
1360
1371
1361
auto partialSubstRef =
1372
1362
abstractRef.subst (archetype->getInterfaceType (), *subs);
1373
1363
auto substRef =
1374
- partialSubstRef.subst (partialSubstTy, opaqueRoot-> getSubstitutions () );
1364
+ partialSubstRef.subst (partialSubstTy, outerSubs );
1375
1365
1376
1366
// If the type still contains opaque types, recur.
1377
1367
if (substTy->hasOpaqueArchetype ()) {
1378
- SeenDecl seenKey (opaqueRoot-> getDecl (), opaqueRoot-> getSubstitutions () );
1368
+ SeenDecl seenKey (decl, outerSubs );
1379
1369
1380
1370
if (auto *alreadySeen = this ->seenDecls ) {
1381
1371
// Detect substitution loops. If we find one, just bounce the original
0 commit comments