Skip to content

[SR-1894] Consider protocol metatypes as not @objc compatible. #4029

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 13, 2016

Conversation

aleksgapp
Copy link
Contributor

What's in this pull request?

StarterBug fix.

Resolved bug number: (SR-1894)


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.

@jrose-apple
Copy link
Contributor

Looking good! A few comments.

@jckarter
Copy link
Contributor

jckarter commented Aug 4, 2016

A single-@objc-protocol metatype ought to be Bridged-@objc representable.


// Consider protocol metatypes as not Objective-C representable.
if (isa<MetatypeType>(metatype) &&
instanceType->getKind() == TypeKind::Protocol)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good but it won't catch a typealias to a protocol, whose TypeKind will be something else. The best mechanism for dealing with that is theTypeBase::getAs function, which will look for a "canonical" type instead of just checking the outermost type.

Can you add a test for typealiases as well?

typealias AnotherNSCoding = NSCoding
typealias MetaNSCoding = NSCoding.Protocol

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.

Thanks for the tip! I've used TypeBase::is function instead, looks like it's more suitable here, please, correct me if I'm wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot that existed. Thanks, Aleksey!

@aleksgapp aleksgapp force-pushed the bugfix/SR-1894 branch 2 times, most recently from ec3c600 to 89b3c0d Compare August 5, 2016 14:14
@jrose-apple
Copy link
Contributor

@swift-ci Please test OS X platform


// Consider protocol metatypes as not Objective-C representable.
if (metatype->is<MetatypeType>() && instanceType->is<ProtocolType>())
return ForeignRepresentableKind::None;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be instanceType->isAnyExistentialType(). The static metatypes of protocol compositions and existential metatypes are also not ObjC-representable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we already reject those, but it's important to have tests for them, and it would be a simpler check anyway.

Copy link
Contributor

@rjmccall rjmccall Aug 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, protocol compositions (id<P,Q>) and existential metatypes (Class<P>) should be ObjC-representable if their protocols are all ObjC, so if that's the condition, they'd get by.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, metatypes of existential types were already rejected, but metatypes of protocol composition crashed the same way as protocol metatypes before.

Added test cases for both of them.


typealias NSCodingExistential = NSCoding.Type

@objc func metatypeOfExistentialMetatypePram1(a: NSCodingExistential.Protocol) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't even realize this syntax was legal. @rjmccall?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the concrete metatype of the existential metatype, (exists T: NSCoding. T.Type).Type. NSCoding.Type.Type would be (exists T: NSCoding. T.Type.Type).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I guess I never thought about that having an actual representation in our system.

@jrose-apple
Copy link
Contributor

@swift-ci Please test OS X platform

@jrose-apple jrose-apple merged commit c8fbfb7 into swiftlang:master Aug 13, 2016
@jrose-apple
Copy link
Contributor

Merged. Thanks, Aleksey!

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