Skip to content

[SDK] Get the preferred @encode string for MapKit types dynamically #19691

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

jrose-apple
Copy link
Contributor

On older OSs, CLLocationCoordinate2D was an anonymous struct with a typedef, which meant its Objective-C type encoding string was "{?=dd}" instead of "{CLLocationCoordinate2D=dd}". This incompatibility led to us rejecting casts from NSValues created with CLLocationCoordinate2Ds in Objective-C on older OSs because they didn't match the type encoding provided. Instead, we can try to create an NSValue the way the frameworks do, and see what its type encoding is. This is what SceneKit already does.

There's an extra wrinkle here because the convenience methods for encoding CLLocationCoordinate2Ds are actually added in MapKit rather than CoreLocation. That means that if someone's app just uses CoreLocation, we can't rely on those convenience methods to get the correct type encoding. In this case, the best thing we can do is just give up and use the static encoding.

This whole thing is indicative of a bigger problem, namely that NSValue normally doesn't try to validate types at all. However, Swift bridge casting really does want to distinguish, say, a CLLocationCoordinate2D from a CGPoint, and checking the NSValue encode string was a way to do that. But this shows that it's still not safe to assume that's consistent against everything in a process even if they're all using @encode on the real struct (or the Swift equivalent), because the different parts of the process may have been compiled against different SDKs.

This change does not attempt to solve that problem.

Finishes rdar://problem/44866579. Follow-up to #19634.

@jrose-apple jrose-apple requested review from moiseev and mikeash October 3, 2018 19:10
@jrose-apple
Copy link
Contributor Author

@swift-ci Please test

Copy link
Contributor

@mikeash mikeash left a comment

Choose a reason for hiding this comment

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

This whole business makes me sad, but it seems like a reasonable solution.

@swift-ci
Copy link
Contributor

swift-ci commented Oct 3, 2018

Build failed
Swift Test OS X Platform
Git Sha - 0f4b6999eb58f342720c0ce614b70a23e2f554f4

On older OSs, CLLocationCoordinate2D was an anonymous struct with a
typedef, which meant its Objective-C type encoding string was "{?=dd}"
instead of "{CLLocationCoordinate2D=dd}". This incompatibility led to
us rejecting casts from NSValues created with CLLocationCoordinate2Ds
in Objective-C on older OSs because they didn't match the type
encoding provided. Instead, we can try to create an NSValue the way
the frameworks do, and see what /its/ type encoding is. This is what
SceneKit already does.

There's an extra wrinkle here because the convenience methods for
encoding CLLocationCoordinate2Ds are actually added in MapKit rather
than CoreLocation. That means that if someone's app just uses
CoreLocation, we can't rely on those convenience methods to get the
correct type encoding. In this case, the best thing we can do is just
give up and use the static encoding.

This whole thing is indicative of a bigger problem, namely that
NSValue normally doesn't try to validate types at all. However, Swift
bridge casting really does want to distinguish, say, a
CLLocationCoordinate2D from a CGPoint, and checking the NSValue encode
string was a way to do that. But this shows that it's still not safe
to assume that's consistent against everything in a process even if
they're all using @encode on the real struct (or the Swift
equivalent), because the different parts of the process may have been
compiled against different SDKs.

This change does not attempt to solve that problem.

Finishes rdar://problem/44866579
@jrose-apple jrose-apple force-pushed the a-stray-question-mark-leads-us-astray branch from 0f4b699 to e813138 Compare October 3, 2018 22:04
@jrose-apple
Copy link
Contributor Author

@swift-ci Please test

@swift-ci

This comment has been minimized.

@swift-ci

This comment has been minimized.

@jrose-apple jrose-apple merged commit 7c9376d into swiftlang:master Oct 4, 2018
@jrose-apple jrose-apple deleted the a-stray-question-mark-leads-us-astray branch October 4, 2018 00:31
modelorganism pushed a commit to modelorganism/swift that referenced this pull request Oct 11, 2018
…wiftlang#19691)

On older OSs, CLLocationCoordinate2D was an anonymous struct with a
typedef, which meant its Objective-C type encoding string was "{?=dd}"
instead of "{CLLocationCoordinate2D=dd}". This incompatibility led to
us rejecting casts from NSValues created with CLLocationCoordinate2Ds
in Objective-C on older OSs because they didn't match the type
encoding provided. Instead, we can try to create an NSValue the way
the frameworks do, and see what /its/ type encoding is. This is what
SceneKit already does.

There's an extra wrinkle here because the convenience methods for
encoding CLLocationCoordinate2Ds are actually added in MapKit rather
than CoreLocation. That means that if someone's app just uses
CoreLocation, we can't rely on those convenience methods to get the
correct type encoding. In this case, the best thing we can do is just
give up and use the static encoding.

This whole thing is indicative of a bigger problem, namely that
NSValue normally doesn't try to validate types at all. However, Swift
bridge casting really does want to distinguish, say, a
CLLocationCoordinate2D from a CGPoint, and checking the NSValue encode
string was a way to do that. But this shows that it's still not safe
to assume that's consistent against everything in a process even if
they're all using @encode on the real struct (or the Swift
equivalent), because the different parts of the process may have been
compiled against different SDKs.

This change does not attempt to solve that problem.

Finishes rdar://problem/44866579
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.

3 participants