Skip to content

Commit 1ee07b9

Browse files
committed
AST: Remove getArchetypeAndRootOpaqueArchetype()
1 parent 53e7803 commit 1ee07b9

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

lib/AST/TypeSubstitution.cpp

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,18 +1068,6 @@ Type TypeBase::adjustSuperclassMemberDeclType(const ValueDecl *baseDecl,
10681068
// Replacing opaque result archetypes with their underlying types
10691069
//===----------------------------------------------------------------------===//
10701070

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-
10831071
OpaqueSubstitutionKind
10841072
ReplaceOpaqueTypesWithUnderlyingTypes::shouldPerformSubstitution(
10851073
OpaqueTypeDecl *opaque) const {
@@ -1217,19 +1205,20 @@ ReplaceOpaqueTypesWithUnderlyingTypes::ReplaceOpaqueTypesWithUnderlyingTypes(
12171205

12181206
Type ReplaceOpaqueTypesWithUnderlyingTypes::
12191207
operator()(SubstitutableType *maybeOpaqueType) const {
1220-
auto archetypeAndRoot = getArchetypeAndRootOpaqueArchetype(maybeOpaqueType);
1221-
if (!archetypeAndRoot)
1208+
auto *archetype = dyn_cast<OpaqueTypeArchetypeType>(maybeOpaqueType);
1209+
if (!archetype)
12221210
return maybeOpaqueType;
12231211

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();
12261215

1227-
auto substitutionKind = shouldPerformSubstitution(opaqueRoot->getDecl());
1216+
auto substitutionKind = shouldPerformSubstitution(decl);
12281217
if (substitutionKind == OpaqueSubstitutionKind::DontSubstitute) {
12291218
return maybeOpaqueType;
12301219
}
12311220

1232-
auto subs = opaqueRoot->getDecl()->getUniqueUnderlyingTypeSubstitutions();
1221+
auto subs = decl->getUniqueUnderlyingTypeSubstitutions();
12331222
// If the body of the opaque decl providing decl has not been type checked we
12341223
// don't have a underlying substitution.
12351224
if (!subs.has_value())
@@ -1261,11 +1250,11 @@ operator()(SubstitutableType *maybeOpaqueType) const {
12611250
// for its type arguments. We perform this substitution after checking for
12621251
// visibility, since we do not want the result of the visibility check to
12631252
// depend on the substitutions previously applied.
1264-
auto substTy = partialSubstTy.subst(opaqueRoot->getSubstitutions());
1253+
auto substTy = partialSubstTy.subst(outerSubs);
12651254

12661255
// If the type changed, but still contains opaque types, recur.
12671256
if (!substTy->isEqual(maybeOpaqueType) && substTy->hasOpaqueArchetype()) {
1268-
SeenDecl seenKey(opaqueRoot->getDecl(), opaqueRoot->getSubstitutions());
1257+
SeenDecl seenKey(decl, outerSubs);
12691258
if (auto *alreadySeen = this->seenDecls) {
12701259
// Detect substitution loops. If we find one, just bounce the original
12711260
// type back to the caller. This substitution will fail at runtime
@@ -1335,8 +1324,8 @@ operator()(CanType maybeOpaqueType, Type replacementType,
13351324
ProtocolDecl *protocol) const {
13361325
auto abstractRef = ProtocolConformanceRef(protocol);
13371326

1338-
auto archetypeAndRoot = getArchetypeAndRootOpaqueArchetype(maybeOpaqueType);
1339-
if (!archetypeAndRoot) {
1327+
auto archetype = dyn_cast<OpaqueTypeArchetypeType>(maybeOpaqueType);
1328+
if (!archetype) {
13401329
if (maybeOpaqueType->isTypeParameter() ||
13411330
maybeOpaqueType->is<ArchetypeType>())
13421331
return abstractRef;
@@ -1350,15 +1339,16 @@ operator()(CanType maybeOpaqueType, Type replacementType,
13501339
llvm_unreachable("origType should have been an opaque type or type parameter");
13511340
}
13521341

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();
13551345

1356-
auto substitutionKind = shouldPerformSubstitution(opaqueRoot->getDecl());
1346+
auto substitutionKind = shouldPerformSubstitution(decl);
13571347
if (substitutionKind == OpaqueSubstitutionKind::DontSubstitute) {
13581348
return abstractRef;
13591349
}
13601350

1361-
auto subs = opaqueRoot->getDecl()->getUniqueUnderlyingTypeSubstitutions();
1351+
auto subs = decl->getUniqueUnderlyingTypeSubstitutions();
13621352
// If the body of the opaque decl providing decl has not been type checked we
13631353
// don't have a underlying substitution.
13641354
if (!subs.has_value())
@@ -1389,16 +1379,16 @@ operator()(CanType maybeOpaqueType, Type replacementType,
13891379
// for its type arguments. We perform this substitution after checking for
13901380
// visibility, since we do not want the result of the visibility check to
13911381
// depend on the substitutions previously applied.
1392-
auto substTy = partialSubstTy.subst(opaqueRoot->getSubstitutions());
1382+
auto substTy = partialSubstTy.subst(outerSubs);
13931383

13941384
auto partialSubstRef =
13951385
abstractRef.subst(archetype->getInterfaceType(), *subs);
13961386
auto substRef =
1397-
partialSubstRef.subst(partialSubstTy, opaqueRoot->getSubstitutions());
1387+
partialSubstRef.subst(partialSubstTy, outerSubs);
13981388

13991389
// If the type still contains opaque types, recur.
14001390
if (substTy->hasOpaqueArchetype()) {
1401-
SeenDecl seenKey(opaqueRoot->getDecl(), opaqueRoot->getSubstitutions());
1391+
SeenDecl seenKey(decl, outerSubs);
14021392

14031393
if (auto *alreadySeen = this->seenDecls) {
14041394
// Detect substitution loops. If we find one, just bounce the original

0 commit comments

Comments
 (0)