Skip to content

Commit 33996de

Browse files
authored
Merge pull request swiftlang#14649 from xedin/objc-existential-type-by-name
[Runtime] Existential types marked as @objc should satisfy class requirement
2 parents f165bce + 73f09ec commit 33996de

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

include/swift/Runtime/Metadata.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,8 @@ struct TargetMetadata {
949949
return asWords + description->getGenericArgumentOffset(this);
950950
}
951951

952+
bool satisfiesClassConstraint() const;
953+
952954
#if SWIFT_OBJC_INTEROP
953955
/// Get the ObjC class object for this type if it has one, or return null if
954956
/// the type is not a class (or not a class with a class object).

stdlib/public/runtime/Metadata.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3015,3 +3015,13 @@ void MetadataAllocator::Deallocate(const void *allocation, size_t size) {
30153015
void *swift::allocateMetadata(size_t size, size_t alignment) {
30163016
return MetadataAllocator().Allocate(size, alignment);
30173017
}
3018+
3019+
template<>
3020+
bool Metadata::satisfiesClassConstraint() const {
3021+
// existential types marked with @objc satisfy class requirement.
3022+
if (auto *existential = dyn_cast<ExistentialTypeMetadata>(this))
3023+
return existential->isObjC();
3024+
3025+
// or it's a class.
3026+
return isAnyClass();
3027+
}

stdlib/public/runtime/ProtocolConformance.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,8 @@ bool swift::_checkGenericRequirements(
777777
case GenericRequirementKind::Layout: {
778778
switch (req.getLayout()) {
779779
case GenericRequirementLayoutKind::Class:
780-
// Check whether the subject type is a class.
781-
if (!subjectType->isAnyClass()) return true;
780+
if (!subjectType->satisfiesClassConstraint())
781+
return true;
782782
continue;
783783
}
784784

test/stdlib/RuntimeObjC.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,16 @@ Runtime.test("Generic class ObjC runtime names") {
386386
GenericEnum<GenericEnum<Int>>>.self))
387387
}
388388

389+
@objc protocol P {}
390+
struct AnyObjStruct<T: AnyObject> {}
391+
389392
Runtime.test("typeByName") {
390393
// Make sure we don't crash if we have foreign classes in the
391394
// table -- those don't have NominalTypeDescriptors
392395
print(CFArray.self)
393396
expectTrue(_typeByName("a.SomeClass") == SomeClass.self)
394397
expectTrue(_typeByName("DoesNotExist") == nil)
398+
expectTrue(_typeByName("1a12AnyObjStructVyAA1P_pG") == AnyObjStruct<P>.self)
395399
}
396400

397401
Runtime.test("casting AnyObject to class metatypes") {

0 commit comments

Comments
 (0)