Skip to content

[SDK] Backwards-deployment support for Core Data's generics. #3154

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
Jun 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib/public/SDK/CoreData/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_swift_library(swiftCoreData ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
CoreData.swift
CoreData.mm

SWIFT_MODULE_DEPENDS Foundation
FRAMEWORK_DEPENDS CoreData)
Expand Down
28 changes: 28 additions & 0 deletions stdlib/public/SDK/CoreData/CoreData.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#import <CoreData/CoreData.h>

// Make sure -conformsToProtocol: checks succeed even on older OSs.
// This only works because NSFetchRequestResult doesn't have any method
// requirements.

#define CONFORMS_TO_NSFETCHREQUESTRESULT(TYPE) \
@interface TYPE (_SwiftNSFetchRequestResult) <NSFetchRequestResult> \
@end \
@implementation TYPE (_SwiftNSFetchRequestResult) \
@end

CONFORMS_TO_NSFETCHREQUESTRESULT(NSNumber)
CONFORMS_TO_NSFETCHREQUESTRESULT(NSDictionary)
CONFORMS_TO_NSFETCHREQUESTRESULT(NSManagedObject)
CONFORMS_TO_NSFETCHREQUESTRESULT(NSManagedObjectID)
9 changes: 9 additions & 0 deletions test/1_stdlib/Inputs/CoreDataHelper/CoreDataHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@import CoreData;

NS_ASSUME_NONNULL_BEGIN

@interface NSFetchRequest<__covariant ResultType: id<NSFetchRequestResult>> (SwiftTesting)
+ (NSArray<ResultType> *)testGettingSomeDictionaries;
@end

NS_ASSUME_NONNULL_END
7 changes: 7 additions & 0 deletions test/1_stdlib/Inputs/CoreDataHelper/CoreDataHelper.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import "CoreDataHelper.h"

@implementation NSFetchRequest (SwiftTesting)
+ (NSArray<id<NSFetchRequestResult>> *)testGettingSomeDictionaries {
return @[ @{}, @{} ];
}
@end
41 changes: 41 additions & 0 deletions validation-test/stdlib/CoreData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// RUN: rm -rf %t && mkdir -p %t

// RUN: %target-clang %S/Inputs/CoreDataHelper/CoreDataHelper.m -c -o %t/CoreDataHelper.o -g -fmodules
// RUN: %target-build-swift %s -import-objc-header %S/Inputs/CoreDataHelper/CoreDataHelper.h -Xlinker %t/CoreDataHelper.o -o %t/main
// RUN: %target-run %t/main

// REQUIRES: executable_test
// REQUIRES: objc_interop

import CoreData
import StdlibUnittest

var CoreDataTests = TestSuite("CoreData")

CoreDataTests.test("conformsToProtocol") {
expectTrue(NSDictionary.conforms(to: NSFetchRequestResult.self))
expectTrue(NSManagedObject.conforms(to: NSFetchRequestResult.self))
}

CoreDataTests.test("downcasting") {
var dictionaries = NSFetchRequest<NSFetchRequestResult>.testGettingSomeDictionaries()
expectType([NSFetchRequestResult].self, &dictionaries)

let casted = dictionaries as? [[NSObject: AnyObject]]
expectNotEmpty(casted)
expectEqual([[:], [:]], casted!)
expectEqual([[:], [:]], dictionaries as! [[NSObject: AnyObject]])
}

CoreDataTests.test("bridging") {
var dictionaries = NSFetchRequest<NSDictionary>.testGettingSomeDictionaries()
expectType([NSDictionary].self, &dictionaries)
expectEqual([[:], [:]], dictionaries)

let casted = dictionaries as? [[NSObject: AnyObject]]
expectNotEmpty(casted)
expectEqual([[:], [:]], casted!)
expectEqual([[:], [:]], dictionaries as! [[NSObject: AnyObject]])
}

runAllTests()