Skip to content

Commit 4cce21f

Browse files
committed
Implement SE-0253 review feedback.
Use `callFunction` instead of `call` as the call-syntax delegate method name. Add trailing closure tests.
1 parent 932f108 commit 4cce21f

File tree

9 files changed

+70
-57
lines changed

9 files changed

+70
-57
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5958,8 +5958,8 @@ class FuncDecl : public AbstractFunctionDecl {
59585958
bool isConsuming() const {
59595959
return getSelfAccessKind() == SelfAccessKind::__Consuming;
59605960
}
5961-
bool isCallable() const {
5962-
return getName().str() == "call" && isInstanceMember();
5961+
bool isCallFunction() const {
5962+
return getName().str() == "callFunction" && isInstanceMember();
59635963
}
59645964

59655965
SelfAccessKind getSelfAccessKind() const {

include/swift/AST/KnownIdentifiers.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ IDENTIFIER(Any)
3131
IDENTIFIER(ArrayLiteralElement)
3232
IDENTIFIER(atIndexedSubscript)
3333
IDENTIFIER_(bridgeToObjectiveC)
34-
IDENTIFIER(call)
34+
IDENTIFIER(callFunction)
3535
IDENTIFIER_WITH_NAME(code_, "_code")
3636
IDENTIFIER(CodingKeys)
3737
IDENTIFIER(combine)

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7228,7 +7228,7 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
72287228
auto selected = solution.getOverloadChoice(cs.getConstraintLocator(loc));
72297229
auto choice = selected.choice;
72307230
auto *callMethod = dyn_cast<FuncDecl>(selected.choice.getDecl());
7231-
if (callMethod && callMethod->isCallable()) {
7231+
if (callMethod && callMethod->isCallFunction()) {
72327232
auto methodType =
72337233
simplifyType(selected.openedType)->castTo<AnyFunctionType>();
72347234
auto selfParam = callMethod->getImplicitSelfDecl();

lib/Sema/CSDiag.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4804,7 +4804,7 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
48044804
auto hasCallMethods = nominal &&
48054805
llvm::any_of(nominal->getMembers(), [](Decl *member) {
48064806
auto funcDecl = dyn_cast<FuncDecl>(member);
4807-
return funcDecl && funcDecl->isCallable();
4807+
return funcDecl && funcDecl->isCallFunction();
48084808
});
48094809

48104810
// Diagnose @dynamicCallable errors.

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5794,7 +5794,7 @@ ConstraintSystem::simplifyApplicableFnConstraint(
57945794
// Get all call methods of the nominal type.
57955795
// TODO: Consider caching?
57965796
SmallVector<FuncDecl *, 4> callMethods;
5797-
auto candidates = lookupMember(desugar2, DeclName(ctx.Id_call));
5797+
auto candidates = lookupMember(desugar2, DeclName(ctx.Id_callFunction));
57985798
for (auto entry : candidates) {
57995799
auto callMethod = dyn_cast<FuncDecl>(entry.getValueDecl());
58005800
if (!callMethod)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
public protocol Layer {
2-
func call(_ input: Float) -> Float
2+
func callFunction(_ input: Float) -> Float
33
}
44

55
public struct Dense {
66
public init() {}
77

8-
public func call(_ input: Float) -> Float {
8+
public func callFunction(_ input: Float) -> Float {
99
return input * 2
1010
}
1111
}

test/Sema/call_method_generic.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
// RUN: %target-typecheck-verify-swift
22

33
protocol P0 {
4-
func call(x: Self)
4+
func callFunction(x: Self)
55
}
66

77
struct ConcreteType {
8-
func call<T, U>(_ x: T, _ y: U) -> (T, U) {
8+
func callFunction<T, U>(_ x: T, _ y: U) -> (T, U) {
99
return (x, y)
1010
}
1111

12-
func call<T, U>(_ fn: @escaping (T) -> U) -> (T) -> U {
12+
func callFunction<T, U>(_ fn: @escaping (T) -> U) -> (T) -> U {
1313
return fn
1414
}
1515
}
1616

1717
let concrete = ConcreteType()
1818
_ = concrete(1, 3.0)
19-
_ = concrete(concrete, concrete.call as ([Int], Float) -> ([Int], Float))
19+
_ = concrete(concrete, concrete.callFunction as ([Int], Float) -> ([Int], Float))
2020

2121
func generic<T, U>(_ x: T, _ y: U) {
2222
_ = concrete(x, x)
@@ -25,14 +25,14 @@ func generic<T, U>(_ x: T, _ y: U) {
2525

2626
struct GenericType<T : Collection> {
2727
let collection: T
28-
func call<U>(_ x: U) -> Bool where U == T.Element, U : Equatable {
28+
func callFunction<U>(_ x: U) -> Bool where U == T.Element, U : Equatable {
2929
return collection.contains(x)
3030
}
3131
}
3232

3333
// Test conditional conformance.
3434
extension GenericType where T.Element : Numeric {
35-
func call(initialValue: T.Element) -> T.Element {
35+
func callFunction(initialValue: T.Element) -> T.Element {
3636
return collection.reduce(initialValue, +)
3737
}
3838
}

test/Sema/call_method_protocol.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %target-typecheck-verify-swift
22

33
protocol P0 {
4-
// expected-note @+1 {{protocol requires function 'call()' with type '() -> Missing'; do you want to add a stub?}}
5-
func call() -> Self
4+
// expected-note @+1 {{protocol requires function 'callFunction()' with type '() -> Missing'; do you want to add a stub?}}
5+
func callFunction() -> Self
66
}
77
func testProtocol(_ x: P0) {
88
_ = x()
@@ -12,18 +12,18 @@ func testGeneric<T : P0>(_ x: T) {
1212
}
1313

1414
protocol P1 {
15-
func call() -> Self
15+
func callFunction() -> Self
1616
}
1717
extension P1 {
1818
// expected-note @+1 {{found this candidate}}
19-
func call() -> Self {
19+
func callFunction() -> Self {
2020
return self
2121
}
2222
}
2323
protocol P2 {}
2424
extension P2 {
2525
// expected-note @+1 {{found this candidate}}
26-
func call(x: Int, y: Int) -> Int {
26+
func callFunction(x: Int, y: Int) -> Int {
2727
return x + y
2828
}
2929
}
@@ -32,13 +32,13 @@ extension P2 {
3232
struct Missing : P0 {}
3333
struct S0 : P0 {
3434
@discardableResult
35-
func call() -> S0 { return self }
35+
func callFunction() -> S0 { return self }
3636
}
3737
let s0 = S0()
3838
s0()
3939

4040
struct S1 : P1 {
41-
func call() -> S1 { return self }
41+
func callFunction() -> S1 { return self }
4242
}
4343

4444
let s1 = S1()
@@ -47,13 +47,13 @@ _ = s1()()
4747
struct Conforming : P0 & P1 & P2 {}
4848
let conforming = Conforming()
4949
_ = conforming(x: 1, y: 2)
50-
_ = conforming().call(x:y:)(1, 2)
51-
_ = conforming.call(x:y:)
52-
_ = conforming.call // expected-error {{ambiguous use of 'call'}}
50+
_ = conforming().callFunction(x:y:)(1, 2)
51+
_ = conforming.callFunction(x:y:)
52+
_ = conforming.callFunction // expected-error {{ambiguous use of 'callFunction'}}
5353

5454
protocol P3 {}
5555
extension P3 {
56-
func call() -> Self { return self }
56+
func callFunction() -> Self { return self }
5757
}
5858
struct S3 : P3 {}
5959

test/Sema/call_method_simple.swift

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %target-typecheck-verify-swift
22

33
struct SimpleCallable {
4-
func call(_ x: Float) -> Float {
4+
func callFunction(_ x: Float) -> Float {
55
return x
66
}
77
}
@@ -21,11 +21,11 @@ let _: (Float) -> Float = foo
2121

2222
// Test direct `call` member references.
2323

24-
_ = foo.call(1)
25-
_ = [1, 2, 3].map(foo.call)
26-
_ = foo.call(foo(1))
27-
_ = foo(foo.call(1))
28-
let _: (Float) -> Float = foo.call
24+
_ = foo.callFunction(1)
25+
_ = [1, 2, 3].map(foo.callFunction)
26+
_ = foo.callFunction(foo(1))
27+
_ = foo(foo.callFunction(1))
28+
let _: (Float) -> Float = foo.callFunction
2929

3030
func callable() -> SimpleCallable {
3131
return SimpleCallable()
@@ -42,43 +42,56 @@ extension SimpleCallable {
4242
_ = foo.foo(1)
4343
_ = foo.bar()(1)
4444
_ = callable()(1)
45-
_ = [1, 2, 3].map(foo.foo.call)
46-
_ = [1, 2, 3].map(foo.bar().call)
47-
_ = [1, 2, 3].map(callable().call)
45+
_ = [1, 2, 3].map(foo.foo.callFunction)
46+
_ = [1, 2, 3].map(foo.bar().callFunction)
47+
_ = [1, 2, 3].map(callable().callFunction)
4848

4949
struct MultipleArgsCallable {
50-
func call(x: Int, y: Float) -> [Int] {
50+
func callFunction(x: Int, y: Float) -> [Int] {
5151
return [x]
5252
}
5353
}
5454

5555
let bar = MultipleArgsCallable()
5656
_ = bar(x: 1, y: 1)
57-
_ = bar.call(x: 1, y: 1)
58-
_ = bar(x: bar.call(x: 1, y: 1)[0], y: 1)
59-
_ = bar.call(x: bar(x: 1, y: 1)[0], y: 1)
57+
_ = bar.callFunction(x: 1, y: 1)
58+
_ = bar(x: bar.callFunction(x: 1, y: 1)[0], y: 1)
59+
_ = bar.callFunction(x: bar(x: 1, y: 1)[0], y: 1)
6060
_ = bar(1, 1) // expected-error {{missing argument labels 'x:y:' in call}}
6161

6262
struct Extended {}
6363
extension Extended {
6464
@discardableResult
65-
func call() -> Extended {
65+
func callFunction() -> Extended {
6666
return self
6767
}
6868
}
6969
var extended = Extended()
70-
extended()().call()()
70+
extended()().callFunction()()
71+
72+
struct TakesTrailingClosure {
73+
func callFunction(_ fn: () -> Void) {
74+
fn()
75+
}
76+
func callFunction(_ x: Int, label y: Float, _ fn: (Int, Float) -> Void) {
77+
fn(x, y)
78+
}
79+
}
80+
var takesTrailingClosure = TakesTrailingClosure()
81+
takesTrailingClosure { print("Hi") }
82+
takesTrailingClosure() { print("Hi") }
83+
takesTrailingClosure(1, label: 2) { _ = Float($0) + $1 }
7184

7285
struct OptionalCallable {
73-
func call() -> OptionalCallable? {
86+
func callFunction() -> OptionalCallable? {
7487
return self
7588
}
7689
}
7790
var optional = OptionalCallable()
78-
_ = optional()?.call()?()
91+
_ = optional()?.callFunction()?()
7992

8093
struct Variadic {
81-
func call(_ args: Int...) -> [Int] {
94+
func callFunction(_ args: Int...) -> [Int] {
8295
return args
8396
}
8497
}
@@ -88,35 +101,35 @@ _ = variadic(1, 2, 3)
88101

89102
struct Mutating {
90103
var x: Int
91-
mutating func call() {
104+
mutating func callFunction() {
92105
x += 1
93106
}
94107
}
95108
func testMutating(_ x: Mutating, _ y: inout Mutating) {
96109
_ = x() // expected-error {{cannot use mutating member on immutable value: 'x' is a 'let' constant}}
97-
_ = x.call() // expected-error {{cannot use mutating member on immutable value: 'x' is a 'let' constant}}
110+
_ = x.callFunction() // expected-error {{cannot use mutating member on immutable value: 'x' is a 'let' constant}}
98111
_ = y()
99-
_ = y.call()
112+
_ = y.callFunction()
100113
}
101114

102115
struct Inout {
103-
func call(_ x: inout Int) {
116+
func callFunction(_ x: inout Int) {
104117
x += 5
105118
}
106119
}
107120
func testInout(_ x: Inout, _ arg: inout Int) {
108121
x(&arg)
109-
x.call(&arg)
122+
x.callFunction(&arg)
110123
// TODO: Improve this error to match the error using a direct `call` member reference.
111124
// expected-error @+2 {{cannot invoke 'x' with an argument list of type '(Int)'}}
112125
// expected-error @+1 {{cannot call value of non-function type 'Inout'}}
113126
x(arg)
114127
// expected-error @+1 {{passing value of type 'Int' to an inout parameter requires explicit '&'}}
115-
x.call(arg)
128+
x.callFunction(arg)
116129
}
117130

118131
struct Autoclosure {
119-
func call(_ condition: @autoclosure () -> Bool,
132+
func callFunction(_ condition: @autoclosure () -> Bool,
120133
_ message: @autoclosure () -> String) {
121134
if condition() {
122135
print(message())
@@ -129,10 +142,10 @@ func testAutoclosure(_ x: Autoclosure) {
129142
}
130143

131144
struct Throwing {
132-
func call() throws -> Throwing {
145+
func callFunction() throws -> Throwing {
133146
return self
134147
}
135-
func call(_ f: () throws -> ()) rethrows {
148+
func callFunction(_ f: () throws -> ()) rethrows {
136149
try f()
137150
}
138151
}
@@ -145,7 +158,7 @@ enum BinaryOperation {
145158
case add, subtract, multiply, divide
146159
}
147160
extension BinaryOperation {
148-
func call(_ lhs: Float, _ rhs: Float) -> Float {
161+
func callFunction(_ lhs: Float, _ rhs: Float) -> Float {
149162
switch self {
150163
case .add: return lhs + rhs
151164
case .subtract: return lhs - rhs
@@ -157,12 +170,12 @@ extension BinaryOperation {
157170
_ = BinaryOperation.add(1, 2)
158171

159172
class BaseClass {
160-
func call() -> Self {
173+
func callFunction() -> Self {
161174
return self
162175
}
163176
}
164177
class SubClass : BaseClass {
165-
override func call() -> Self {
178+
override func callFunction() -> Self {
166179
return self
167180
}
168181
}
@@ -173,10 +186,10 @@ func testIUO(a: SimpleCallable!, b: MultipleArgsCallable!, c: Extended!,
173186
_ = a(1)
174187
_ = b(x: 1, y: 1)
175188
_ = c()
176-
_ = d()?.call()?()
189+
_ = d()?.callFunction()?()
177190
_ = e()
178191
_ = e(1, 2, 3)
179-
// FIXME(TF-444): `mutating func call` and IUO doesn't work.
192+
// FIXME(TF-444): `mutating func callFunction` and IUO doesn't work.
180193
// _ = f()
181194
_ = g(&inoutInt)
182195
_ = try? h()

0 commit comments

Comments
 (0)