Skip to content

Make NS_TYPED_ENUMS ObjectiveCBridgeable when they wrap an object #15270

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 1 commit into from
Mar 15, 2018

Conversation

jrose-apple
Copy link
Contributor

@jrose-apple jrose-apple commented Mar 15, 2018

This allows them to be used in generic arguments for NSArray et al. We already do this for the ones that wrap bridged values (like NSString/String), but failed to do it for objects that weren't bridged to Swift values (class instances and protocol compositions), or for Error-which-is-special.

In addition to this being a sensible thing to do, not doing this led to IRGen getting very confused (i.e. crashing) when we imported a Objective-C protocol that actually used an NS_TYPED_ENUM in this way.

(We actually shouldn't be using Swift's IRGen logic to emit protocol descriptors for imported protocols at all, because it's possible we weren't able to import all the requirements. But that's a separate issue.)

SR-6844 / rdar://problem/36911791, possibly others

@jrose-apple
Copy link
Contributor Author

@swift-ci Please test

@jrose-apple
Copy link
Contributor Author

@swift-ci Please test source compatibility

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 0db14edf1f62c3e3f7fc597b4e76ac087532b21c

@jrose-apple
Copy link
Contributor Author

Oops, requires_objc.

This allows them to be used in generic arguments for NSArray et al.
We already do this for the ones that wrap bridged values (like
NSString/String), but failed to do it for objects that /weren't/
bridged to Swift values (class instances and protocol compositions),
or for Error-which-is-special.

In addition to this being a sensible thing to do, /not/ doing this led
to IRGen getting very confused (i.e. crashing) when we imported a
Objective-C protocol that actually used an NS_TYPED_ENUM in this way.

(We actually shouldn't be using Swift's IRGen logic to emit protocol
descriptors for imported protocols at all, because it's possible we
weren't able to import all the requirements. But that's a separate
issue.)

https://bugs.swift.org/browse/SR-6844
@jrose-apple
Copy link
Contributor Author

Passed everything except that Linux failed on one of the new tests.

@swift-ci Please smoke test

Copy link
Contributor

@jckarter jckarter left a comment

Choose a reason for hiding this comment

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

LGTM.

@@ -24,6 +24,9 @@ extension _SwiftNewtypeWrapper where Self.RawValue : Hashable {

#if _runtime(_ObjC)
extension _SwiftNewtypeWrapper where Self.RawValue : _ObjectiveCBridgeable {
// Note: This is the only default typealias for _ObjectiveCType, because
// constrained extensions aren't allowed to define types in different ways.
// Fortunately the others don't need it.
public typealias _ObjectiveCType = Self.RawValue._ObjectiveCType
Copy link
Member

Choose a reason for hiding this comment

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

I don't trust the compiler to get this right consistently, because a typealias with the same name as an associated type, but in a constrained extension, isn't something modeled well in the conformance checker. You're already adding a synthesized typealias in the importer, so why do you need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the one case the importer is skipping; I didn't want to have to resolve the _ObjectiveCType of the RawValue, especially when compiling an overlay where we may not have actually determined it yet. How bad do you think this is? (Note that it's not necessarily the original type; a swift_wrapper of an int is bridged to NSNumber.)

Copy link
Member

Choose a reason for hiding this comment

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

I was thinking the that the swift_bridge attribute had all of the information, but of course it doesn't for NSNumber or NSValue. Slightly concerned that we'll be unable to form this type witness if the Clang importer imports one of these types after the type checker is gone, but I supposed #15254 should make that no longer an issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think if that happens we're hosed anyway for all the default value requirements, so I'm not going to worry about it. :-)

Copy link
Member

Choose a reason for hiding this comment

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

Associated type witnesses get used in Type::subst calls a lot, and we've had a number of compiler crashes due to missing associated type witnesses in the importer. Value witnesses are less often used---only really by the specializer or (in this case) the SILGen bridging code---and hasn't been as much of an issue. Anyway, I'm okay with this solution.

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