Skip to content

Commit c12e31f

Browse files
authored
Merge pull request #40804 from hborla/5.6-enable-existential-any
[5.6][SE-0335] Enable explicit existential types.
2 parents fa43d48 + 8fe5396 commit c12e31f

File tree

85 files changed

+678
-297
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+678
-297
lines changed

include/swift/AST/ASTContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ class ASTContext final {
526526

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

531531
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
532532
/** Retrieve the declaration of Swift.NAME. */ \

include/swift/AST/ASTDemangler.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ASTBuilder {
6666

6767
Demangle::NodeFactory &getNodeFactory() { return Factory; }
6868

69-
Type decodeMangledType(NodePointer node);
69+
Type decodeMangledType(NodePointer node, bool forRequirement = true);
7070
Type createBuiltinType(StringRef builtinName, StringRef mangledName);
7171

7272
TypeDecl *createTypeDecl(NodePointer node);
@@ -109,7 +109,8 @@ class ASTBuilder {
109109

110110
Type createProtocolCompositionType(ArrayRef<ProtocolDecl *> protocols,
111111
Type superclass,
112-
bool isClassBound);
112+
bool isClassBound,
113+
bool forRequirement = true);
113114

114115
Type createExistentialMetatypeType(Type instance,
115116
Optional<Demangle::ImplMetatypeRepresentation> repr=None);

include/swift/AST/ASTSynthesis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ inline Type synthesizeType(SynthesisContext &SC,
5656
switch (kind) {
5757
case _any: return SC.Context.TheAnyType;
5858
case _bridgeObject: return SC.Context.TheBridgeObjectType;
59-
case _error: return SC.Context.getExceptionType();
59+
case _error: return SC.Context.getErrorExistentialType();
6060
case _executor: return SC.Context.TheExecutorType;
6161
case _job: return SC.Context.TheJobType;
6262
case _nativeObject: return SC.Context.TheNativeObjectType;

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/DiagnosticsSema.def

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,8 +2441,8 @@ ERROR(protocol_composition_one_class,none,
24412441
"contains class %1", (Type, Type))
24422442

24432443
ERROR(requires_conformance_nonprotocol,none,
2444-
"type %0 constrained to non-protocol, non-class type %1",
2445-
(Type, Type))
2444+
"type %0 constrained to non-protocol, non-class type '%1'",
2445+
(Type, StringRef))
24462446
NOTE(requires_conformance_nonprotocol_fixit,none,
24472447
"use '%0 == %1' to require '%0' to be '%1'",
24482448
(StringRef, StringRef))
@@ -4643,8 +4643,6 @@ ERROR(unchecked_not_inheritance_clause,none,
46434643
ERROR(unchecked_not_existential,none,
46444644
"'unchecked' attribute cannot apply to non-protocol type %0", (Type))
46454645

4646-
WARNING(unnecessary_any,none,
4647-
"'any' is redundant on type %0", (Type))
46484646
ERROR(any_not_existential,none,
46494647
"'any' has no effect on %select{concrete type|type parameter}0 %1",
46504648
(bool, Type))

include/swift/AST/PrintOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ struct PrintOptions {
276276

277277
bool PrintImplicitAttrs = true;
278278

279+
/// Whether to print the \c any keyword for existential
280+
/// types.
281+
bool PrintExplicitAny = false;
282+
279283
/// Whether to skip keywords with a prefix of underscore such as __consuming.
280284
bool SkipUnderscoredKeywords = false;
281285

include/swift/AST/ProtocolConformance.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ class SelfProtocolConformance : public RootProtocolConformance {
660660
public:
661661
/// Get the protocol being conformed to.
662662
ProtocolDecl *getProtocol() const {
663-
return getType()->castTo<ProtocolType>()->getDecl();
663+
return dyn_cast<ProtocolDecl>(getType()->getAnyNominal());
664664
}
665665

666666
/// Get the declaration context in which this conformance was declared.

include/swift/AST/Type.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ class CanType : public Type {
385385

386386
static bool isReferenceTypeImpl(CanType type, const GenericSignatureImpl *sig,
387387
bool functionsCount);
388+
static bool isConstraintTypeImpl(CanType type);
388389
static bool isExistentialTypeImpl(CanType type);
389390
static bool isAnyExistentialTypeImpl(CanType type);
390391
static bool isObjCExistentialTypeImpl(CanType type);
@@ -457,6 +458,10 @@ class CanType : public Type {
457458
/*functions count*/ false);
458459
}
459460

461+
bool isConstraintType() const {
462+
return isConstraintTypeImpl(*this);
463+
}
464+
460465
/// Is this type existential?
461466
bool isExistentialType() const {
462467
return isExistentialTypeImpl(*this);

include/swift/AST/Types.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,9 @@ class alignas(1 << TypeAlignInBits) TypeBase
703703
return getRecursiveProperties().hasDependentMember();
704704
}
705705

706+
/// Whether this type represents a generic constraint.
707+
bool isConstraintType() const;
708+
706709
/// isExistentialType - Determines whether this type is an existential type,
707710
/// whose real (runtime) type is unknown but which is known to conform to
708711
/// some set of protocols. Protocol and protocol-conformance types are
@@ -2704,6 +2707,8 @@ class ExistentialMetatypeType : public AnyMetatypeType {
27042707
static bool classof(const TypeBase *T) {
27052708
return T->getKind() == TypeKind::ExistentialMetatype;
27062709
}
2710+
2711+
Type getExistentialInstanceType();
27072712

27082713
private:
27092714
ExistentialMetatypeType(Type T, const ASTContext *C,
@@ -5213,7 +5218,7 @@ class ExistentialType final : public TypeBase {
52135218
ConstraintType(constraintType) {}
52145219

52155220
public:
5216-
static ExistentialType *get(Type constraint);
5221+
static Type get(Type constraint);
52175222

52185223
Type getConstraintType() const { return ConstraintType; }
52195224

@@ -6181,6 +6186,15 @@ inline GenericTypeParamType *TypeBase::getRootGenericParam() {
61816186
return t->castTo<GenericTypeParamType>();
61826187
}
61836188

6189+
inline bool TypeBase::isConstraintType() const {
6190+
return getCanonicalType().isConstraintType();
6191+
}
6192+
6193+
inline bool CanType::isConstraintTypeImpl(CanType type) {
6194+
return (isa<ProtocolType>(type) ||
6195+
isa<ProtocolCompositionType>(type));
6196+
}
6197+
61846198
inline bool TypeBase::isExistentialType() {
61856199
return getCanonicalType().isExistentialType();
61866200
}
@@ -6287,6 +6301,8 @@ inline NominalTypeDecl *TypeBase::getNominalOrBoundGenericNominal() {
62876301
inline NominalTypeDecl *CanType::getNominalOrBoundGenericNominal() const {
62886302
if (auto Ty = dyn_cast<NominalOrBoundGenericNominalType>(*this))
62896303
return Ty->getDecl();
6304+
if (auto Ty = dyn_cast<ExistentialType>(*this))
6305+
return Ty->getConstraintType()->getNominalOrBoundGenericNominal();
62906306
return nullptr;
62916307
}
62926308

@@ -6295,6 +6311,9 @@ inline NominalTypeDecl *TypeBase::getAnyNominal() {
62956311
}
62966312

62976313
inline Type TypeBase::getNominalParent() {
6314+
if (auto existential = getAs<ExistentialType>())
6315+
return existential->getConstraintType()->getNominalParent();
6316+
62986317
return castTo<AnyGenericType>()->getParent();
62996318
}
63006319

include/swift/Basic/LangOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ namespace swift {
315315

316316
/// Enable support for explicit existential types via the \c any
317317
/// keyword.
318-
bool EnableExplicitExistentialTypes = false;
318+
bool EnableExplicitExistentialTypes = true;
319319

320320
/// Enable experimental flow-sensitive concurrent captures.
321321
bool EnableExperimentalFlowSensitiveConcurrentCaptures = false;

0 commit comments

Comments
 (0)