Skip to content

Commit 518bda2

Browse files
committed
Implement SE-0253 review feedback.
Use `callAsFunction` as the call-syntax delegate method name.
1 parent d4737db commit 518bda2

11 files changed

+88
-88
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 isCallFunction() const {
5962-
return getName().str() == "callFunction" && isInstanceMember();
5961+
bool isCallAsFunctionMethod() const {
5962+
return getName().str() == "callAsFunction" && 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(callFunction)
34+
IDENTIFIER(callAsFunction)
3535
IDENTIFIER_WITH_NAME(code_, "_code")
3636
IDENTIFIER(CodingKeys)
3737
IDENTIFIER(combine)

lib/Sema/CSApply.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7051,8 +7051,8 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
70517051
llvm_unreachable("Unhandled DeclTypeCheckingSemantics in switch.");
70527052
};
70537053

7054-
// Save the original potentially lvalue function for rewriting call method
7055-
// applications.
7054+
// Save the original potentially lvalue function for rewriting
7055+
// `callAsFunction` method applications.
70567056
auto *originalFn = fn;
70577057
// The function is always an rvalue.
70587058
fn = cs.coerceToRValue(fn);
@@ -7200,19 +7200,19 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
72007200
return finishApply(apply, openedType, locator);
72017201
}
72027202

7203-
// Handle call method applications.
7203+
// Handle `callAsFunction` method applications.
72047204
auto &ctx = cs.getASTContext();
72057205

72067206
TupleExpr *arg = dyn_cast<TupleExpr>(apply->getArg());
72077207
if (auto parenExpr = dyn_cast<ParenExpr>(apply->getArg()))
72087208
arg = TupleExpr::createImplicit(ctx, parenExpr->getSubExpr(), {});
72097209

7210-
// Get resolved call method and verify it.
7210+
// Get resolved `callAsFunction` method and verify it.
72117211
auto loc = locator.withPathElement(ConstraintLocator::ApplyFunction);
72127212
auto selected = solution.getOverloadChoice(cs.getConstraintLocator(loc));
72137213
auto choice = selected.choice;
72147214
auto *callMethod = dyn_cast<FuncDecl>(selected.choice.getDecl());
7215-
if (callMethod && callMethod->isCallFunction()) {
7215+
if (callMethod && callMethod->isCallAsFunctionMethod()) {
72167216
auto methodType =
72177217
simplifyType(selected.openedType)->castTo<AnyFunctionType>();
72187218
auto selfParam = callMethod->getImplicitSelfDecl();
@@ -7227,7 +7227,7 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
72277227
failure.diagnose();
72287228
return nullptr;
72297229
}
7230-
// Create direct reference to call method.
7230+
// Create direct reference to `callAsFunction` method.
72317231
bool isDynamic = choice.getKind() == OverloadChoiceKind::DeclViaDynamic;
72327232
Expr *declRef = buildMemberRef(originalFn, selected.openedFullType,
72337233
/*dotLoc=*/SourceLoc(), choice,
@@ -7240,7 +7240,7 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
72407240
return nullptr;
72417241
declRef->setImplicit(apply->isImplicit());
72427242
apply->setFn(declRef);
7243-
// Coerce argument to input type of call method.
7243+
// Coerce argument to input type of the `callAsFunction` method.
72447244
SmallVector<Identifier, 2> argLabelsScratch;
72457245
auto *arg = coerceCallArguments(apply->getArg(), methodType, apply,
72467246
apply->getArgumentLabels(argLabelsScratch),

lib/Sema/CSDiag.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4797,12 +4797,12 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
47974797
auto isDynamicCallable =
47984798
CS.DynamicCallableCache[fnType->getCanonicalType()].isValid();
47994799

4800-
// TODO: Consider caching?
4800+
// Note: Consider caching `hasCallAsFunctionMethods` in `NominalTypeDecl`.
48014801
auto *nominal = fnType->getAnyNominal();
4802-
auto hasCallMethods = nominal &&
4802+
auto hasCallAsFunctionMethods = nominal &&
48034803
llvm::any_of(nominal->getMembers(), [](Decl *member) {
48044804
auto funcDecl = dyn_cast<FuncDecl>(member);
4805-
return funcDecl && funcDecl->isCallFunction();
4805+
return funcDecl && funcDecl->isCallAsFunctionMethod();
48064806
});
48074807

48084808
// Diagnose @dynamicCallable errors.
@@ -4862,7 +4862,7 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
48624862
}
48634863
}
48644864

4865-
if (!isDynamicCallable && !hasCallMethods)
4865+
if (!isDynamicCallable && !hasCallAsFunctionMethods)
48664866
return true;
48674867
}
48684868

lib/Sema/CSSimplify.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5833,27 +5833,27 @@ ConstraintSystem::simplifyApplicableFnConstraint(
58335833
return simplified;
58345834
}
58355835

5836-
// Handle applications of types with call methods.
5836+
// Handle applications of types with `callAsFunction` methods.
58375837
if (desugar2->mayHaveMembers()) {
58385838
auto &ctx = getASTContext();
5839-
// Get all call methods of the nominal type.
5840-
// TODO: Consider caching?
5839+
// Get all `callAsFunction` methods of the nominal type.
5840+
// Note: Consider caching `callAsFunction` methods.
58415841
SmallVector<FuncDecl *, 4> callMethods;
5842-
auto candidates = lookupMember(desugar2, DeclName(ctx.Id_callFunction));
5842+
auto candidates = lookupMember(desugar2, DeclName(ctx.Id_callAsFunction));
58435843
for (auto entry : candidates) {
58445844
auto callMethod = dyn_cast<FuncDecl>(entry.getValueDecl());
58455845
if (!callMethod)
58465846
continue;
58475847
callMethods.push_back(callMethod);
58485848
}
58495849

5850-
// Handle call methods calls.
5850+
// Handle `callAsFunction` methods calls.
58515851
if (!callMethods.empty()) {
5852-
// Create a type variable for the call method.
5852+
// Create a type variable for the `callAsFunction` method.
58535853
auto loc = getConstraintLocator(locator);
58545854
auto tv = createTypeVariable(loc, TVO_CanBindToLValue);
58555855

5856-
// Record the call method overload set.
5856+
// Record the `callAsFunction` method overload set.
58575857
SmallVector<OverloadChoice, 4> choices;
58585858
for (auto candidate : callMethods) {
58595859
TC.validateDecl(candidate);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public protocol Layer {
2+
func callAsFunction(_ input: Float) -> Float
3+
}
4+
5+
public struct Dense {
6+
public init() {}
7+
8+
public func callAsFunction(_ input: Float) -> Float {
9+
return input * 2
10+
}
11+
}

test/Sema/Inputs/call_method_other_module.swift

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

test/Sema/call_method_cross_module.swift renamed to test/Sema/call_as_function_cross_module.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend -emit-module -primary-file %S/Inputs/call_method_other_module.swift -emit-module-path %t/call_method_other_module.swiftmodule
2+
// RUN: %target-swift-frontend -emit-module -primary-file %S/Inputs/call_as_function_other_module.swift -emit-module-path %t/call_as_function_other_module.swiftmodule
33
// RUN: %target-swift-frontend -typecheck -I %t -primary-file %s -verify
44

5-
import call_method_other_module
5+
import call_as_function_other_module
66

77
func testLayer<L: Layer>(_ layer: L) -> Float {
88
return layer(1)

test/Sema/call_method_generic.swift renamed to test/Sema/call_as_function_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 callFunction(x: Self)
4+
func callAsFunction(x: Self)
55
}
66

77
struct ConcreteType {
8-
func callFunction<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 callFunction<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.callFunction 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 callFunction<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 callFunction(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 renamed to test/Sema/call_as_function_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 'callFunction()' with type '() -> Missing'; do you want to add a stub?}}
5-
func callFunction() -> 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 callFunction() -> Self
15+
func callAsFunction() -> Self
1616
}
1717
extension P1 {
1818
// expected-note @+1 {{found this candidate}}
19-
func callFunction() -> 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 callFunction(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 callFunction() -> S0 { return self }
35+
func callAsFunction() -> S0 { return self }
3636
}
3737
let s0 = S0()
3838
s0()
3939

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

0 commit comments

Comments
 (0)