Files
swift-mirror/test/Sema/availability_versions_objc_api.swift
Jordan Rose b5aca663bc [ClangImporter] Remove importer-based NS stripping. (#3880)
* [ClangImporter] Remove importer-based NS stripping.

As Tony puts it, in the end we wound up with more Foundation
declarations imported as members or keeping "NS" than those that
dropped it, and any further decisions will be made on a case-by-case
basis. Move all of the existing cases of prefix-stripping into
Foundation's API notes and drop the logic from the compiler.

Tested by dumping the generated interface for Foundation and its
submodules for both macOS and the iOS simulator, and comparing the
results. A few cases did slip through here because of the interaction
between "SwiftName" and "Availability: nonswift".

The next commit will re-add "NS" to some stragglers that we missed.

rdar://problem/26880017

* APINotes: Add "NS" back to a few types.

NSKeyedUnarchiverDelegate
NSKeyedArchiverDelegate
NSTextCheckingTypes
NSBinarySearchingOptions
NSEnumerationOptions
NSSortOptions

More rdar://problem/26880017

* Remove now-redundant SwiftNames from API notes.

No change observed in the generated interface of Foundation and its
submodules.

Finishes rdar://problem/26880017.
2016-08-01 20:54:26 -07:00

231 lines
10 KiB
Swift

// RUN: %target-parse-verify-swift %clang-importer-sdk -I %S/Inputs/custom-modules
// RUN: not %target-swift-frontend -parse %clang-importer-sdk -I %S/Inputs/custom-modules %s 2>&1 | FileCheck %s
// REQUIRES: OS=macosx
// REQUIRES: objc_interop
import Foundation
// Tests for uses of version-based potential unavailability imported from ObjC APIs.
func callUnavailableObjC() {
_ = NSAvailableOn10_51() // expected-error {{'NSAvailableOn10_51' is only available on OS X 10.51 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
// expected-note@-2 {{add 'if #available' version check}}
if #available(OSX 10.51, *) {
let o = NSAvailableOn10_51()!
// Properties
_ = o.propertyOn10_52 // expected-error {{'propertyOn10_52' is only available on OS X 10.52 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
// expected-note@-2 {{add 'if #available' version check}}
o.propertyOn10_52 = 22 // expected-error {{'propertyOn10_52' is only available on OS X 10.52 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
// expected-note@-2 {{add 'if #available' version check}}
// Methods
o.methodAvailableOn10_52() // expected-error {{'methodAvailableOn10_52()' is only available on OS X 10.52 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
// expected-note@-2 {{add 'if #available' version check}}
// Initializers
_ = NSAvailableOn10_51(stringOn10_52:"Hi") // expected-error {{'init(stringOn10_52:)' is only available on OS X 10.52 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
// expected-note@-2 {{add 'if #available' version check}}
}
}
// Declarations with Objective-C-originated potentially unavailable APIs
func functionWithObjCParam(o: NSAvailableOn10_51) { // expected-error {{'NSAvailableOn10_51' is only available on OS X 10.51 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
}
class ClassExtendingUnvailableClass : NSAvailableOn10_51 { // expected-error {{'NSAvailableOn10_51' is only available on OS X 10.51 or newer}}
// expected-note@-1 {{add @available attribute to enclosing class}}
}
// We allow classes to conform to potentially unavailable protocols
class ClassAdoptingUnavailableProtocol : NSProtocolAvailableOn10_51 {
}
class SomeSoonToBeConformingClass { }
extension SomeSoonToBeConformingClass : NSProtocolAvailableOn10_51 {
}
// Enums from Objective-C
let _: NSPotentiallyUnavailableOptions = .first // expected-error {{'NSPotentiallyUnavailableOptions' is only available on OS X 10.51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: NSOptionsWithUnavailableElement = .third // expected-error {{'third' is only available on OS X 10.51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: NSUnavailableEnum = .first // expected-error {{'NSUnavailableEnum' is only available on OS X 10.51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: NSEnumWithUnavailableElement = .third // expected-error {{'third' is only available on OS X 10.51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
// Differing availability on getters and setters imported from ObjC.
func gettersAndSettersFromObjC(o: NSAvailableOn10_9) {
let _: Int = o.propertyOn10_51WithSetterOn10_52After // expected-error {{'propertyOn10_51WithSetterOn10_52After' is only available on OS X 10.51 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
// expected-note@-2 {{add 'if #available' version check}}
if #available(OSX 10.51, *) {
// Properties with unavailable accessors declared before property in Objective-C header
o.propertyOn10_51WithSetterOn10_52Before = 5 // expected-error {{setter for 'propertyOn10_51WithSetterOn10_52Before' is only available on OS X 10.52 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
// expected-note@-2 {{add 'if #available' version check}}
let _: Int = o.propertyOn10_51WithGetterOn10_52Before // expected-error {{getter for 'propertyOn10_51WithGetterOn10_52Before' is only available on OS X 10.52 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
// expected-note@-2 {{add 'if #available' version check}}
// Properties with unavailable accessors declared after property in Objective-C header
o.propertyOn10_51WithSetterOn10_52After = 5 // expected-error {{setter for 'propertyOn10_51WithSetterOn10_52After' is only available on OS X 10.52 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
// expected-note@-2 {{add 'if #available' version check}}
let _: Int = o.propertyOn10_51WithGetterOn10_52After // expected-error {{getter for 'propertyOn10_51WithGetterOn10_52After' is only available on OS X 10.52 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
// expected-note@-2 {{add 'if #available' version check}}
// Property with unavailable setter redeclared in Objective-C category
o.readOnlyRedeclaredWithSetterInCategory = 5 // expected-error {{setter for 'readOnlyRedeclaredWithSetterInCategory' is only available on OS X 10.52 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
// expected-note@-2 {{add 'if #available' version check}}
}
}
// Globals from Objective-C
func useGlobalsFromObjectiveC() {
_ = globalStringAvailableOn10_51 // expected-error {{'globalStringAvailableOn10_51' is only available on OS X 10.51 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
// expected-note@-2 {{add 'if #available' version check}}
_ = globalStringAvailableOn10_52 // expected-error {{'globalStringAvailableOn10_52' is only available on OS X 10.52 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
// expected-note@-2 {{add 'if #available' version check}}
_ = globalClassInstanceAvailableOn10_51 // expected-error {{'globalClassInstanceAvailableOn10_51' is only available on OS X 10.51 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
// expected-note@-2 {{add 'if #available' version check}}
if #available(OSX 10.51, *) {
_ = globalStringAvailableOn10_51
let _: NSAvailableOn10_51 = globalClassInstanceAvailableOn10_51
}
}
// Optional Protocol Requirements from Objective-C
// Make sure we're not emitting errors in the Foundation module, where the witness is.
// CHECK-NOT: Foundation.ClassWithMethodFromNSProtocolWithOptionalRequirement:
class SubclassOfNSClassWithMethodFromNSProtocolWithOptionalRequirement : NSClassWithMethodFromNSProtocolWithOptionalRequirement {
}
class SubclassWithItsOwnAvailableWitnessOfNSClassWithMethodFromNSProtocolWithOptionalRequirement : NSClassWithMethodFromNSProtocolWithOptionalRequirement {
override func optionalRequirement() { }
}
// Inference of protocol requirement availability when checking conformance to
// unannotated Objective-C protocols
class UserClass : UnannotatedFrameworkProtocol {
@available(OSX 10.51, *)
@objc(doSomethingWithClass:)
func doSomething(with k: AnnotatedFrameworkClass?) { }
@available(OSX 10.51, *)
@objc
func doSomething(withNonNullableClass k: AnnotatedFrameworkClass) { }
@available(OSX 10.51, *)
@objc(doSomethingWithIUOClass:)
func doSomething(withIUOClass k: AnnotatedFrameworkClass!) { }
@objc
@available(OSX 10.51, *)
func returnSomething() -> AnnotatedFrameworkClass? {
return nil
}
@objc
func noUnavailableTypesInSignature() { }
@objc(doSomethingWithClass:andLaterClass:) @available(OSX 10.52, *)
func doSomething(with k: AnnotatedFrameworkClass, andLaterClass lk: AnnotatedLaterFrameworkClass) { }
@objc
@available(OSX 10.53, *)
func someMethodWithAvailability() { }
@available(OSX 10.51, *)
@objc var someProperty: AnnotatedFrameworkClass {
get { return AnnotatedFrameworkClass() }
set(newValue) { }
}
}
func callViaUnannotatedFrameworkProtocol(p: UnannotatedFrameworkProtocol) {
let _ = p.returnSomething() // expected-error {{'returnSomething()' is only available on OS X 10.51 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
// expected-note@-2 {{add 'if #available' version check}}
}
func callViaAnnotatedFrameworkProtocol(p: AnnotatedFrameworkProtocol) {
// We won't synthesize availability for AnnotatedFrameworkProtocol because
// the protocol has an availability annotation on it.
let _ = p.returnSomething()
}
class SubclassOfFrameworkClassConformingToUnannotatedFrameworkProtocol : FrameworkClassConformingToUnannotatedFrameworkProtocol {
@available(OSX 10.51, *)
override func doSomething(withNonNullableClass k: AnnotatedFrameworkClass) {
}
@available(OSX 10.51, *)
override var someProperty: AnnotatedFrameworkClass {
get { return AnnotatedFrameworkClass() }
set(newValue) { }
}
@available(OSX 10.52, *)
override func doSomething(withIUOClass k: AnnotatedFrameworkClass!) { } // expected-error {{'doSomething' must be as available as declaration it overrides}}
}
@available(OSX 10.52, *)
class SubclassOfLaterFrameworkClassConformingToUnannotatedFrameworkProtocol : LaterFrameworkClassConformingToUnannotatedFrameworkProtocol {
@available(OSX 10.52, *)
override func doSomething(withNonNullableClass k: AnnotatedFrameworkClass) {
}
@available(OSX 10.53, *)
override func someMethodWithAvailability() { }
}
class SubclassOfFrameworkClassConformingToLaterAnnotatedFrameworkProtocol : FrameworkClassConformingToLaterAnnotatedFrameworkProtocol {
@available(OSX 10.52, *)
override func returnSomething() -> AnnotatedFrameworkClass? {
}
@available(OSX 10.53, *)
override func someMethodWithAvailability() { }
@available(OSX 10.52, *)
override var someProperty: AnnotatedFrameworkClass {
get { return AnnotatedFrameworkClass() }
set(newValue) { }
}
}