Skip to content

Commit 25f998a

Browse files
committed
AST: Remove getArchetypeAndRootOpaqueArchetype()
1 parent 66cd708 commit 25f998a

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
@@ -1045,18 +1045,6 @@ Type TypeBase::adjustSuperclassMemberDeclType(const ValueDecl *baseDecl,
10451045
// Replacing opaque result archetypes with their underlying types
10461046
//===----------------------------------------------------------------------===//
10471047

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-
10601048
OpaqueSubstitutionKind
10611049
ReplaceOpaqueTypesWithUnderlyingTypes::shouldPerformSubstitution(
10621050
OpaqueTypeDecl *opaque) const {
@@ -1194,19 +1182,20 @@ ReplaceOpaqueTypesWithUnderlyingTypes::ReplaceOpaqueTypesWithUnderlyingTypes(
11941182

11951183
Type ReplaceOpaqueTypesWithUnderlyingTypes::
11961184
operator()(SubstitutableType *maybeOpaqueType) const {
1197-
auto archetypeAndRoot = getArchetypeAndRootOpaqueArchetype(maybeOpaqueType);
1198-
if (!archetypeAndRoot)
1185+
auto *archetype = dyn_cast<OpaqueTypeArchetypeType>(maybeOpaqueType);
1186+
if (!archetype)
11991187
return maybeOpaqueType;
12001188

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

1204-
auto substitutionKind = shouldPerformSubstitution(opaqueRoot->getDecl());
1193+
auto substitutionKind = shouldPerformSubstitution(decl);
12051194
if (substitutionKind == OpaqueSubstitutionKind::DontSubstitute) {
12061195
return maybeOpaqueType;
12071196
}
12081197

1209-
auto subs = opaqueRoot->getDecl()->getUniqueUnderlyingTypeSubstitutions();
1198+
auto subs = decl->getUniqueUnderlyingTypeSubstitutions();
12101199
// If the body of the opaque decl providing decl has not been type checked we
12111200
// don't have a underlying substitution.
12121201
if (!subs.has_value())
@@ -1238,11 +1227,11 @@ operator()(SubstitutableType *maybeOpaqueType) const {
12381227
// for its type arguments. We perform this substitution after checking for
12391228
// visibility, since we do not want the result of the visibility check to
12401229
// depend on the substitutions previously applied.
1241-
auto substTy = partialSubstTy.subst(opaqueRoot->getSubstitutions());
1230+
auto substTy = partialSubstTy.subst(outerSubs);
12421231

12431232
// If the type changed, but still contains opaque types, recur.
12441233
if (!substTy->isEqual(maybeOpaqueType) && substTy->hasOpaqueArchetype()) {
1245-
SeenDecl seenKey(opaqueRoot->getDecl(), opaqueRoot->getSubstitutions());
1234+
SeenDecl seenKey(decl, outerSubs);
12461235
if (auto *alreadySeen = this->seenDecls) {
12471236
// Detect substitution loops. If we find one, just bounce the original
12481237
// type back to the caller. This substitution will fail at runtime
@@ -1312,8 +1301,8 @@ operator()(CanType maybeOpaqueType, Type replacementType,
13121301
ProtocolDecl *protocol) const {
13131302
auto abstractRef = ProtocolConformanceRef(protocol);
13141303

1315-
auto archetypeAndRoot = getArchetypeAndRootOpaqueArchetype(maybeOpaqueType);
1316-
if (!archetypeAndRoot) {
1304+
auto archetype = dyn_cast<OpaqueTypeArchetypeType>(maybeOpaqueType);
1305+
if (!archetype) {
13171306
if (maybeOpaqueType->isTypeParameter() ||
13181307
maybeOpaqueType->is<ArchetypeType>())
13191308
return abstractRef;
@@ -1327,15 +1316,16 @@ operator()(CanType maybeOpaqueType, Type replacementType,
13271316
llvm_unreachable("origType should have been an opaque type or type parameter");
13281317
}
13291318

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

1333-
auto substitutionKind = shouldPerformSubstitution(opaqueRoot->getDecl());
1323+
auto substitutionKind = shouldPerformSubstitution(decl);
13341324
if (substitutionKind == OpaqueSubstitutionKind::DontSubstitute) {
13351325
return abstractRef;
13361326
}
13371327

1338-
auto subs = opaqueRoot->getDecl()->getUniqueUnderlyingTypeSubstitutions();
1328+
auto subs = decl->getUniqueUnderlyingTypeSubstitutions();
13391329
// If the body of the opaque decl providing decl has not been type checked we
13401330
// don't have a underlying substitution.
13411331
if (!subs.has_value())
@@ -1366,16 +1356,16 @@ operator()(CanType maybeOpaqueType, Type replacementType,
13661356
// for its type arguments. We perform this substitution after checking for
13671357
// visibility, since we do not want the result of the visibility check to
13681358
// depend on the substitutions previously applied.
1369-
auto substTy = partialSubstTy.subst(opaqueRoot->getSubstitutions());
1359+
auto substTy = partialSubstTy.subst(outerSubs);
13701360

13711361
auto partialSubstRef =
13721362
abstractRef.subst(archetype->getInterfaceType(), *subs);
13731363
auto substRef =
1374-
partialSubstRef.subst(partialSubstTy, opaqueRoot->getSubstitutions());
1364+
partialSubstRef.subst(partialSubstTy, outerSubs);
13751365

13761366
// If the type still contains opaque types, recur.
13771367
if (substTy->hasOpaqueArchetype()) {
1378-
SeenDecl seenKey(opaqueRoot->getDecl(), opaqueRoot->getSubstitutions());
1368+
SeenDecl seenKey(decl, outerSubs);
13791369

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

0 commit comments

Comments
 (0)