Skip to content

[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

Merged
merged 2 commits into from
Aug 1, 2016

Conversation

lilyball
Copy link
Contributor

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 by private 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:

  • Test pull request on Swift continuous integration.

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

Platform Comment
All supported platforms @swift-ci Please smoke test
All supported platforms @swift-ci Please smoke test and merge
OS X platform @swift-ci Please smoke test OS X platform
Linux platform @swift-ci Please smoke test Linux platform

A smoke test on macOS does the following:

  1. Builds the compiler incrementally.
  2. Builds the standard library only for macOS. Simulator standard libraries and
    device standard libraries are not built.
  3. lldb is not built.
  4. The test and validation-test targets are run only for macOS. The optimized
    version of these tests are not run.

A smoke test on Linux does the following:

  1. Builds the compiler incrementally.
  2. Builds the standard library incrementally.
  3. lldb is built incrementally.
  4. The swift test and validation-test targets are run. The optimized version of these
    tests are not run.
  5. lldb is tested.

Validation Testing

Platform Comment
All supported platforms @swift-ci Please test
All supported platforms @swift-ci Please test and merge
OS X platform @swift-ci Please test OS X platform
OS X platform @swift-ci Please benchmark
Linux platform @swift-ci Please test Linux platform

Lint Testing

Language Comment
Python @swift-ci Please Python lint

Note: Only members of the Apple organization can trigger swift-ci.

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.
@DougGregor
Copy link
Member

Nifty. @jrose-apple , any concerns with this?

@jrose-apple
Copy link
Contributor

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.

@lilyball
Copy link
Contributor Author

@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 [[Base alloc] initWithX:42] and [[ChildB alloc] initWithX:42] and I'm not allowed to invoke [[ChildA alloc] initWithX:42].

@jrose-apple
Copy link
Contributor

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) {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.
@lilyball lilyball force-pushed the printasobjc_unavailable branch from efa2ada to d0a056f Compare July 29, 2016 20:50
@jrose-apple
Copy link
Contributor

@swift-ci Please test OS X platform

@jrose-apple
Copy link
Contributor

Over to Ted to merge. (As a PrintAsObjC change for classes, there should be no effect on Linux.)

@tkremenek tkremenek merged commit 0473e99 into swiftlang:master Aug 1, 2016
@lilyball lilyball deleted the printasobjc_unavailable branch August 1, 2016 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants