Skip to content

Commit 352b3a2

Browse files
Merge pull request #41201 from AnthonyLatsis/opened-root
AST: Remove OpenedArchetypeType::getOpenedExistentialType()
2 parents ca34a30 + 65d4af7 commit 352b3a2

File tree

11 files changed

+40
-41
lines changed

11 files changed

+40
-41
lines changed

include/swift/AST/Types.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5773,9 +5773,6 @@ class OpenedArchetypeType final : public ArchetypeType,
57735773

57745774
/// Retrieve the ID number of this opened existential.
57755775
UUID getOpenedExistentialID() const;
5776-
5777-
/// Retrieve the opened existential type
5778-
Type getOpenedExistentialType() const;
57795776

57805777
/// Return the archetype that represents the root generic parameter of its
57815778
/// interface type.
@@ -6415,8 +6412,7 @@ inline bool TypeBase::isOpenedExistentialWithError() {
64156412

64166413
CanType T = getCanonicalType();
64176414
if (auto archetype = dyn_cast<OpenedArchetypeType>(T)) {
6418-
auto openedExistentialType = archetype->getOpenedExistentialType();
6419-
return openedExistentialType->isExistentialWithError();
6415+
return archetype->getExistentialType()->isExistentialWithError();
64206416
}
64216417
return false;
64226418
}

include/swift/SIL/SILCloner.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,10 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
248248
return asImpl().remapASTType(ty);
249249
}
250250

251-
void remapOpenedType(CanOpenedArchetypeType archetypeTy) {
252-
auto existentialTy = archetypeTy->getOpenedExistentialType()->getCanonicalType();
251+
void remapRootOpenedType(CanOpenedArchetypeType archetypeTy) {
252+
assert(archetypeTy->isRoot());
253+
254+
auto existentialTy = archetypeTy->getExistentialType()->getCanonicalType();
253255
auto replacementTy = OpenedArchetypeType::get(
254256
getOpASTType(existentialTy),
255257
archetypeTy->getInterfaceType());
@@ -2199,7 +2201,7 @@ template<typename ImplClass>
21992201
void
22002202
SILCloner<ImplClass>::visitOpenExistentialAddrInst(OpenExistentialAddrInst *Inst) {
22012203
// Create a new archetype for this opened existential type.
2202-
remapOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
2204+
remapRootOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
22032205

22042206
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
22052207
recordClonedInstruction(
@@ -2212,7 +2214,7 @@ template <typename ImplClass>
22122214
void SILCloner<ImplClass>::visitOpenExistentialValueInst(
22132215
OpenExistentialValueInst *Inst) {
22142216
// Create a new archetype for this opened existential type.
2215-
remapOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
2217+
remapRootOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
22162218

22172219
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
22182220
recordClonedInstruction(
@@ -2235,7 +2237,7 @@ visitOpenExistentialMetatypeInst(OpenExistentialMetatypeInst *Inst) {
22352237
exType = exMetatype->getExistentialInstanceType()->getCanonicalType();
22362238
openedType = cast<MetatypeType>(openedType).getInstanceType();
22372239
}
2238-
remapOpenedType(cast<OpenedArchetypeType>(openedType));
2240+
remapRootOpenedType(cast<OpenedArchetypeType>(openedType));
22392241

22402242
if (!Inst->getOperand()->getType().canUseExistentialRepresentation(
22412243
ExistentialRepresentation::Class)) {
@@ -2259,7 +2261,7 @@ void
22592261
SILCloner<ImplClass>::
22602262
visitOpenExistentialRefInst(OpenExistentialRefInst *Inst) {
22612263
// Create a new archetype for this opened existential type.
2262-
remapOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
2264+
remapRootOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
22632265

22642266
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
22652267
recordClonedInstruction(
@@ -2276,7 +2278,7 @@ void
22762278
SILCloner<ImplClass>::
22772279
visitOpenExistentialBoxInst(OpenExistentialBoxInst *Inst) {
22782280
// Create a new archetype for this opened existential type.
2279-
remapOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
2281+
remapRootOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
22802282

22812283
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
22822284
recordClonedInstruction(Inst, getBuilder().createOpenExistentialBox(
@@ -2290,7 +2292,7 @@ void
22902292
SILCloner<ImplClass>::
22912293
visitOpenExistentialBoxValueInst(OpenExistentialBoxValueInst *Inst) {
22922294
// Create a new archetype for this opened existential type.
2293-
remapOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
2295+
remapRootOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
22942296

22952297
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
22962298
recordClonedInstruction(

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4481,10 +4481,10 @@ CanOpenedArchetypeType OpenedArchetypeType::get(CanType existential,
44814481
auto found = openedExistentialEnvironments.find(*knownID);
44824482

44834483
if (found != openedExistentialEnvironments.end()) {
4484+
assert(found->second->getOpenedExistentialType()->isEqual(existential) &&
4485+
"Retrieved the wrong generic environment?");
44844486
auto result = found->second->mapTypeIntoContext(interfaceType)
44854487
->castTo<OpenedArchetypeType>();
4486-
assert(result->getOpenedExistentialType()->isEqual(existential) &&
4487-
"Retrieved the wrong opened existential type?");
44884488
return CanOpenedArchetypeType(result);
44894489
}
44904490
} else {

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3743,7 +3743,8 @@ namespace {
37433743
}
37443744
void visitOpenedArchetypeType(OpenedArchetypeType *T, StringRef label) {
37453745
printArchetypeCommon(T, "opened_archetype_type", label);
3746-
printRec("opened_existential", T->getOpenedExistentialType());
3746+
printRec("opened_existential",
3747+
T->getGenericEnvironment()->getOpenedExistentialType());
37473748
printField("opened_existential_id", T->getOpenedExistentialID());
37483749
PrintWithColorRAII(OS, ParenthesisColor) << ')';
37493750
}

lib/AST/ASTPrinter.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -943,13 +943,10 @@ class PrintAST : public ASTVisitor<PrintAST> {
943943
// so we can't use it for TypeTransformContext.
944944
// To work around this, replace the OpenedArchetypeType with the type of
945945
// the protocol itself.
946-
CurrentType = CurrentType.transform([](Type T) -> Type {
947-
if (auto *Opened = T->getAs<OpenedArchetypeType>()) {
948-
return Opened->getOpenedExistentialType();
949-
} else {
950-
return T;
951-
}
952-
});
946+
if (auto *Opened = CurrentType->getAs<OpenedArchetypeType>()) {
947+
assert(Opened->isRoot());
948+
CurrentType = Opened->getExistentialType();
949+
}
953950
CurrentType = CurrentType->mapTypeOutOfContext();
954951
}
955952
setCurrentType(CurrentType);
@@ -5983,7 +5980,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
59835980

59845981
if (Options.PrintForSIL)
59855982
Printer << "@opened(\"" << T->getOpenedExistentialID() << "\") ";
5986-
visit(T->getOpenedExistentialType());
5983+
visit(T->getExistentialType());
59875984
}
59885985

59895986
void printArchetypeCommon(ArchetypeType *T,

lib/AST/Type.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ Type TypeBase::typeEraseOpenedArchetypesWithRoot(
483483
return type;
484484

485485
const auto sig = root->getASTContext().getOpenedArchetypeSignature(
486-
root->getOpenedExistentialType());
486+
root->getExistentialType());
487487

488488
unsigned metatypeDepth = 0;
489489

@@ -519,7 +519,7 @@ Type TypeBase::typeEraseOpenedArchetypesWithRoot(
519519

520520
Type erasedTy;
521521
if (root->isEqual(archetype)) {
522-
erasedTy = root->getOpenedExistentialType();
522+
erasedTy = root->getExistentialType();
523523
} else {
524524
erasedTy =
525525
sig->getNonDependentUpperBounds(archetype->getInterfaceType());
@@ -3345,9 +3345,12 @@ bool ArchetypeType::isRoot() const {
33453345

33463346
Type ArchetypeType::getExistentialType() const {
33473347
// Opened types hold this directly.
3348-
if (auto opened = dyn_cast<OpenedArchetypeType>(this))
3349-
return opened->getOpenedExistentialType();
3350-
3348+
if (auto *opened = dyn_cast<OpenedArchetypeType>(this)) {
3349+
if (opened->isRoot()) {
3350+
return getGenericEnvironment()->getOpenedExistentialType();
3351+
}
3352+
}
3353+
33513354
// Otherwise, compute it from scratch.
33523355
SmallVector<Type, 4> constraintTypes;
33533356

@@ -3398,10 +3401,6 @@ UUID OpenedArchetypeType::getOpenedExistentialID() const {
33983401
return getGenericEnvironment()->getOpenedExistentialUUID();
33993402
}
34003403

3401-
Type OpenedArchetypeType::getOpenedExistentialType() const {
3402-
return getGenericEnvironment()->getOpenedExistentialType();
3403-
}
3404-
34053404
OpaqueTypeArchetypeType::OpaqueTypeArchetypeType(
34063405
GenericEnvironment *environment,
34073406
RecursiveTypeProperties properties,

lib/IDE/Utils.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,8 +1299,10 @@ void swift::ide::getReceiverType(Expr *Base,
12991299
ReceiverTy = SelfT->getSelfType();
13001300

13011301
// TODO: Handle generics and composed protocols
1302-
if (auto OpenedTy = ReceiverTy->getAs<OpenedArchetypeType>())
1303-
ReceiverTy = OpenedTy->getOpenedExistentialType();
1302+
if (auto OpenedTy = ReceiverTy->getAs<OpenedArchetypeType>()) {
1303+
assert(OpenedTy->isRoot());
1304+
ReceiverTy = OpenedTy->getExistentialType();
1305+
}
13041306

13051307
if (auto TyD = ReceiverTy->getAnyNominal()) {
13061308
Types.push_back(TyD);

lib/SILGen/ResultPlan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ mapTypeOutOfOpenedExistentialContext(CanType t) {
100100
params.push_back(param);
101101

102102
const auto constraintTy = openedTypes[i]
103-
->getOpenedExistentialType()
103+
->getExistentialType()
104104
->castTo<ExistentialType>()
105105
->getConstraintType();
106106
requirements.emplace_back(RequirementKind::Conformance, param,

lib/SILGen/SILGenPoly.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3049,7 +3049,8 @@ buildThunkSignature(SILGenFunction &SGF,
30493049
auto *newGenericParam =
30503050
GenericTypeParamType::get(/*type sequence*/ false, depth, 0, ctx);
30513051

3052-
auto constraint = openedExistential->getOpenedExistentialType();
3052+
assert(openedExistential->isRoot());
3053+
auto constraint = openedExistential->getExistentialType();
30533054
if (auto existential = constraint->getAs<ExistentialType>())
30543055
constraint = existential->getConstraintType();
30553056

lib/SILOptimizer/Differentiation/Thunk.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ CanGenericSignature buildThunkSignature(SILFunction *fn, bool inheritGenericSig,
6767
auto *newGenericParam =
6868
GenericTypeParamType::get(/*type sequence*/ false, depth, 0, ctx);
6969

70-
auto constraint = openedExistential->getOpenedExistentialType();
70+
assert(openedExistential->isRoot());
71+
auto constraint = openedExistential->getExistentialType();
7172
if (auto existential = constraint->getAs<ExistentialType>())
7273
constraint = existential->getConstraintType();
7374

lib/Sema/CSApply.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ Type Solution::getFixedType(TypeVariableType *typeVar) const {
6666
/// members via dynamic lookup.
6767
static bool isOpenedAnyObject(Type type) {
6868
auto archetype = type->getAs<OpenedArchetypeType>();
69-
if (!archetype)
69+
if (!archetype || !archetype->isRoot())
7070
return false;
7171

72-
return archetype->getOpenedExistentialType()->isAnyObject();
72+
return archetype->getExistentialType()->isAnyObject();
7373
}
7474

7575
SubstitutionMap
@@ -7514,7 +7514,7 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
75147514
->getExistentialInstanceType();
75157515
}
75167516
assert(openedInstanceTy->castTo<OpenedArchetypeType>()
7517-
->getOpenedExistentialType()
7517+
->getExistentialType()
75187518
->isEqual(existentialInstanceTy));
75197519

75207520
auto opaqueValue =

0 commit comments

Comments
 (0)