Skip to content

Commit 47a247c

Browse files
authored
[SDK] Backwards-deployment support for Core Data's generics. (#3154)
rdar://problem/26825103
1 parent 45ea78e commit 47a247c

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

stdlib/public/SDK/CoreData/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_swift_library(swiftCoreData ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
22
CoreData.swift
3+
CoreData.mm
34

45
SWIFT_MODULE_DEPENDS Foundation
56
FRAMEWORK_DEPENDS CoreData)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#import <CoreData/CoreData.h>
14+
15+
// Make sure -conformsToProtocol: checks succeed even on older OSs.
16+
// This only works because NSFetchRequestResult doesn't have any method
17+
// requirements.
18+
19+
#define CONFORMS_TO_NSFETCHREQUESTRESULT(TYPE) \
20+
@interface TYPE (_SwiftNSFetchRequestResult) <NSFetchRequestResult> \
21+
@end \
22+
@implementation TYPE (_SwiftNSFetchRequestResult) \
23+
@end
24+
25+
CONFORMS_TO_NSFETCHREQUESTRESULT(NSNumber)
26+
CONFORMS_TO_NSFETCHREQUESTRESULT(NSDictionary)
27+
CONFORMS_TO_NSFETCHREQUESTRESULT(NSManagedObject)
28+
CONFORMS_TO_NSFETCHREQUESTRESULT(NSManagedObjectID)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@import CoreData;
2+
3+
NS_ASSUME_NONNULL_BEGIN
4+
5+
@interface NSFetchRequest<__covariant ResultType: id<NSFetchRequestResult>> (SwiftTesting)
6+
+ (NSArray<ResultType> *)testGettingSomeDictionaries;
7+
@end
8+
9+
NS_ASSUME_NONNULL_END
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#import "CoreDataHelper.h"
2+
3+
@implementation NSFetchRequest (SwiftTesting)
4+
+ (NSArray<id<NSFetchRequestResult>> *)testGettingSomeDictionaries {
5+
return @[ @{}, @{} ];
6+
}
7+
@end

validation-test/stdlib/CoreData.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// RUN: rm -rf %t && mkdir -p %t
2+
3+
// RUN: %target-clang %S/Inputs/CoreDataHelper/CoreDataHelper.m -c -o %t/CoreDataHelper.o -g -fmodules
4+
// RUN: %target-build-swift %s -import-objc-header %S/Inputs/CoreDataHelper/CoreDataHelper.h -Xlinker %t/CoreDataHelper.o -o %t/main
5+
// RUN: %target-run %t/main
6+
7+
// REQUIRES: executable_test
8+
// REQUIRES: objc_interop
9+
10+
import CoreData
11+
import StdlibUnittest
12+
13+
var CoreDataTests = TestSuite("CoreData")
14+
15+
CoreDataTests.test("conformsToProtocol") {
16+
expectTrue(NSDictionary.conforms(to: NSFetchRequestResult.self))
17+
expectTrue(NSManagedObject.conforms(to: NSFetchRequestResult.self))
18+
}
19+
20+
CoreDataTests.test("downcasting") {
21+
var dictionaries = NSFetchRequest<NSFetchRequestResult>.testGettingSomeDictionaries()
22+
expectType([NSFetchRequestResult].self, &dictionaries)
23+
24+
let casted = dictionaries as? [[NSObject: AnyObject]]
25+
expectNotEmpty(casted)
26+
expectEqual([[:], [:]], casted!)
27+
expectEqual([[:], [:]], dictionaries as! [[NSObject: AnyObject]])
28+
}
29+
30+
CoreDataTests.test("bridging") {
31+
var dictionaries = NSFetchRequest<NSDictionary>.testGettingSomeDictionaries()
32+
expectType([NSDictionary].self, &dictionaries)
33+
expectEqual([[:], [:]], dictionaries)
34+
35+
let casted = dictionaries as? [[NSObject: AnyObject]]
36+
expectNotEmpty(casted)
37+
expectEqual([[:], [:]], casted!)
38+
expectEqual([[:], [:]], dictionaries as! [[NSObject: AnyObject]])
39+
}
40+
41+
runAllTests()

0 commit comments

Comments
 (0)