Skip to content

Commit 7c9376d

Browse files
authored
[SDK] Get the preferred @encode string for MapKit types dynamically (#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
1 parent ebd4d9f commit 7c9376d

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

stdlib/public/SDK/CoreLocation/NSValue.swift.gyb

+32-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,37 @@
22
import Foundation
33

44
%{
5-
from gyb_foundation_support import ObjectiveCBridgeableImplementationForNSValue
5+
from gyb_foundation_support import \
6+
ObjectiveCBridgeableImplementationForNSValueWithCategoryMethods
67
}%
78

8-
${ ObjectiveCBridgeableImplementationForNSValue("CLLocationCoordinate2D") }
9+
// Get the ObjC type used by -[NSValue valueWithMKCoordinate:]
10+
// to instantiate the resulting NSValue objects, in case these get changed
11+
// in the future. This is extra tricky because MapKit *might not be loaded*,
12+
// in which case we need to fall back to the static @encode string.
13+
private let CLLocationCoordinate2DInNSValueObjCType: UnsafePointer<CChar> = {
14+
let factorySel = Selector(("valueWithMKCoordinate:"))
15+
guard let opaqueFactoryMethod = NSValue.method(for: factorySel) else {
16+
return _getObjCTypeEncoding(CLLocationCoordinate2D.self)
17+
}
18+
typealias FactoryMethodType =
19+
@convention(c) (AnyClass, Selector, CLLocationCoordinate2D) -> NSValue
20+
let factoryMethod =
21+
unsafeBitCast(opaqueFactoryMethod, to: FactoryMethodType.self)
22+
return factoryMethod(NSValue.self, factorySel, .init()).objCType
23+
}()
24+
25+
${ ObjectiveCBridgeableImplementationForNSValueWithCategoryMethods(
26+
Type="CLLocationCoordinate2D",
27+
initializer="""{
28+
var addressableValue = $0
29+
return NSValue(bytes: &addressableValue,
30+
objCType: CLLocationCoordinate2DInNSValueObjCType)
31+
}""",
32+
getter="""{
33+
var result = CLLocationCoordinate2D()
34+
$0.getValue(&result)
35+
return result
36+
}""",
37+
objCType="{ _ in CLLocationCoordinate2DInNSValueObjCType }",
38+
) }

stdlib/public/SDK/MapKit/NSValue.swift.gyb

+30-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,35 @@
22
import Foundation
33

44
%{
5-
from gyb_foundation_support import ObjectiveCBridgeableImplementationForNSValue
5+
from gyb_foundation_support import \
6+
ObjectiveCBridgeableImplementationForNSValueWithCategoryMethods
67
}%
78

8-
${ ObjectiveCBridgeableImplementationForNSValue("MKCoordinateSpan") }
9+
// Get the ObjC type used by -[NSValue valueWithMKCoordinateSpan:]
10+
// to instantiate the resulting NSValue objects, in case these get changed
11+
// in the future.
12+
@available(tvOS 9.2, *)
13+
private let MKCoordinateSpanInNSValueObjCType =
14+
NSValue(mkCoordinateSpan: .init()).objCType
15+
16+
${ ObjectiveCBridgeableImplementationForNSValueWithCategoryMethods(
17+
Type="MKCoordinateSpan",
18+
initializer="""{
19+
guard #available(tvOS 9.2, *) else {
20+
fatalError("MKCoordinateSpan is not supported on tvOS before tvOS 9.2")
21+
}
22+
return NSValue(mkCoordinateSpan: $0)
23+
}""",
24+
getter="""{
25+
guard #available(tvOS 9.2, *) else {
26+
fatalError("MKCoordinateSpan is not supported on tvOS before tvOS 9.2")
27+
}
28+
return $0.mkCoordinateSpanValue
29+
}""",
30+
objCType="""{ _ in
31+
guard #available(tvOS 9.2, *) else {
32+
fatalError("MKCoordinateSpan is not supported on tvOS before tvOS 9.2")
33+
}
34+
return MKCoordinateSpanInNSValueObjCType
35+
}""",
36+
) }

test/stdlib/MapKit.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ func spansEqual(_ x: MKCoordinateSpan, _ y: MKCoordinateSpan)
2020
}
2121

2222
if #available(tvOS 9.2, *) {
23-
mapKit.test("NSValue bridging")
24-
.xfail(.iOSMinor(9, 3, reason: "<rdar://problem/41440036>"))
25-
.xfail(.osxMinorRange(10, 9...10, reason: "<rdar://problem/44866579>"))
26-
.code {
23+
mapKit.test("CLLocationCoordinate2D bridging") {
2724
expectBridgeToNSValue(CLLocationCoordinate2D(latitude: 17, longitude: 38),
2825
nsValueInitializer: { NSValue(mkCoordinate: $0) },
2926
nsValueGetter: { $0.mkCoordinateValue },
3027
equal: coordinatesEqual)
28+
}
29+
30+
mapKit.test("MKCoordinateSpan bridging") {
3131
expectBridgeToNSValue(MKCoordinateSpan(latitudeDelta: 6,
3232
longitudeDelta: 79),
3333
nsValueInitializer: { NSValue(mkCoordinateSpan: $0) },

0 commit comments

Comments
 (0)