File tree Expand file tree Collapse file tree 4 files changed +55
-0
lines changed Expand file tree Collapse file tree 4 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -385,6 +385,7 @@ class CanType : public Type {
385
385
386
386
static bool isReferenceTypeImpl (CanType type, const GenericSignatureImpl *sig,
387
387
bool functionsCount);
388
+ static bool isConstraintTypeImpl (CanType type);
388
389
static bool isExistentialTypeImpl (CanType type);
389
390
static bool isAnyExistentialTypeImpl (CanType type);
390
391
static bool isObjCExistentialTypeImpl (CanType type);
@@ -457,6 +458,10 @@ class CanType : public Type {
457
458
/* functions count*/ false );
458
459
}
459
460
461
+ bool isConstraintType () const {
462
+ return isConstraintTypeImpl (*this );
463
+ }
464
+
460
465
// / Is this type existential?
461
466
bool isExistentialType () const {
462
467
return isExistentialTypeImpl (*this );
Original file line number Diff line number Diff line change @@ -727,6 +727,9 @@ class alignas(1 << TypeAlignInBits) TypeBase
727
727
return getRecursiveProperties ().hasDependentMember ();
728
728
}
729
729
730
+ // / Whether this type represents a generic constraint.
731
+ bool isConstraintType () const ;
732
+
730
733
// / isExistentialType - Determines whether this type is an existential type,
731
734
// / whose real (runtime) type is unknown but which is known to conform to
732
735
// / some set of protocols. Protocol and protocol-conformance types are
@@ -6335,6 +6338,15 @@ inline GenericTypeParamType *TypeBase::getRootGenericParam() {
6335
6338
return t->castTo <GenericTypeParamType>();
6336
6339
}
6337
6340
6341
+ inline bool TypeBase::isConstraintType () const {
6342
+ return getCanonicalType ().isConstraintType ();
6343
+ }
6344
+
6345
+ inline bool CanType::isConstraintTypeImpl (CanType type) {
6346
+ return (isa<ProtocolType>(type) ||
6347
+ isa<ProtocolCompositionType>(type));
6348
+ }
6349
+
6338
6350
inline bool TypeBase::isExistentialType () {
6339
6351
return getCanonicalType ().isExistentialType ();
6340
6352
}
Original file line number Diff line number Diff line change @@ -4783,6 +4783,23 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
4783
4783
auto memberType = TypeChecker::substMemberTypeWithBase (DC->getParentModule (),
4784
4784
typeDecl, Adoptee);
4785
4785
4786
+ // Type witnesses that resolve to constraint types are always
4787
+ // existential types. This can only happen when the type witness
4788
+ // is explicitly written with a type alias. The type alias itself
4789
+ // is still a constraint type because it can be used as both a
4790
+ // type witness and as a generic constraint.
4791
+ //
4792
+ // With SE-0335, using a type alias as both a type witness and a generic
4793
+ // constraint will be disallowed in Swift 6, because existential types
4794
+ // must be explicit, and a generic constraint isn't a valid type witness.
4795
+ //
4796
+ // Note that Any and AnyObject aren't yet resolved using ExistentialType.
4797
+ if (getASTContext ().LangOpts .EnableExplicitExistentialTypes &&
4798
+ memberType->isConstraintType () &&
4799
+ !(memberType->isAny () || memberType->isAnyObject ())) {
4800
+ memberType = ExistentialType::get (memberType);
4801
+ }
4802
+
4786
4803
if (!viableTypes.insert (memberType->getCanonicalType ()).second )
4787
4804
continue ;
4788
4805
Original file line number Diff line number Diff line change @@ -164,3 +164,24 @@ func testMetatypes() {
164
164
}
165
165
166
166
func generic< T: any P1 > ( _ t: T ) { } // expected-error {{type 'T' constrained to non-protocol, non-class type 'any P1'}}
167
+
168
+ protocol RawRepresentable {
169
+ associatedtype RawValue
170
+ var rawValue : RawValue { get }
171
+ }
172
+
173
+ enum E1 : RawRepresentable {
174
+ typealias RawValue = P1
175
+
176
+ var rawValue : P1 {
177
+ return ConcreteComposition ( )
178
+ }
179
+ }
180
+
181
+ enum E2 : RawRepresentable {
182
+ typealias RawValue = any P1
183
+
184
+ var rawValue : any P1 {
185
+ return ConcreteComposition ( )
186
+ }
187
+ }
You can’t perform that action at this time.
0 commit comments