File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -752,6 +752,10 @@ class alignas(1 << TypeAlignInBits) TypeBase
752
752
return getRecursiveProperties ().hasOpenedExistential ();
753
753
}
754
754
755
+ // / True if this type is an existential or an archetype which may be an
756
+ // / existential.
757
+ bool canBeExistential ();
758
+
755
759
// / Determine whether the type involves an opened element archetype.
756
760
bool hasElementArchetype () const {
757
761
return getRecursiveProperties ().hasElementArchetype ();
Original file line number Diff line number Diff line change @@ -616,6 +616,29 @@ bool TypeBase::isTypeVariableOrMember() {
616
616
return getDependentMemberRoot ()->is <TypeVariableType>();
617
617
}
618
618
619
+ bool TypeBase::canBeExistential () {
620
+ if (isAnyExistentialType ())
621
+ return true ;
622
+
623
+ Type ty (this );
624
+ // Unwrap (potentially multiple levels of) metatypes.
625
+ while (auto *mt = ty->getAs <MetatypeType>())
626
+ ty = mt->getInstanceType ();
627
+
628
+ if (auto *archeTy = ty->getAs <ArchetypeType>()) {
629
+ // Only if all conformances are self-conforming protocols, the archetype
630
+ // may be an existential.
631
+ for (auto *proto : archeTy->getConformsTo ()) {
632
+ if (!proto->existentialConformsToSelf ())
633
+ return false ;
634
+ }
635
+ // If there are no requirements on the archetype at all (`getConformsTo`
636
+ // is empty), the archetype can still be `Any` and we have to return true.
637
+ return true ;
638
+ }
639
+ return false ;
640
+ }
641
+
619
642
bool TypeBase::isTypeParameter () {
620
643
return getDependentMemberRoot ()->is <GenericTypeParamType>();
621
644
}
You can’t perform that action at this time.
0 commit comments