Skip to content

Commit 2345a54

Browse files
authored
Make sure artificial subclasses work with '===' and casting (#17765)
Dynamic subclasses aren't /really/ valid Swift type metadata, but they can still be used as values of type AnyClass. Make sure we don't assert when that happens. No intended functionality change.
1 parent 95c80cf commit 2345a54

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

stdlib/public/runtime/Metadata.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ swift::swift_getObjCClassFromMetadata(const Metadata *theMetadata) {
593593

594594
// Otherwise, the input should already be a Swift class object.
595595
auto theClass = cast<ClassMetadata>(theMetadata);
596-
assert(theClass->isTypeMetadata() && !theClass->isArtificialSubclass());
596+
assert(theClass->isTypeMetadata());
597597
return theClass;
598598
}
599599

test/Interpreter/SDK/KVO.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,20 @@ foo.addObserver(foo, forKeyPath: "foo", options: [], context: &kvoContext)
5555
let bar = foo.foo
5656
// CHECK-NEXT: 0
5757
print(bar)
58+
59+
let fooClass: AnyClass = object_getClass(foo)!
60+
precondition(fooClass !== Foo.self, "no KVO subclass?")
61+
precondition(fooClass is Foo.Type, "improper KVO subclass")
62+
precondition(!(fooClass is Observer.Type), "improper KVO subclass")
63+
64+
let fooClassAsObject: AnyObject = fooClass
65+
precondition(fooClassAsObject !== Foo.self, "no KVO subclass?")
66+
precondition(fooClassAsObject is Foo.Type, "improper KVO subclass")
67+
precondition(!(fooClassAsObject is Observer.Type), "improper KVO subclass")
68+
69+
let fooClassAsAny: Any = fooClass
70+
precondition(fooClassAsAny is Foo.Type, "improper KVO subclass")
71+
precondition(!(fooClassAsAny is Observer.Type), "improper KVO subclass")
72+
73+
// CHECK-NEXT: class metadata checks okay
74+
print("class metadata checks okay")

0 commit comments

Comments
 (0)