Skip to content

[Callable] 'func call' -> 'func callAsFunction'. #25235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5950,7 +5950,7 @@ class FuncDecl : public AbstractFunctionDecl {
return Bits.FuncDecl.IsStatic;
}
bool isCallable() const {
return getName().str() == "call" && isInstanceMember();
return getName().str() == "callAsFunction" && isInstanceMember();
}
/// \returns the way 'static'/'class' was spelled in the source.
StaticSpellingKind getStaticSpelling() const {
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/KnownIdentifiers.def
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ IDENTIFIER(ArrayLiteralElement)
IDENTIFIER(atIndexedSubscript)
IDENTIFIER_(bridgeToObjectiveC)
// SWIFT_ENABLE_TENSORFLOW
IDENTIFIER(call)
IDENTIFIER(callAsFunction)
IDENTIFIER_WITH_NAME(code_, "_code")
IDENTIFIER(CodingKeys)
IDENTIFIER(combine)
Expand Down
6 changes: 3 additions & 3 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7140,8 +7140,8 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
};

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

// SWIFT_ENABLE_TENSORFLOW
// Handle call method applications.
// Handle 'callAsFunction' method applications.
auto &ctx = cs.getASTContext();

TupleExpr *arg = dyn_cast<TupleExpr>(apply->getArg());
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5827,7 +5827,7 @@ ConstraintSystem::simplifyApplicableFnConstraint(
auto &ctx = getASTContext();
// Get all call methods of the nominal type.
SmallVector<FuncDecl *, 4> callMethods;
auto candidates = lookupMember(desugar2, DeclName(ctx.Id_call));
auto candidates = lookupMember(desugar2, DeclName(ctx.Id_callAsFunction));
for (auto entry : candidates) {
auto callMethod = dyn_cast<FuncDecl>(entry.getValueDecl());
if (!callMethod)
Expand Down
6 changes: 4 additions & 2 deletions test/Sema/Inputs/call_method_other_module.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// SWIFT_ENABLE_TENSORFLOW

public protocol Layer : Differentiable {
@differentiable
func call(_ input: Float) -> Float
func callAsFunction(_ input: Float) -> Float
}

public struct Dense : Differentiable {
public init() {}

@differentiable
public func call(_ input: Float) -> Float {
public func callAsFunction(_ input: Float) -> Float {
return input * 2
}
}
12 changes: 6 additions & 6 deletions test/Sema/call_method_generic.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
// RUN: %target-typecheck-verify-swift

protocol P0 {
func call(x: Self)
func callAsFunction(x: Self)
}

struct ConcreteType {
func call<T, U>(_ x: T, _ y: U) -> (T, U) {
func callAsFunction<T, U>(_ x: T, _ y: U) -> (T, U) {
return (x, y)
}

func call<T, U>(_ fn: @escaping (T) -> U) -> (T) -> U {
func callAsFunction<T, U>(_ fn: @escaping (T) -> U) -> (T) -> U {
return fn
}
}

let concrete = ConcreteType()
_ = concrete(1, 3.0)
_ = concrete(concrete, concrete.call as ([Int], Float) -> ([Int], Float))
_ = concrete(concrete, concrete.callAsFunction as ([Int], Float) -> ([Int], Float))

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

struct GenericType<T : Collection> {
let collection: T
func call<U>(_ x: U) -> Bool where U == T.Element, U : Equatable {
func callAsFunction<U>(_ x: U) -> Bool where U == T.Element, U : Equatable {
return collection.contains(x)
}
}

// Test conditional conformance.
extension GenericType where T.Element : Numeric {
func call(initialValue: T.Element) -> T.Element {
func callAsFunction(initialValue: T.Element) -> T.Element {
return collection.reduce(initialValue, +)
}
}
Expand Down
22 changes: 11 additions & 11 deletions test/Sema/call_method_protocol.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// RUN: %target-typecheck-verify-swift

protocol P0 {
// expected-note @+1 {{protocol requires function 'call()' with type '() -> Missing'; do you want to add a stub?}}
func call() -> Self
// expected-note @+1 {{protocol requires function 'callAsFunction()' with type '() -> Missing'; do you want to add a stub?}}
func callAsFunction() -> Self
}
func testProtocol(_ x: P0) {
_ = x()
Expand All @@ -12,18 +12,18 @@ func testGeneric<T : P0>(_ x: T) {
}

protocol P1 {
func call() -> Self
func callAsFunction() -> Self
}
extension P1 {
// expected-note @+1 {{found this candidate}}
func call() -> Self {
func callAsFunction() -> Self {
return self
}
}
protocol P2 {}
extension P2 {
// expected-note @+1 {{found this candidate}}
func call(x: Int, y: Int) -> Int {
func callAsFunction(x: Int, y: Int) -> Int {
return x + y
}
}
Expand All @@ -32,13 +32,13 @@ extension P2 {
struct Missing : P0 {}
struct S0 : P0 {
@discardableResult
func call() -> S0 { return self }
func callAsFunction() -> S0 { return self }
}
let s0 = S0()
s0()

struct S1 : P1 {
func call() -> S1 { return self }
func callAsFunction() -> S1 { return self }
}

let s1 = S1()
Expand All @@ -47,13 +47,13 @@ _ = s1()()
struct Conforming : P0 & P1 & P2 {}
let conforming = Conforming()
_ = conforming(x: 1, y: 2)
_ = conforming().call(x:y:)(1, 2)
_ = conforming.call(x:y:)
_ = conforming.call // expected-error {{ambiguous use of 'call'}}
_ = conforming().callAsFunction(x:y:)(1, 2)
_ = conforming.callAsFunction(x:y:)
_ = conforming.callAsFunction // expected-error {{ambiguous use of 'callAsFunction'}}

protocol P3 {}
extension P3 {
func call() -> Self { return self }
func callAsFunction() -> Self { return self }
}
struct S3 : P3 {}

Expand Down
68 changes: 34 additions & 34 deletions test/Sema/call_method_simple.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %target-typecheck-verify-swift

struct SimpleCallable {
func call(_ x: Float) -> Float {
func callAsFunction(_ x: Float) -> Float {
return x
}
}
Expand All @@ -12,20 +12,20 @@ let foo = SimpleCallable()
_ = foo(1)
_ = foo(foo(1))

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

// Test direct `call` member references.
// Test direct `callAsFunction` member references.

_ = foo.call(1)
_ = [1, 2, 3].map(foo.call)
_ = foo.call(foo(1))
_ = foo(foo.call(1))
let _: (Float) -> Float = foo.call
_ = foo.callAsFunction(1)
_ = [1, 2, 3].map(foo.callAsFunction)
_ = foo.callAsFunction(foo(1))
_ = foo(foo.callAsFunction(1))
let _: (Float) -> Float = foo.callAsFunction

func callable() -> SimpleCallable {
return SimpleCallable()
Expand All @@ -42,43 +42,43 @@ extension SimpleCallable {
_ = foo.foo(1)
_ = foo.bar()(1)
_ = callable()(1)
_ = [1, 2, 3].map(foo.foo.call)
_ = [1, 2, 3].map(foo.bar().call)
_ = [1, 2, 3].map(callable().call)
_ = [1, 2, 3].map(foo.foo.callAsFunction)
_ = [1, 2, 3].map(foo.bar().callAsFunction)
_ = [1, 2, 3].map(callable().callAsFunction)

struct MultipleArgsCallable {
func call(x: Int, y: Float) -> [Int] {
func callAsFunction(x: Int, y: Float) -> [Int] {
return [x]
}
}

let bar = MultipleArgsCallable()
_ = bar(x: 1, y: 1)
_ = bar.call(x: 1, y: 1)
_ = bar(x: bar.call(x: 1, y: 1)[0], y: 1)
_ = bar.call(x: bar(x: 1, y: 1)[0], y: 1)
_ = bar.callAsFunction(x: 1, y: 1)
_ = bar(x: bar.callAsFunction(x: 1, y: 1)[0], y: 1)
_ = bar.callAsFunction(x: bar(x: 1, y: 1)[0], y: 1)
_ = bar(1, 1) // expected-error {{missing argument labels 'x:y:' in call}}

struct Extended {}
extension Extended {
@discardableResult
func call() -> Extended {
func callAsFunction() -> Extended {
return self
}
}
var extended = Extended()
extended()().call()()
extended()().callAsFunction()()

struct OptionalCallable {
func call() -> OptionalCallable? {
func callAsFunction() -> OptionalCallable? {
return self
}
}
var optional = OptionalCallable()
_ = optional()?.call()?()
_ = optional()?.callAsFunction()?()

struct Variadic {
func call(_ args: Int...) -> [Int] {
func callAsFunction(_ args: Int...) -> [Int] {
return args
}
}
Expand All @@ -88,36 +88,36 @@ _ = variadic(1, 2, 3)

struct Mutating {
var x: Int
mutating func call() {
mutating func callAsFunction() {
x += 1
}
}
func testMutating(_ x: Mutating, _ y: inout Mutating) {
_ = x() // expected-error {{cannot use mutating member on immutable value: 'x' is a 'let' constant}}
_ = x.call() // expected-error {{cannot use mutating member on immutable value: 'x' is a 'let' constant}}
_ = x.callAsFunction() // expected-error {{cannot use mutating member on immutable value: 'x' is a 'let' constant}}
_ = y()
_ = y.call()
_ = y.callAsFunction()
}

struct Inout {
func call(_ x: inout Int) {
func callAsFunction(_ x: inout Int) {
x += 5
}
}
func testInout(_ x: Inout, _ arg: inout Int) {
x(&arg)
x.call(&arg)
x.callAsFunction(&arg)
// TODO: Improve this error to match the error using a direct `call` member reference.
// expected-error @+2 {{cannot invoke 'x' with an argument list of type '(Int)'}}
// expected-error @+1 {{cannot call value of non-function type 'Inout'}}
x(arg)
// expected-error @+1 {{passing value of type 'Int' to an inout parameter requires explicit '&'}}
x.call(arg)
x.callAsFunction(arg)
}

struct Autoclosure {
func call(_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String) {
func callAsFunction(_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String) {
if condition() {
print(message())
}
Expand All @@ -129,10 +129,10 @@ func testAutoclosure(_ x: Autoclosure) {
}

struct Throwing {
func call() throws -> Throwing {
func callAsFunction() throws -> Throwing {
return self
}
func call(_ f: () throws -> ()) rethrows {
func callAsFunction(_ f: () throws -> ()) rethrows {
try f()
}
}
Expand All @@ -145,7 +145,7 @@ enum BinaryOperation {
case add, subtract, multiply, divide
}
extension BinaryOperation {
func call(_ lhs: Float, _ rhs: Float) -> Float {
func callAsFunction(_ lhs: Float, _ rhs: Float) -> Float {
switch self {
case .add: return lhs + rhs
case .subtract: return lhs - rhs
Expand All @@ -157,12 +157,12 @@ extension BinaryOperation {
_ = BinaryOperation.add(1, 2)

class BaseClass {
func call() -> Self {
func callAsFunction() -> Self {
return self
}
}
class SubClass : BaseClass {
override func call() -> Self {
override func callAsFunction() -> Self {
return self
}
}
Expand All @@ -173,7 +173,7 @@ func testIUO(a: SimpleCallable!, b: MultipleArgsCallable!, c: Extended!,
_ = a(1)
_ = b(x: 1, y: 1)
_ = c()
_ = d()?.call()?()
_ = d()?.callAsFunction()?()
_ = e()
_ = e(1, 2, 3)
// FIXME(TF-444): `mutating func call` and IUO doesn't work.
Expand Down
2 changes: 1 addition & 1 deletion utils/update_checkout/update-checkout-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@
"clang-tools-extra": "swift-DEVELOPMENT-SNAPSHOT-2019-05-26-a",
"libcxx": "swift-DEVELOPMENT-SNAPSHOT-2019-05-26-a",
"tensorflow": "ebc41609e27dcf0998d8970e77a2e1f53e13ac86",
"tensorflow-swift-apis": "835d1436a01d9261f0467bc2803cc7f6ac56ed80",
"tensorflow-swift-apis": "e9092254c8135202a813a6cab727fbca691010e8",
"indexstore-db": "swift-DEVELOPMENT-SNAPSHOT-2019-05-26-a",
"sourcekit-lsp": "swift-DEVELOPMENT-SNAPSHOT-2019-05-26-a"
}
Expand Down