mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
108 lines
6.9 KiB
Swift
108 lines
6.9 KiB
Swift
// REQUIRES: objc_interop
|
|
|
|
// RUN: rm -rf %t
|
|
// RUN: mkdir -p %t
|
|
// RUN: rm -rf %t.overlays
|
|
// RUN: mkdir -p %t.overlays
|
|
|
|
// FIXME: BEGIN -enable-source-import hackaround
|
|
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t.overlays %clang-importer-sdk-path/swift-modules/ObjectiveC.swift
|
|
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t.overlays %clang-importer-sdk-path/swift-modules/CoreGraphics.swift
|
|
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t.overlays %clang-importer-sdk-path/swift-modules/Foundation.swift
|
|
// FIXME: END -enable-source-import hackaround
|
|
|
|
// Make sure we get the right diagnostics.
|
|
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t.overlays) -parse %s -verify
|
|
|
|
// Copy the source, apply the Fix-Its, and compile it again, making
|
|
// sure that we've cleaned up all of the deprecation warnings.
|
|
// RUN: mkdir -p %t.sources
|
|
// RUN: mkdir -p %t.remapping
|
|
// RUN: cp %s %t.sources/fixits.swift
|
|
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t.overlays) -parse %t.sources/fixits.swift -fixit-all -emit-fixits-path %t.remapping/fixits.remap
|
|
// RUN: %S/../../../../utils/apply-fixit-edits.py %t.remapping
|
|
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t.overlays) -parse %t.sources/fixits.swift 2> %t.result
|
|
|
|
// RUN: FileCheck %s < %t.result
|
|
// RUN: grep -c "warning:" %t.result | grep 4
|
|
|
|
// CHECK: warning: no method declared with Objective-C selector 'unknownMethodWithValue:label:'
|
|
// CHECK: warning: string literal is not a valid Objective-C selector
|
|
// CHECK: warning: no method declared with Objective-C selector 'unknownMethodWithValue:label:'
|
|
// CHECK: warning: string literal is not a valid Objective-C selector
|
|
|
|
import Foundation
|
|
|
|
class Bar : Foo {
|
|
@objc(method2WithValue:) override func method2(value: Int) { }
|
|
|
|
@objc(overloadedWithInt:) func overloaded(x: Int) { }
|
|
@objc(overloadedWithString:) func overloaded(x: String) { }
|
|
|
|
@objc(staticOverloadedWithInt:) static func staticOverloaded(x: Int) { }
|
|
@objc(staticOverloadedWithString:) static func staticOverloaded(x: String) { }
|
|
|
|
@objc(staticOrNonStatic:) func staticOrNonStatic(x: Int) { }
|
|
@objc(staticOrNonStatic:) static func staticOrNonStatic(x: Int) { }
|
|
|
|
@objc(theInstanceOne:) func staticOrNonStatic2(x: Int) { }
|
|
@objc(theStaticOne:) static func staticOrNonStatic2(x: Int) { }
|
|
}
|
|
|
|
class Foo {
|
|
@objc(methodWithValue:label:) func method(value: Int, label: String) { }
|
|
|
|
@objc(method2WithValue:) func method2(value: Int) { }
|
|
|
|
@objc func method3() { }
|
|
|
|
@objc var property: String = ""
|
|
}
|
|
|
|
|
|
func testDeprecatedStringLiteralSelector() {
|
|
let sel1: Selector = "methodWithValue:label:" // expected-warning{{use of string literal for Objective-C selectors is deprecated; use '#selector' instead}}{{24-48=#selector(Foo.method(_:label:))}}
|
|
_ = sel1
|
|
|
|
_ = "methodWithValue:label:" as Selector // expected-warning{{use of string literal for Objective-C selectors is deprecated; use '#selector' instead}}{{7-43=#selector(Foo.method(_:label:))}}
|
|
_ = "property" as Selector // expected-warning{{use of string literal for Objective-C selectors is deprecated; use '#selector' or explicitly construct a 'Selector'}}{{7-7=Selector(}}{{17-29=)}}
|
|
_ = "setProperty:" as Selector // expected-warning{{use of string literal for Objective-C selectors is deprecated; use '#selector' or explicitly construct a 'Selector'}}{{7-7=Selector(}}{{21-33=)}}
|
|
_ = "unknownMethodWithValue:label:" as Selector // expected-warning{{no method declared with Objective-C selector 'unknownMethodWithValue:label:'}}{{7-7=Selector(}}{{38-50=)}}
|
|
_ = "badSelector:label" as Selector // expected-warning{{string literal is not a valid Objective-C selector}}
|
|
_ = "method2WithValue:" as Selector // expected-warning{{use of string literal for Objective-C selectors is deprecated; use '#selector' instead}}{{7-38=#selector(Foo.method2(_:))}}
|
|
_ = "method3" as Selector // expected-warning{{use of string literal for Objective-C selectors is deprecated; use '#selector' instead}}{{7-28=#selector(Foo.method3)}}
|
|
|
|
// Overloaded cases
|
|
_ = "overloadedWithInt:" as Selector // expected-warning{{use of string literal for Objective-C selectors is deprecated; use '#selector' instead}}{{7-39=#selector(Bar.overloaded(_:) as (Bar) -> (Int) -> ())}}
|
|
_ = "overloadedWithString:" as Selector // expected-warning{{use of string literal for Objective-C selectors is deprecated; use '#selector' instead}}{{7-42=#selector(Bar.overloaded(_:) as (Bar) -> (String) -> ())}}
|
|
|
|
_ = "staticOverloadedWithInt:" as Selector // expected-warning{{use of string literal for Objective-C selectors is deprecated; use '#selector' instead}}{{7-45=#selector(Bar.staticOverloaded(_:) as (Int) -> ())}}
|
|
_ = "staticOverloadedWithString:" as Selector // expected-warning{{use of string literal for Objective-C selectors is deprecated; use '#selector' instead}}{{7-48=#selector(Bar.staticOverloaded(_:) as (String) -> ())}}
|
|
|
|
// We don't need coercion here because we get the right selector
|
|
// from the static method.
|
|
_ = "staticOrNonStatic:" as Selector // expected-warning{{use of string literal for Objective-C selectors is deprecated; use '#selector' instead}}{{7-39=#selector(Bar.staticOrNonStatic(_:))}}
|
|
|
|
// We need coercion here because we asked for a selector from an
|
|
// instance method with the same name as (but a different selector
|
|
// from) a static method.
|
|
_ = "theInstanceOne:" as Selector // expected-warning{{use of string literal for Objective-C selectors is deprecated; use '#selector' instead}}{{7-36=#selector(Bar.staticOrNonStatic2(_:) as (Bar) -> (Int) -> ())}}
|
|
|
|
// Note: from Foundation
|
|
_ = "initWithArray:" as Selector // expected-warning{{use of string literal for Objective-C selectors is deprecated; use '#selector' instead}}{{7-35=#selector(NSSet.init(array:))}}
|
|
|
|
// Note: from Foundation overlay
|
|
_ = "methodIntroducedInOverlay" as Selector // expected-warning{{use of string literal for Objective-C selectors is deprecated; use '#selector' instead}}{{7-46=#selector(NSArray.introducedInOverlay)}}
|
|
}
|
|
|
|
func testSelectorConstruction() {
|
|
_ = Selector("methodWithValue:label:") // expected-warning{{use '#selector' instead of explicitly constructing a 'Selector'}}{{7-41=#selector(Foo.method(_:label:))}}
|
|
_ = Selector("unknownMethodWithValue:label:") // expected-warning{{no method declared with Objective-C selector 'unknownMethodWithValue:label:'}}
|
|
_ = Selector("badSelector:label") // expected-warning{{string literal is not a valid Objective-C selector}}
|
|
_ = Selector("method2WithValue:") // expected-warning{{use '#selector' instead of explicitly constructing a 'Selector'}}{{7-36=#selector(Foo.method2(_:))}}
|
|
_ = Selector("method3") // expected-warning{{use '#selector' instead of explicitly constructing a 'Selector'}}{{7-26=#selector(Foo.method3)}}
|
|
|
|
// Note: from Foundation
|
|
_ = Selector("initWithArray:") // expected-warning{{use '#selector' instead of explicitly constructing a 'Selector'}}{{7-33=#selector(NSSet.init(array:))}}
|
|
}
|