@@ -1068,18 +1068,6 @@ Type TypeBase::adjustSuperclassMemberDeclType(const ValueDecl *baseDecl,
1068
1068
// Replacing opaque result archetypes with their underlying types
1069
1069
// ===----------------------------------------------------------------------===//
1070
1070
1071
- static std::optional<std::pair<ArchetypeType *, OpaqueTypeArchetypeType *>>
1072
- getArchetypeAndRootOpaqueArchetype (Type maybeOpaqueType) {
1073
- auto archetype = dyn_cast<ArchetypeType>(maybeOpaqueType.getPointer ());
1074
- if (!archetype)
1075
- return std::nullopt;
1076
- auto opaqueRoot = dyn_cast<OpaqueTypeArchetypeType>(archetype->getRoot ());
1077
- if (!opaqueRoot)
1078
- return std::nullopt;
1079
-
1080
- return std::make_pair (archetype, opaqueRoot);
1081
- }
1082
-
1083
1071
OpaqueSubstitutionKind
1084
1072
ReplaceOpaqueTypesWithUnderlyingTypes::shouldPerformSubstitution (
1085
1073
OpaqueTypeDecl *opaque) const {
@@ -1217,19 +1205,20 @@ ReplaceOpaqueTypesWithUnderlyingTypes::ReplaceOpaqueTypesWithUnderlyingTypes(
1217
1205
1218
1206
Type ReplaceOpaqueTypesWithUnderlyingTypes::
1219
1207
operator ()(SubstitutableType *maybeOpaqueType) const {
1220
- auto archetypeAndRoot = getArchetypeAndRootOpaqueArchetype (maybeOpaqueType);
1221
- if (!archetypeAndRoot )
1208
+ auto *archetype = dyn_cast<OpaqueTypeArchetypeType> (maybeOpaqueType);
1209
+ if (!archetype )
1222
1210
return maybeOpaqueType;
1223
1211
1224
- auto archetype = archetypeAndRoot->first ;
1225
- auto opaqueRoot = archetypeAndRoot->second ;
1212
+ auto *genericEnv = archetype->getGenericEnvironment ();
1213
+ auto *decl = genericEnv->getOpaqueTypeDecl ();
1214
+ auto outerSubs = genericEnv->getOpaqueSubstitutions ();
1226
1215
1227
- auto substitutionKind = shouldPerformSubstitution (opaqueRoot-> getDecl () );
1216
+ auto substitutionKind = shouldPerformSubstitution (decl );
1228
1217
if (substitutionKind == OpaqueSubstitutionKind::DontSubstitute) {
1229
1218
return maybeOpaqueType;
1230
1219
}
1231
1220
1232
- auto subs = opaqueRoot-> getDecl () ->getUniqueUnderlyingTypeSubstitutions ();
1221
+ auto subs = decl ->getUniqueUnderlyingTypeSubstitutions ();
1233
1222
// If the body of the opaque decl providing decl has not been type checked we
1234
1223
// don't have a underlying substitution.
1235
1224
if (!subs.has_value ())
@@ -1261,11 +1250,11 @@ operator()(SubstitutableType *maybeOpaqueType) const {
1261
1250
// for its type arguments. We perform this substitution after checking for
1262
1251
// visibility, since we do not want the result of the visibility check to
1263
1252
// depend on the substitutions previously applied.
1264
- auto substTy = partialSubstTy.subst (opaqueRoot-> getSubstitutions () );
1253
+ auto substTy = partialSubstTy.subst (outerSubs );
1265
1254
1266
1255
// If the type changed, but still contains opaque types, recur.
1267
1256
if (!substTy->isEqual (maybeOpaqueType) && substTy->hasOpaqueArchetype ()) {
1268
- SeenDecl seenKey (opaqueRoot-> getDecl (), opaqueRoot-> getSubstitutions () );
1257
+ SeenDecl seenKey (decl, outerSubs );
1269
1258
if (auto *alreadySeen = this ->seenDecls ) {
1270
1259
// Detect substitution loops. If we find one, just bounce the original
1271
1260
// type back to the caller. This substitution will fail at runtime
@@ -1335,8 +1324,8 @@ operator()(CanType maybeOpaqueType, Type replacementType,
1335
1324
ProtocolDecl *protocol) const {
1336
1325
auto abstractRef = ProtocolConformanceRef (protocol);
1337
1326
1338
- auto archetypeAndRoot = getArchetypeAndRootOpaqueArchetype (maybeOpaqueType);
1339
- if (!archetypeAndRoot ) {
1327
+ auto archetype = dyn_cast<OpaqueTypeArchetypeType> (maybeOpaqueType);
1328
+ if (!archetype ) {
1340
1329
if (maybeOpaqueType->isTypeParameter () ||
1341
1330
maybeOpaqueType->is <ArchetypeType>())
1342
1331
return abstractRef;
@@ -1350,15 +1339,16 @@ operator()(CanType maybeOpaqueType, Type replacementType,
1350
1339
llvm_unreachable (" origType should have been an opaque type or type parameter" );
1351
1340
}
1352
1341
1353
- auto archetype = archetypeAndRoot->first ;
1354
- auto opaqueRoot = archetypeAndRoot->second ;
1342
+ auto *genericEnv = archetype->getGenericEnvironment ();
1343
+ auto *decl = genericEnv->getOpaqueTypeDecl ();
1344
+ auto outerSubs = genericEnv->getOpaqueSubstitutions ();
1355
1345
1356
- auto substitutionKind = shouldPerformSubstitution (opaqueRoot-> getDecl () );
1346
+ auto substitutionKind = shouldPerformSubstitution (decl );
1357
1347
if (substitutionKind == OpaqueSubstitutionKind::DontSubstitute) {
1358
1348
return abstractRef;
1359
1349
}
1360
1350
1361
- auto subs = opaqueRoot-> getDecl () ->getUniqueUnderlyingTypeSubstitutions ();
1351
+ auto subs = decl ->getUniqueUnderlyingTypeSubstitutions ();
1362
1352
// If the body of the opaque decl providing decl has not been type checked we
1363
1353
// don't have a underlying substitution.
1364
1354
if (!subs.has_value ())
@@ -1389,16 +1379,16 @@ operator()(CanType maybeOpaqueType, Type replacementType,
1389
1379
// for its type arguments. We perform this substitution after checking for
1390
1380
// visibility, since we do not want the result of the visibility check to
1391
1381
// depend on the substitutions previously applied.
1392
- auto substTy = partialSubstTy.subst (opaqueRoot-> getSubstitutions () );
1382
+ auto substTy = partialSubstTy.subst (outerSubs );
1393
1383
1394
1384
auto partialSubstRef =
1395
1385
abstractRef.subst (archetype->getInterfaceType (), *subs);
1396
1386
auto substRef =
1397
- partialSubstRef.subst (partialSubstTy, opaqueRoot-> getSubstitutions () );
1387
+ partialSubstRef.subst (partialSubstTy, outerSubs );
1398
1388
1399
1389
// If the type still contains opaque types, recur.
1400
1390
if (substTy->hasOpaqueArchetype ()) {
1401
- SeenDecl seenKey (opaqueRoot-> getDecl (), opaqueRoot-> getSubstitutions () );
1391
+ SeenDecl seenKey (decl, outerSubs );
1402
1392
1403
1393
if (auto *alreadySeen = this ->seenDecls ) {
1404
1394
// Detect substitution loops. If we find one, just bounce the original
0 commit comments