Skip to content

Commit 65598d8

Browse files
DougGregortkremenek
authored andcommitted
SE-0022: Address Jordan's review comments about #selector.
1 parent 481ecb6 commit 65598d8

File tree

10 files changed

+40
-24
lines changed

10 files changed

+40
-24
lines changed

include/swift/AST/DeclContext.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,9 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
405405
LazyResolver *typeResolver,
406406
SmallVectorImpl<ValueDecl *> &decls) const;
407407

408-
/// Look up Objective-C methods with the given selector.
409-
void lookupObjCMethods(
408+
/// Look up all Objective-C methods with the given selector visible
409+
/// in the enclosing module.
410+
void lookupAllObjCMethods(
410411
ObjCSelector selector,
411412
SmallVectorImpl<AbstractFunctionDecl *> &results) const;
412413

include/swift/AST/Module.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,8 @@ class SourceFile final : public FileUnit {
919919

920920
/// A mapping from Objective-C selectors to the methods that have
921921
/// those selectors.
922-
llvm::DenseMap<ObjCSelector, std::vector<AbstractFunctionDecl *>> ObjCMethods;
922+
llvm::DenseMap<ObjCSelector, llvm::TinyPtrVector<AbstractFunctionDecl *>>
923+
ObjCMethods;
923924

924925
template <typename T>
925926
using OperatorMap = llvm::DenseMap<Identifier,llvm::PointerIntPair<T,1,bool>>;

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,7 @@ bool DeclContext::lookupQualified(Type type,
13751375
return !decls.empty();
13761376
}
13771377

1378-
void DeclContext::lookupObjCMethods(
1378+
void DeclContext::lookupAllObjCMethods(
13791379
ObjCSelector selector,
13801380
SmallVectorImpl<AbstractFunctionDecl *> &results) const {
13811381
// Collect all of the methods with this selector.

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3323,12 +3323,8 @@ namespace {
33233323

33243324
// Look through an implicit force-value.
33253325
if (auto force = dyn_cast<ForceValueExpr>(subExpr)) {
3326-
if (force->isImplicit()) {
3327-
subExpr = force->getSubExpr();
3328-
continue;
3329-
}
3330-
3331-
break;
3326+
subExpr = force->getSubExpr();
3327+
continue;
33323328
}
33333329

33343330
// Look through implicit open-existential operations.

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2482,7 +2482,7 @@ namespace {
24822482

24832483
Type visitObjCSelectorExpr(ObjCSelectorExpr *E) {
24842484
// #selector only makes sense when we have the Objective-C
2485-
// #runtime.
2485+
// runtime.
24862486
auto &tc = CS.getTypeChecker();
24872487
if (!tc.Context.LangOpts.EnableObjCInterop) {
24882488
tc.diagnose(E->getLoc(), diag::expr_selector_no_objc_runtime);

lib/Sema/MiscDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2025,7 +2025,7 @@ class ObjCSelectorWalker : public ASTWalker {
20252025

20262026
// Look for methods with this selector.
20272027
SmallVector<AbstractFunctionDecl *, 8> allMethods;
2028-
DC->lookupObjCMethods(*selector, allMethods);
2028+
DC->lookupAllObjCMethods(*selector, allMethods);
20292029

20302030
// If we didn't find any methods, complain.
20312031
if (allMethods.empty()) {

test/IRGen/objc_selector.sil

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// RUN: %target-swift-frontend -emit-ir %s | FileCheck %s
22

33
// REQUIRES: objc_interop
4-
// REQUIRES: CPU=x86_64
54

65
sil_stage canonical
76

87
import Builtin
98

109
// CHECK: @"\01L_selector_data(help:me:)" = private global [9 x i8] c"help:me:\00", section "__TEXT,__objc_methname,cstring_literals"
11-
// CHECK: @"\01L_selector(help:me:)" = private externally_initialized global i8* getelementptr inbounds ([9 x i8], [9 x i8]* @"\01L_selector_data(help:me:)", i64 0, i64 0), section "__DATA,__objc_selrefs,literal_pointers,no_dead_strip"
10+
// CHECK: @"\01L_selector(help:me:)" = private externally_initialized global i8* getelementptr inbounds ([9 x i8], [9 x i8]* @"\01L_selector_data(help:me:)", {{i(32|64)}} 0, {{i(32|64)}} 0), section "__DATA,__objc_selrefs,literal_pointers,no_dead_strip"
1211

1312
// CHECK-LABEL: define i8* @objc_selector_literal() #0 {
1413
sil @objc_selector_literal : $@convention(thin) () -> Builtin.RawPointer {

test/expr/unary/selector/fixits.filecheck

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/expr/unary/selector/fixits.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// REQUIRES: objc_interop
2+
13
// RUN: rm -rf %t
24
// RUN: mkdir -p %t
35
// RUN: rm -rf %t.overlays
@@ -21,9 +23,14 @@
2123
// RUN: %S/../../../../utils/apply-fixit-edits.py %t.remapping
2224
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t.overlays) -parse %t.sources/fixits.swift 2> %t.result
2325

24-
// RUN: FileCheck %S/fixits.filecheck < %t.result
26+
// RUN: FileCheck %s < %t.result
2527
// RUN: grep -c "warning:" %t.result | grep 4
2628

29+
// CHECK: warning: no method declared with Objective-C selector 'unknownMethodWithValue:label:'
30+
// CHECK: warning: string literal is not a valid Objective-C selector
31+
// CHECK: warning: no method declared with Objective-C selector 'unknownMethodWithValue:label:'
32+
// CHECK: warning: string literal is not a valid Objective-C selector
33+
2734
import Foundation
2835

2936
class Bar : Foo {

test/expr/unary/selector/selector.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ import ObjectiveC
77
@objc class B { }
88

99
class C1 {
10+
@objc init(a: A, b: B) { }
11+
1012
@objc func method1(a: A, b: B) { }
1113
@objc(someMethodWithA:B:) func method2(a: A, b: B) { }
1214

1315
@objc class func method3(a: A, b: B) { } // expected-note{{found this candidate}}
1416
@objc class func method3(a a: A, b: B) { } // expected-note{{found this candidate}}
1517

1618
@objc var a: A = A() // expected-note{{'a' declared here}}
19+
20+
@objc func getC1() -> AnyObject { return self }
1721
}
1822

1923
@objc protocol P1 {
@@ -25,7 +29,7 @@ extension C1 {
2529
final func method6() { } // expected-note{{add '@objc' to expose this method to Objective-C}}{{3-3=@objc }}
2630
}
2731

28-
func testSelector(c1: C1, p1: P1) {
32+
func testSelector(c1: C1, p1: P1, obj: AnyObject) {
2933
// Instance methods on an instance
3034
let sel1 = #selector(c1.method1)
3135
_ = #selector(c1.method1(_:b:))
@@ -50,6 +54,14 @@ func testSelector(c1: C1, p1: P1) {
5054
_ = #selector(p1.dynamicType.method5)
5155
_ = #selector(p1.dynamicType.method5(_:b:))
5256

57+
// Interesting expressions that refer to methods.
58+
_ = #selector(Swift.AnyObject.method1)
59+
_ = #selector(AnyObject.method1!)
60+
_ = #selector(obj.getC1?().method1)
61+
62+
// Initializers
63+
_ = #selector(C1.init(a:b:))
64+
5365
// Make sure the result has type "ObjectiveC.Selector"
5466
let sel2: Selector
5567
sel2 = sel1
@@ -62,7 +74,7 @@ func testAmbiguity() {
6274

6375
func testProperties(c1: C1) {
6476
_ = #selector(c1.a) // expected-error{{argument of '#selector' cannot refer to a property}}
65-
_ = #selector(C1.a) // expected-error{{instance member 'a' cannot be used on type 'C1'}}
77+
_ = #selector(C1.a) // FIXME poor diagnostic: expected-error{{instance member 'a' cannot be used on type 'C1'}}
6678
}
6779

6880
func testNonObjC(c1: C1) {
@@ -81,3 +93,10 @@ func testParseErrors3(c1: C1) {
8193
#selector( // expected-note{{to match this opening '('}}
8294
c1.method1(_:b:) // expected-error{{expected ')' to complete '#selector' expression}}
8395
}
96+
97+
func testParseErrors4() {
98+
// Subscripts
99+
_ = #selector(C1.subscript) // expected-error{{expected member name following '.'}}
100+
// expected-error@-1{{consecutive statements on a line must be separated by ';'}}
101+
// expected-error@-2{{expected '(' for subscript parameters}}
102+
}

0 commit comments

Comments
 (0)