Skip to content

Commit 7c3cf1d

Browse files
committed
ClangImporter: Fix mirroring of instance properties as static methods on NSObject
Because all metaclasses ultimately inherit from NSObject, instance members of NSObject are also visible as static members of NSObject. If the instance member is a property, we import the getter as an ordinary static method, and not a static property. The lazy loading path normally checks for the presence of alternate decls with the same name, but it was failing to do this check if the imported decl was a property and the alternate decl was attached to the accessor and not the property itself. This wasn't a problem until recently, because we weren't lazy loading members of NSObject itself, since it had protocol conformances; now that we are, this problem was exposed. Fixes <rdar://problem/59170514>.
1 parent 5f26e93 commit 7c3cf1d

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,6 +3787,17 @@ ClangImporter::Implementation::loadNamedMembers(
37873787
Members.push_back(V);
37883788
}
37893789
}
3790+
3791+
// If the property's accessors have alternate decls, we might have
3792+
// to import those too.
3793+
if (auto *ASD = dyn_cast<AbstractStorageDecl>(TD)) {
3794+
for (auto *AD : ASD->getAllAccessors()) {
3795+
for (auto *D : getAlternateDecls(AD)) {
3796+
if (D->getBaseName() == N)
3797+
Members.push_back(D);
3798+
}
3799+
}
3800+
}
37903801
}
37913802
}
37923803

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-sil %s -verify
2+
// REQUIRES: objc_interop
3+
4+
import Foundation
5+
6+
func mirrorInstancePropertyAsStaticMethod() {
7+
// Instance properties are mirrored as static _methods_. Make sure this works.
8+
let _: AnyClass = NSObject.classForCoder()
9+
}

test/Inputs/clang-importer-sdk/usr/include/objc/NSObject.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
@property (readonly) NSInteger hash;
2828
@end
2929

30+
@interface NSObject (Coding)
31+
- (Class)classForCoder;
32+
@end
33+
3034
@interface A : NSObject
3135
- (int)method:(int)arg withDouble:(double)d;
3236
+ (int)classMethod;

0 commit comments

Comments
 (0)