-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[PrintAsObjC] Add unavailable attribute to unavailable obj-c initializers #3852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Initializers that aren't inherited by subclasses cannot be called, so we should make this visible to Obj-C. Due to SR-2211, non-inherited convenience initializers do not get this same treatment.
Nifty. @jrose-apple , any concerns with this? |
There is a problem, which is that one deeper level of subclass can reintroduce the initializer, and I'm pretty sure we don't handle that correctly in Clang. I'm not sure we handle that correctly in Swift either, though. |
@jrose-apple Do you mean something like this? @interface Base : NSObject
- (nonnull instancetype)initWithX:(NSInteger)x;
@end
@interface ChildA : Base
- (nonnull instancetype)initWithX:(NSInteger)x __attribute__((unavailable));
@end
@interface ChildB : ChildA
- (nonnull instancetype)initWithX:(NSInteger)x;
@end This works perfectly fine in my testing. I'm able to invoke |
Huh, okay, I must be misremembering. In that case, looks good to me! @swift-ci Please test OS X platform |
@@ -479,7 +487,9 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>, | |||
|
|||
// Swift designated initializers are Objective-C designated initializers. | |||
if (auto ctor = dyn_cast<ConstructorDecl>(AFD)) { | |||
if (ctor->isDesignatedInit() && | |||
if (ctor->hasStubImplementation() || ctor->getFormalAccess() < minRequiredAccess) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although…can you add a comment here saying this will only happen if the overridden initializer had the required access?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. While I'm at it I'm going to tweak the tests as well to test the grandchild-reintroduces-initializer case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
When a public initializer is overridden with a private one, we need to mark these as unavailable to Obj-C as they're not supposed to be callable even though they do exist.
efa2ada
to
d0a056f
Compare
@swift-ci Please test OS X platform |
Over to Ted to merge. (As a PrintAsObjC change for classes, there should be no effect on Linux.) |
What's in this pull request?
Fixes PrintAsObjC to declare unavailable initializers with
__attribute__((unavailable))
so they can't be called from Obj-C. This covers non-inherited designated initializers as well as initializers overridden byprivate
declarations.Notably, this does not include non-inherited convenience initializers. This is because non-inherited convenience initializers do not get Obj-C stub implementations (filed as SR-2211).
Resolved bug number: (SR-746)
Before merging this pull request to apple/swift repository:
Triggering Swift CI
The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:
Smoke Testing
A smoke test on macOS does the following:
device standard libraries are not built.
version of these tests are not run.
A smoke test on Linux does the following:
tests are not run.
Validation Testing
Lint Testing
Note: Only members of the Apple organization can trigger swift-ci.