Skip to content

Commit ea8171b

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

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
@@ -526,7 +526,6 @@ class ASTContext final {
526526

527527
/// Retrieve the declaration of Swift.Error.
528528
ProtocolDecl *getErrorDecl() const;
529-
CanType getExceptionType() const;
530529
CanType getErrorExistentialType() const;
531530

532531
#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
@@ -4297,6 +4297,11 @@ class ProtocolDecl final : public NominalTypeDecl {
42974297
/// not exist.
42984298
AssociatedTypeDecl *getAssociatedType(Identifier name) const;
42994299

4300+
/// Returns the existential type for this protocol.
4301+
Type getExistentialType() const {
4302+
return ExistentialType::get(getDeclaredInterfaceType());
4303+
}
4304+
43004305
/// Walk this protocol and all of the protocols inherited by this protocol,
43014306
/// transitively, invoking the callback function for each protocol.
43024307
///

include/swift/AST/Types.h

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

52205220
public:
5221-
static ExistentialType *get(Type constraint);
5221+
static Type get(Type constraint);
52225222

52235223
Type getConstraintType() const { return ConstraintType; }
52245224

lib/AST/ASTContext.cpp

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

859-
CanType ASTContext::getExceptionType() const {
859+
CanType ASTContext::getErrorExistentialType() const {
860860
if (auto exn = getErrorDecl()) {
861-
return exn->getDeclaredInterfaceType()->getCanonicalType();
861+
return exn->getExistentialType()->getCanonicalType();
862862
} else {
863863
// Use Builtin.NativeObject just as a stand-in.
864864
return TheNativeObjectType;
@@ -869,16 +869,6 @@ ProtocolDecl *ASTContext::getErrorDecl() const {
869869
return getProtocol(KnownProtocolKind::Error);
870870
}
871871

872-
CanType ASTContext::getErrorExistentialType() const {
873-
Type errorType = getExceptionType();
874-
if (LangOpts.EnableExplicitExistentialTypes &&
875-
errorType->isConstraintType()) {
876-
errorType = ExistentialType::get(errorType);
877-
}
878-
879-
return errorType->getCanonicalType();
880-
}
881-
882872
EnumElementDecl *ASTContext::getOptionalSomeDecl() const {
883873
if (!getImpl().OptionalSomeDecl)
884874
getImpl().OptionalSomeDecl = getOptionalDecl()->getUniqueElement(/*hasVal*/true);
@@ -2336,7 +2326,7 @@ ASTContext::getSelfConformance(ProtocolDecl *protocol) {
23362326
auto &entry = selfConformances[protocol];
23372327
if (!entry) {
23382328
entry = new (*this, AllocationArena::Permanent)
2339-
SelfProtocolConformance(protocol->getDeclaredInterfaceType());
2329+
SelfProtocolConformance(protocol->getExistentialType());
23402330
}
23412331
return entry;
23422332
}
@@ -3364,15 +3354,7 @@ ExistentialMetatypeType::ExistentialMetatypeType(Type T,
33643354
}
33653355

33663356
Type ExistentialMetatypeType::getExistentialInstanceType() {
3367-
auto instanceType = getInstanceType();
3368-
// Note that Any and AnyObject don't yet use ExistentialType.
3369-
if (getASTContext().LangOpts.EnableExplicitExistentialTypes &&
3370-
!instanceType->is<ExistentialMetatypeType>() &&
3371-
!(instanceType->isAny() || instanceType->isAnyObject())) {
3372-
instanceType = ExistentialType::get(instanceType);
3373-
}
3374-
3375-
return instanceType;
3357+
return ExistentialType::get(getInstanceType());
33763358
}
33773359

33783360
ModuleType *ModuleType::get(ModuleDecl *M) {
@@ -4121,11 +4103,22 @@ ProtocolType::ProtocolType(ProtocolDecl *TheDecl, Type Parent,
41214103
RecursiveTypeProperties properties)
41224104
: NominalType(TypeKind::Protocol, &Ctx, TheDecl, Parent, properties) { }
41234105

4124-
ExistentialType *ExistentialType::get(Type constraint) {
4106+
Type ExistentialType::get(Type constraint) {
4107+
auto &C = constraint->getASTContext();
4108+
if (!C.LangOpts.EnableExplicitExistentialTypes)
4109+
return constraint;
4110+
4111+
// FIXME: Any and AnyObject don't yet use ExistentialType.
4112+
if (constraint->isAny() || constraint->isAnyObject())
4113+
return constraint;
4114+
4115+
// ExistentialMetatypeType is already an existential type.
4116+
if (constraint->is<ExistentialMetatypeType>())
4117+
return constraint;
4118+
41254119
auto properties = constraint->getRecursiveProperties();
41264120
auto arena = getArena(properties);
41274121

4128-
auto &C = constraint->getASTContext();
41294122
auto &entry = C.getImpl().getArena(arena).ExistentialTypes[constraint];
41304123
if (entry)
41314124
return entry;

lib/AST/Type.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,7 @@ CanType TypeBase::computeCanonicalType() {
14621462
case TypeKind::Existential: {
14631463
auto *existential = cast<ExistentialType>(this);
14641464
auto constraint = existential->getConstraintType()->getCanonicalType();
1465-
Result = ExistentialType::get(constraint);
1465+
Result = ExistentialType::get(constraint).getPointer();
14661466
break;
14671467
}
14681468
case TypeKind::ExistentialMetatype: {
@@ -3208,10 +3208,7 @@ Type ArchetypeType::getExistentialType() const {
32083208
auto constraint = ProtocolCompositionType::get(
32093209
ctx, constraintTypes, requiresClass());
32103210

3211-
if (ctx.LangOpts.EnableExplicitExistentialTypes)
3212-
return ExistentialType::get(constraint);
3213-
3214-
return constraint;
3211+
return ExistentialType::get(constraint);
32153212
}
32163213

32173214
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
@@ -1019,12 +1019,10 @@ namespace {
10191019
if (memberTypes.empty())
10201020
hasExplicitAnyObject = true;
10211021

1022-
Type importedTypeArg = ProtocolCompositionType::get(
1023-
Impl.SwiftContext, memberTypes,
1024-
hasExplicitAnyObject);
1025-
if (Impl.SwiftContext.LangOpts.EnableExplicitExistentialTypes) {
1026-
importedTypeArg = ExistentialType::get(importedTypeArg);
1027-
}
1022+
Type importedTypeArg = ExistentialType::get(
1023+
ProtocolCompositionType::get(
1024+
Impl.SwiftContext, memberTypes,
1025+
hasExplicitAnyObject));
10281026
importedTypeArgs.push_back(importedTypeArg);
10291027
}
10301028
}
@@ -1153,8 +1151,7 @@ namespace {
11531151
}
11541152
}
11551153

1156-
if (bridgedType->isConstraintType() &&
1157-
Impl.SwiftContext.LangOpts.EnableExplicitExistentialTypes)
1154+
if (bridgedType->isConstraintType())
11581155
bridgedType = ExistentialType::get(bridgedType);
11591156

11601157
return { importedType,
@@ -1177,13 +1174,9 @@ namespace {
11771174
members.push_back(proto->getDeclaredInterfaceType());
11781175
}
11791176

1180-
importedType = ProtocolCompositionType::get(Impl.SwiftContext,
1181-
members,
1182-
/*HasExplicitAnyObject=*/false);
1183-
1184-
if (Impl.SwiftContext.LangOpts.EnableExplicitExistentialTypes) {
1185-
importedType = ExistentialType::get(importedType);
1186-
}
1177+
importedType = ExistentialType::get(
1178+
ProtocolCompositionType::get(Impl.SwiftContext, members,
1179+
/*HasExplicitAnyObject=*/false));
11871180
}
11881181

11891182
// Class or Class<P> maps to an existential metatype.
@@ -2460,9 +2453,7 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
24602453
bool paramIsIUO;
24612454
if (kind == SpecialMethodKind::NSDictionarySubscriptGetter &&
24622455
paramTy->isObjCIdType()) {
2463-
swiftParamTy = SwiftContext.getNSCopyingType();
2464-
if (SwiftContext.LangOpts.EnableExplicitExistentialTypes)
2465-
swiftParamTy = ExistentialType::get(swiftParamTy);
2456+
swiftParamTy = ExistentialType::get(SwiftContext.getNSCopyingType());
24662457
if (!swiftParamTy)
24672458
return {Type(), false};
24682459
if (optionalityOfParam != OTK_None)

lib/SILGen/SILGenType.cpp

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

833833
// Open the protocol type.
834-
Type existential = protocolType;
835-
if (SGM.getASTContext().LangOpts.EnableExplicitExistentialTypes)
836-
existential = ExistentialType::get(protocolType);
837-
auto openedType = OpenedArchetypeType::get(existential);
834+
auto openedType = OpenedArchetypeType::get(protocol->getExistentialType());
838835

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

lib/Sema/CSSimplify.cpp

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

6852-
auto nilLiteralType = nilLiteral->getDeclaredType();
6853-
if (ctx.LangOpts.EnableExplicitExistentialTypes) {
6854-
nilLiteralType = ExistentialType::get(nilLiteralType);
6855-
}
6856-
6857-
return toType->isEqual(nilLiteralType) &&
6852+
return toType->isEqual(nilLiteral->getExistentialType()) &&
68586853
fromType->getOptionalObjectType();
68596854
}
68606855

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

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4771,11 +4771,7 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
47714771
// With SE-0335, using a type alias as both a type witness and a generic
47724772
// constraint will be disallowed in Swift 6, because existential types
47734773
// must be explicit, and a generic constraint isn't a valid type witness.
4774-
//
4775-
// Note that Any and AnyObject aren't yet resolved using ExistentialType.
4776-
if (getASTContext().LangOpts.EnableExplicitExistentialTypes &&
4777-
memberType->isConstraintType() &&
4778-
!(memberType->isAny() || memberType->isAnyObject())) {
4774+
if (memberType->isConstraintType()) {
47794775
memberType = ExistentialType::get(memberType);
47804776
}
47814777

lib/Sema/TypeCheckType.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3389,9 +3389,7 @@ TypeResolver::resolveIdentifierType(IdentTypeRepr *IdType,
33893389
return ErrorType::get(getASTContext());
33903390
}
33913391

3392-
// FIXME: Don't use ExistentialType for AnyObject for now.
3393-
if (result->isConstraintType() && !result->isAnyObject() &&
3394-
getASTContext().LangOpts.EnableExplicitExistentialTypes &&
3392+
if (result->isConstraintType() &&
33953393
options.isConstraintImplicitExistential()) {
33963394
return ExistentialType::get(result);
33973395
}
@@ -3761,10 +3759,8 @@ TypeResolver::resolveCompositionType(CompositionTypeRepr *repr,
37613759
auto composition =
37623760
ProtocolCompositionType::get(getASTContext(), Members,
37633761
/*HasExplicitAnyObject=*/false);
3764-
if (getASTContext().LangOpts.EnableExplicitExistentialTypes &&
3765-
options.isConstraintImplicitExistential() &&
3766-
!composition->isAny()) {
3767-
composition = ExistentialType::get(composition);
3762+
if (options.isConstraintImplicitExistential()) {
3763+
return ExistentialType::get(composition);
37683764
}
37693765
return composition;
37703766
}
@@ -3792,7 +3788,6 @@ TypeResolver::resolveExistentialType(ExistentialTypeRepr *repr,
37923788
diagnose(repr->getLoc(), diag::unnecessary_any,
37933789
constraintType)
37943790
.fixItRemove({anyStart, anyEnd});
3795-
return constraintType;
37963791
}
37973792

37983793
return ExistentialType::get(constraintType);

0 commit comments

Comments
 (0)