Skip to content

Commit 6060de6

Browse files
committed
[AST] Teach ExistentialType::get to only produce ExistentialType when
explicit existential types are enabled.
1 parent ee331a8 commit 6060de6

File tree

12 files changed

+42
-85
lines changed

12 files changed

+42
-85
lines changed

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,6 @@ class ASTContext final {
531531

532532
/// Retrieve the declaration of Swift.Error.
533533
ProtocolDecl *getErrorDecl() const;
534-
CanType getExceptionType() const;
535534
CanType getErrorExistentialType() const;
536535

537536
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \

include/swift/AST/Decl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4363,6 +4363,11 @@ class ProtocolDecl final : public NominalTypeDecl {
43634363
/// not exist.
43644364
AssociatedTypeDecl *getAssociatedType(Identifier name) const;
43654365

4366+
/// Returns the existential type for this protocol.
4367+
Type getExistentialType() const {
4368+
return ExistentialType::get(getDeclaredInterfaceType());
4369+
}
4370+
43664371
/// Walk this protocol and all of the protocols inherited by this protocol,
43674372
/// transitively, invoking the callback function for each protocol.
43684373
///

include/swift/AST/Types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5248,7 +5248,7 @@ class ExistentialType final : public TypeBase {
52485248
ConstraintType(constraintType) {}
52495249

52505250
public:
5251-
static ExistentialType *get(Type constraint);
5251+
static Type get(Type constraint);
52525252

52535253
Type getConstraintType() const { return ConstraintType; }
52545254

lib/AST/ASTContext.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -847,9 +847,9 @@ Type ASTContext::get##NAME##Type() const { \
847847
}
848848
#include "swift/AST/KnownStdlibTypes.def"
849849

850-
CanType ASTContext::getExceptionType() const {
850+
CanType ASTContext::getErrorExistentialType() const {
851851
if (auto exn = getErrorDecl()) {
852-
return exn->getDeclaredInterfaceType()->getCanonicalType();
852+
return exn->getExistentialType()->getCanonicalType();
853853
} else {
854854
// Use Builtin.NativeObject just as a stand-in.
855855
return TheNativeObjectType;
@@ -860,16 +860,6 @@ ProtocolDecl *ASTContext::getErrorDecl() const {
860860
return getProtocol(KnownProtocolKind::Error);
861861
}
862862

863-
CanType ASTContext::getErrorExistentialType() const {
864-
Type errorType = getExceptionType();
865-
if (LangOpts.EnableExplicitExistentialTypes &&
866-
errorType->isConstraintType()) {
867-
errorType = ExistentialType::get(errorType);
868-
}
869-
870-
return errorType->getCanonicalType();
871-
}
872-
873863
EnumElementDecl *ASTContext::getOptionalSomeDecl() const {
874864
if (!getImpl().OptionalSomeDecl)
875865
getImpl().OptionalSomeDecl = getOptionalDecl()->getUniqueElement(/*hasVal*/true);
@@ -2266,7 +2256,7 @@ ASTContext::getSelfConformance(ProtocolDecl *protocol) {
22662256
auto &entry = selfConformances[protocol];
22672257
if (!entry) {
22682258
entry = new (*this, AllocationArena::Permanent)
2269-
SelfProtocolConformance(protocol->getDeclaredInterfaceType());
2259+
SelfProtocolConformance(protocol->getExistentialType());
22702260
}
22712261
return entry;
22722262
}
@@ -3367,15 +3357,7 @@ ExistentialMetatypeType::ExistentialMetatypeType(Type T,
33673357
}
33683358

33693359
Type ExistentialMetatypeType::getExistentialInstanceType() {
3370-
auto instanceType = getInstanceType();
3371-
// Note that Any and AnyObject don't yet use ExistentialType.
3372-
if (getASTContext().LangOpts.EnableExplicitExistentialTypes &&
3373-
!instanceType->is<ExistentialMetatypeType>() &&
3374-
!(instanceType->isAny() || instanceType->isAnyObject())) {
3375-
instanceType = ExistentialType::get(instanceType);
3376-
}
3377-
3378-
return instanceType;
3360+
return ExistentialType::get(getInstanceType());
33793361
}
33803362

33813363
ModuleType *ModuleType::get(ModuleDecl *M) {
@@ -4130,11 +4112,22 @@ ProtocolType::ProtocolType(ProtocolDecl *TheDecl, Type Parent,
41304112
RecursiveTypeProperties properties)
41314113
: NominalType(TypeKind::Protocol, &Ctx, TheDecl, Parent, properties) { }
41324114

4133-
ExistentialType *ExistentialType::get(Type constraint) {
4115+
Type ExistentialType::get(Type constraint) {
4116+
auto &C = constraint->getASTContext();
4117+
if (!C.LangOpts.EnableExplicitExistentialTypes)
4118+
return constraint;
4119+
4120+
// FIXME: Any and AnyObject don't yet use ExistentialType.
4121+
if (constraint->isAny() || constraint->isAnyObject())
4122+
return constraint;
4123+
4124+
// ExistentialMetatypeType is already an existential type.
4125+
if (constraint->is<ExistentialMetatypeType>())
4126+
return constraint;
4127+
41344128
auto properties = constraint->getRecursiveProperties();
41354129
auto arena = getArena(properties);
41364130

4137-
auto &C = constraint->getASTContext();
41384131
auto &entry = C.getImpl().getArena(arena).ExistentialTypes[constraint];
41394132
if (entry)
41404133
return entry;

lib/AST/Type.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,7 @@ CanType TypeBase::computeCanonicalType() {
15011501
case TypeKind::Existential: {
15021502
auto *existential = cast<ExistentialType>(this);
15031503
auto constraint = existential->getConstraintType()->getCanonicalType();
1504-
Result = ExistentialType::get(constraint);
1504+
Result = ExistentialType::get(constraint).getPointer();
15051505
break;
15061506
}
15071507
case TypeKind::ExistentialMetatype: {
@@ -3251,10 +3251,7 @@ Type ArchetypeType::getExistentialType() const {
32513251
auto constraint = ProtocolCompositionType::get(
32523252
ctx, constraintTypes, requiresClass());
32533253

3254-
if (ctx.LangOpts.EnableExplicitExistentialTypes)
3255-
return ExistentialType::get(constraint);
3256-
3257-
return constraint;
3254+
return ExistentialType::get(constraint);
32583255
}
32593256

32603257
PrimaryArchetypeType::PrimaryArchetypeType(const ASTContext &Ctx,

lib/AST/TypeJoinMeet.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,6 @@ CanType TypeJoin::visitExistentialType(CanType second) {
287287
if (!joinInstance)
288288
return CanType();
289289

290-
if (joinInstance->is<ExistentialMetatypeType>() ||
291-
joinInstance->isAny() ||
292-
joinInstance->isAnyObject())
293-
return joinInstance;
294-
295290
return ExistentialType::get(joinInstance)->getCanonicalType();
296291
}
297292

lib/ClangImporter/ImportType.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,12 +1049,10 @@ namespace {
10491049
if (memberTypes.empty())
10501050
hasExplicitAnyObject = true;
10511051

1052-
Type importedTypeArg = ProtocolCompositionType::get(
1053-
Impl.SwiftContext, memberTypes,
1054-
hasExplicitAnyObject);
1055-
if (Impl.SwiftContext.LangOpts.EnableExplicitExistentialTypes) {
1056-
importedTypeArg = ExistentialType::get(importedTypeArg);
1057-
}
1052+
Type importedTypeArg = ExistentialType::get(
1053+
ProtocolCompositionType::get(
1054+
Impl.SwiftContext, memberTypes,
1055+
hasExplicitAnyObject));
10581056
importedTypeArgs.push_back(importedTypeArg);
10591057
}
10601058
}
@@ -1183,8 +1181,7 @@ namespace {
11831181
}
11841182
}
11851183

1186-
if (bridgedType->isConstraintType() &&
1187-
Impl.SwiftContext.LangOpts.EnableExplicitExistentialTypes)
1184+
if (bridgedType->isConstraintType())
11881185
bridgedType = ExistentialType::get(bridgedType);
11891186

11901187
return { importedType,
@@ -1207,13 +1204,9 @@ namespace {
12071204
members.push_back(proto->getDeclaredInterfaceType());
12081205
}
12091206

1210-
importedType = ProtocolCompositionType::get(Impl.SwiftContext,
1211-
members,
1212-
/*HasExplicitAnyObject=*/false);
1213-
1214-
if (Impl.SwiftContext.LangOpts.EnableExplicitExistentialTypes) {
1215-
importedType = ExistentialType::get(importedType);
1216-
}
1207+
importedType = ExistentialType::get(
1208+
ProtocolCompositionType::get(Impl.SwiftContext, members,
1209+
/*HasExplicitAnyObject=*/false));
12171210
}
12181211

12191212
// Class or Class<P> maps to an existential metatype.
@@ -2508,9 +2501,7 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
25082501
bool paramIsIUO;
25092502
if (kind == SpecialMethodKind::NSDictionarySubscriptGetter &&
25102503
paramTy->isObjCIdType()) {
2511-
swiftParamTy = SwiftContext.getNSCopyingType();
2512-
if (SwiftContext.LangOpts.EnableExplicitExistentialTypes)
2513-
swiftParamTy = ExistentialType::get(swiftParamTy);
2504+
swiftParamTy = ExistentialType::get(SwiftContext.getNSCopyingType());
25142505
if (!swiftParamTy)
25152506
return {Type(), false};
25162507
if (optionalityOfParam != OTK_None)

lib/SILGen/SILGenType.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -842,10 +842,7 @@ static SILFunction *emitSelfConformanceWitness(SILGenModule &SGM,
842842
ProtocolConformanceRef(conformance));
843843

844844
// Open the protocol type.
845-
Type existential = protocolType;
846-
if (SGM.getASTContext().LangOpts.EnableExplicitExistentialTypes)
847-
existential = ExistentialType::get(protocolType);
848-
auto openedType = OpenedArchetypeType::get(existential);
845+
auto openedType = OpenedArchetypeType::get(protocol->getExistentialType());
849846

850847
// Form the substitutions for calling the witness.
851848
auto witnessSubs = SubstitutionMap::getProtocolSubstitutions(protocol,

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7009,12 +7009,7 @@ static bool isCastToExpressibleByNilLiteral(ConstraintSystem &cs, Type fromType,
70097009
if (!nilLiteral)
70107010
return false;
70117011

7012-
auto nilLiteralType = nilLiteral->getDeclaredType();
7013-
if (ctx.LangOpts.EnableExplicitExistentialTypes) {
7014-
nilLiteralType = ExistentialType::get(nilLiteralType);
7015-
}
7016-
7017-
return toType->isEqual(nilLiteralType) &&
7012+
return toType->isEqual(nilLiteral->getExistentialType()) &&
70187013
fromType->getOptionalObjectType();
70197014
}
70207015

lib/Sema/DerivedConformanceCodable.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,10 +1211,7 @@ static FuncDecl *deriveEncodable_encode(DerivedConformance &derived) {
12111211
// output: ()
12121212
// Create from the inside out:
12131213

1214-
auto encoderType = C.getEncoderType();
1215-
if (C.LangOpts.EnableExplicitExistentialTypes)
1216-
encoderType = ExistentialType::get(encoderType);
1217-
1214+
auto encoderType = ExistentialType::get(C.getEncoderType());
12181215
auto returnType = TupleType::getEmpty(C);
12191216

12201217
// Params: (Encoder)
@@ -1805,10 +1802,7 @@ static ValueDecl *deriveDecodable_init(DerivedConformance &derived) {
18051802
// Compute from the inside out:
18061803

18071804
// Params: (Decoder)
1808-
auto decoderType = C.getDecoderType();
1809-
if (C.LangOpts.EnableExplicitExistentialTypes)
1810-
decoderType = ExistentialType::get(decoderType);
1811-
1805+
auto decoderType = ExistentialType::get(C.getDecoderType());
18121806
auto *decoderParamDecl = new (C) ParamDecl(
18131807
SourceLoc(), SourceLoc(), C.Id_from,
18141808
SourceLoc(), C.Id_decoder, conformanceDC);

0 commit comments

Comments
 (0)