Skip to content

Commit d146b69

Browse files
authored
Merge pull request swiftlang#40666 from hborla/explicit-existential-fixes
[SE-0335] Enable explicit existential types.
2 parents 2daff9f + 626bea2 commit d146b69

File tree

85 files changed

+656
-290
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

+656
-290
lines changed

include/swift/AST/ASTContext.h

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

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

536536
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
537537
/** 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
@@ -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/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))
@@ -4648,8 +4648,6 @@ ERROR(unchecked_not_inheritance_clause,none,
46484648
ERROR(unchecked_not_existential,none,
46494649
"'unchecked' attribute cannot apply to non-protocol type %0", (Type))
46504650

4651-
WARNING(unnecessary_any,none,
4652-
"'any' is redundant on type %0", (Type))
46534651
ERROR(any_not_existential,none,
46544652
"'any' has no effect on %select{concrete type|type parameter}0 %1",
46554653
(bool, Type))

include/swift/AST/PrintOptions.h

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

279279
bool PrintImplicitAttrs = true;
280280

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

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
@@ -727,6 +727,9 @@ class alignas(1 << TypeAlignInBits) TypeBase
727727
return getRecursiveProperties().hasDependentMember();
728728
}
729729

730+
/// Whether this type represents a generic constraint.
731+
bool isConstraintType() const;
732+
730733
/// isExistentialType - Determines whether this type is an existential type,
731734
/// whose real (runtime) type is unknown but which is known to conform to
732735
/// some set of protocols. Protocol and protocol-conformance types are
@@ -2731,6 +2734,8 @@ class ExistentialMetatypeType : public AnyMetatypeType {
27312734
static bool classof(const TypeBase *T) {
27322735
return T->getKind() == TypeKind::ExistentialMetatype;
27332736
}
2737+
2738+
Type getExistentialInstanceType();
27342739

27352740
private:
27362741
ExistentialMetatypeType(Type T, const ASTContext *C,
@@ -5243,7 +5248,7 @@ class ExistentialType final : public TypeBase {
52435248
ConstraintType(constraintType) {}
52445249

52455250
public:
5246-
static ExistentialType *get(Type constraint);
5251+
static Type get(Type constraint);
52475252

52485253
Type getConstraintType() const { return ConstraintType; }
52495254

@@ -6335,6 +6340,15 @@ inline GenericTypeParamType *TypeBase::getRootGenericParam() {
63356340
return t->castTo<GenericTypeParamType>();
63366341
}
63376342

6343+
inline bool TypeBase::isConstraintType() const {
6344+
return getCanonicalType().isConstraintType();
6345+
}
6346+
6347+
inline bool CanType::isConstraintTypeImpl(CanType type) {
6348+
return (isa<ProtocolType>(type) ||
6349+
isa<ProtocolCompositionType>(type));
6350+
}
6351+
63386352
inline bool TypeBase::isExistentialType() {
63396353
return getCanonicalType().isExistentialType();
63406354
}
@@ -6441,6 +6455,8 @@ inline NominalTypeDecl *TypeBase::getNominalOrBoundGenericNominal() {
64416455
inline NominalTypeDecl *CanType::getNominalOrBoundGenericNominal() const {
64426456
if (auto Ty = dyn_cast<NominalOrBoundGenericNominalType>(*this))
64436457
return Ty->getDecl();
6458+
if (auto Ty = dyn_cast<ExistentialType>(*this))
6459+
return Ty->getConstraintType()->getNominalOrBoundGenericNominal();
64446460
return nullptr;
64456461
}
64466462

@@ -6449,6 +6465,9 @@ inline NominalTypeDecl *TypeBase::getAnyNominal() {
64496465
}
64506466

64516467
inline Type TypeBase::getNominalParent() {
6468+
if (auto existential = getAs<ExistentialType>())
6469+
return existential->getConstraintType()->getNominalParent();
6470+
64526471
return castTo<AnyGenericType>()->getParent();
64536472
}
64546473

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)