Skip to content

Commit e0b67d4

Browse files
committed
[Callable] 'func call' -> 'func callAsFunction'.
Apply the final naming decision in [SE-0253](https://github.com/apple/swift-evolution/blob/master/proposals/0253-callable.md) to the 'tensorflow' branch implementation.
1 parent f537a92 commit e0b67d4

File tree

8 files changed

+61
-59
lines changed

8 files changed

+61
-59
lines changed

include/swift/AST/Decl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5950,7 +5950,7 @@ class FuncDecl : public AbstractFunctionDecl {
59505950
return Bits.FuncDecl.IsStatic;
59515951
}
59525952
bool isCallable() const {
5953-
return getName().str() == "call" && isInstanceMember();
5953+
return getName().str() == "callAsFunction" && isInstanceMember();
59545954
}
59555955
/// \returns the way 'static'/'class' was spelled in the source.
59565956
StaticSpellingKind getStaticSpelling() const {

include/swift/AST/KnownIdentifiers.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ IDENTIFIER(ArrayLiteralElement)
3232
IDENTIFIER(atIndexedSubscript)
3333
IDENTIFIER_(bridgeToObjectiveC)
3434
// SWIFT_ENABLE_TENSORFLOW
35-
IDENTIFIER(call)
35+
IDENTIFIER(callAsFunction)
3636
IDENTIFIER_WITH_NAME(code_, "_code")
3737
IDENTIFIER(CodingKeys)
3838
IDENTIFIER(combine)

lib/Sema/CSApply.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7140,8 +7140,8 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
71407140
};
71417141

71427142
// SWIFT_ENABLE_TENSORFLOW
7143-
// Save the original potentially lvalue function for rewriting call method
7144-
// applications.
7143+
// Save the original potentially lvalue function for rewriting
7144+
// 'callAsFunction' method applications.
71457145
auto *originalFn = fn;
71467146
// SWIFT_ENABLE_TENSORFLOW END
71477147
// The function is always an rvalue.
@@ -7291,7 +7291,7 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
72917291
}
72927292

72937293
// SWIFT_ENABLE_TENSORFLOW
7294-
// Handle call method applications.
7294+
// Handle 'callAsFunction' method applications.
72957295
auto &ctx = cs.getASTContext();
72967296

72977297
TupleExpr *arg = dyn_cast<TupleExpr>(apply->getArg());

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5827,7 +5827,7 @@ ConstraintSystem::simplifyApplicableFnConstraint(
58275827
auto &ctx = getASTContext();
58285828
// Get all call methods of the nominal type.
58295829
SmallVector<FuncDecl *, 4> callMethods;
5830-
auto candidates = lookupMember(desugar2, DeclName(ctx.Id_call));
5830+
auto candidates = lookupMember(desugar2, DeclName(ctx.Id_callAsFunction));
58315831
for (auto entry : candidates) {
58325832
auto callMethod = dyn_cast<FuncDecl>(entry.getValueDecl());
58335833
if (!callMethod)
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
// SWIFT_ENABLE_TENSORFLOW
2+
13
public protocol Layer : Differentiable {
24
@differentiable
3-
func call(_ input: Float) -> Float
5+
func callAsFunction(_ input: Float) -> Float
46
}
57

68
public struct Dense : Differentiable {
79
public init() {}
810

911
@differentiable
10-
public func call(_ input: Float) -> Float {
12+
public func callAsFunction(_ input: Float) -> Float {
1113
return input * 2
1214
}
1315
}

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 callAsFunction(x: Self)
55
}
66

77
struct ConcreteType {
8-
func call<T, U>(_ x: T, _ y: U) -> (T, U) {
8+
func callAsFunction<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 callAsFunction<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.callAsFunction 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 callAsFunction<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 callAsFunction(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 'callAsFunction()' with type '() -> Missing'; do you want to add a stub?}}
5+
func callAsFunction() -> 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 callAsFunction() -> Self
1616
}
1717
extension P1 {
1818
// expected-note @+1 {{found this candidate}}
19-
func call() -> Self {
19+
func callAsFunction() -> 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 callAsFunction(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 callAsFunction() -> S0 { return self }
3636
}
3737
let s0 = S0()
3838
s0()
3939

4040
struct S1 : P1 {
41-
func call() -> S1 { return self }
41+
func callAsFunction() -> 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().callAsFunction(x:y:)(1, 2)
51+
_ = conforming.callAsFunction(x:y:)
52+
_ = conforming.callAsFunction // expected-error {{ambiguous use of 'callAsFunction'}}
5353

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

test/Sema/call_method_simple.swift

Lines changed: 34 additions & 34 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 callAsFunction(_ x: Float) -> Float {
55
return x
66
}
77
}
@@ -12,20 +12,20 @@ let foo = SimpleCallable()
1212
_ = foo(1)
1313
_ = foo(foo(1))
1414

15-
// TODO: Improve this error to match the error using a direct `call` member reference.
15+
// TODO: Improve this error to match the error using a direct `callAsFunction` member reference.
1616
// expected-error @+2 {{cannot call value of non-function type 'SimpleCallable'}}
1717
// expected-error @+1 {{cannot invoke 'foo' with an argument list of type '(Int, Int)'}}
1818
_ = foo(1, 1)
1919
// expected-error @+1 {{cannot convert value of type 'SimpleCallable' to specified type '(Float) -> Float'}}
2020
let _: (Float) -> Float = foo
2121

22-
// Test direct `call` member references.
22+
// Test direct `callAsFunction` 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.callAsFunction(1)
25+
_ = [1, 2, 3].map(foo.callAsFunction)
26+
_ = foo.callAsFunction(foo(1))
27+
_ = foo(foo.callAsFunction(1))
28+
let _: (Float) -> Float = foo.callAsFunction
2929

3030
func callable() -> SimpleCallable {
3131
return SimpleCallable()
@@ -42,43 +42,43 @@ 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.callAsFunction)
46+
_ = [1, 2, 3].map(foo.bar().callAsFunction)
47+
_ = [1, 2, 3].map(callable().callAsFunction)
4848

4949
struct MultipleArgsCallable {
50-
func call(x: Int, y: Float) -> [Int] {
50+
func callAsFunction(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.callAsFunction(x: 1, y: 1)
58+
_ = bar(x: bar.callAsFunction(x: 1, y: 1)[0], y: 1)
59+
_ = bar.callAsFunction(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 callAsFunction() -> Extended {
6666
return self
6767
}
6868
}
6969
var extended = Extended()
70-
extended()().call()()
70+
extended()().callAsFunction()()
7171

7272
struct OptionalCallable {
73-
func call() -> OptionalCallable? {
73+
func callAsFunction() -> OptionalCallable? {
7474
return self
7575
}
7676
}
7777
var optional = OptionalCallable()
78-
_ = optional()?.call()?()
78+
_ = optional()?.callAsFunction()?()
7979

8080
struct Variadic {
81-
func call(_ args: Int...) -> [Int] {
81+
func callAsFunction(_ args: Int...) -> [Int] {
8282
return args
8383
}
8484
}
@@ -88,36 +88,36 @@ _ = variadic(1, 2, 3)
8888

8989
struct Mutating {
9090
var x: Int
91-
mutating func call() {
91+
mutating func callAsFunction() {
9292
x += 1
9393
}
9494
}
9595
func testMutating(_ x: Mutating, _ y: inout Mutating) {
9696
_ = 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}}
97+
_ = x.callAsFunction() // expected-error {{cannot use mutating member on immutable value: 'x' is a 'let' constant}}
9898
_ = y()
99-
_ = y.call()
99+
_ = y.callAsFunction()
100100
}
101101

102102
struct Inout {
103-
func call(_ x: inout Int) {
103+
func callAsFunction(_ x: inout Int) {
104104
x += 5
105105
}
106106
}
107107
func testInout(_ x: Inout, _ arg: inout Int) {
108108
x(&arg)
109-
x.call(&arg)
109+
x.callAsFunction(&arg)
110110
// TODO: Improve this error to match the error using a direct `call` member reference.
111111
// expected-error @+2 {{cannot invoke 'x' with an argument list of type '(Int)'}}
112112
// expected-error @+1 {{cannot call value of non-function type 'Inout'}}
113113
x(arg)
114114
// expected-error @+1 {{passing value of type 'Int' to an inout parameter requires explicit '&'}}
115-
x.call(arg)
115+
x.callAsFunction(arg)
116116
}
117117

118118
struct Autoclosure {
119-
func call(_ condition: @autoclosure () -> Bool,
120-
_ message: @autoclosure () -> String) {
119+
func callAsFunction(_ condition: @autoclosure () -> Bool,
120+
_ message: @autoclosure () -> String) {
121121
if condition() {
122122
print(message())
123123
}
@@ -129,10 +129,10 @@ func testAutoclosure(_ x: Autoclosure) {
129129
}
130130

131131
struct Throwing {
132-
func call() throws -> Throwing {
132+
func callAsFunction() throws -> Throwing {
133133
return self
134134
}
135-
func call(_ f: () throws -> ()) rethrows {
135+
func callAsFunction(_ f: () throws -> ()) rethrows {
136136
try f()
137137
}
138138
}
@@ -145,7 +145,7 @@ enum BinaryOperation {
145145
case add, subtract, multiply, divide
146146
}
147147
extension BinaryOperation {
148-
func call(_ lhs: Float, _ rhs: Float) -> Float {
148+
func callAsFunction(_ lhs: Float, _ rhs: Float) -> Float {
149149
switch self {
150150
case .add: return lhs + rhs
151151
case .subtract: return lhs - rhs
@@ -157,12 +157,12 @@ extension BinaryOperation {
157157
_ = BinaryOperation.add(1, 2)
158158

159159
class BaseClass {
160-
func call() -> Self {
160+
func callAsFunction() -> Self {
161161
return self
162162
}
163163
}
164164
class SubClass : BaseClass {
165-
override func call() -> Self {
165+
override func callAsFunction() -> Self {
166166
return self
167167
}
168168
}
@@ -173,7 +173,7 @@ func testIUO(a: SimpleCallable!, b: MultipleArgsCallable!, c: Extended!,
173173
_ = a(1)
174174
_ = b(x: 1, y: 1)
175175
_ = c()
176-
_ = d()?.call()?()
176+
_ = d()?.callAsFunction()?()
177177
_ = e()
178178
_ = e(1, 2, 3)
179179
// FIXME(TF-444): `mutating func call` and IUO doesn't work.

0 commit comments

Comments
 (0)