Skip to content

Commit f978c30

Browse files
committed
[@dynamicCallable] Minor clean up.
- Use known identifiers `Id_dynamicallyCall`, etc. in CSSimplify. - Update doc comment in CSDiag.
1 parent 3625710 commit f978c30

File tree

2 files changed

+40
-41
lines changed

2 files changed

+40
-41
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5717,7 +5717,8 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
57175717
}
57185718

57195719
// If we resolved a concrete expression for the callee, and it has
5720-
// non-function/non-metatype type, then we cannot call it!
5720+
// non-function/non-metatype/non-@dynamicCallable type, then we cannot call
5721+
// it!
57215722
if (!isUnresolvedOrTypeVarType(fnType) &&
57225723
!fnType->is<AnyFunctionType>() && !fnType->is<MetatypeType>()
57235724
&& !CS.DynamicCallableCache[fnType->getCanonicalType()].isValid()) {

lib/Sema/CSSimplify.cpp

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,40 +2942,6 @@ getArgumentLabels(ConstraintSystem &cs, ConstraintLocatorBuilder locator) {
29422942
return known->second;
29432943
}
29442944

2945-
/// Returns the function declaration corresponding to a @dynamicCallable
2946-
/// attribute required method (if it exists) implemented by a type. Otherwise,
2947-
/// return nullptr.
2948-
static FuncDecl *
2949-
lookupDynamicCallableMethod(Type type, ConstraintSystem &CS,
2950-
const ConstraintLocatorBuilder &locator,
2951-
StringRef methodName, StringRef argumentName,
2952-
bool hasKeywordArgs, bool &error) {
2953-
auto &ctx = CS.getASTContext();
2954-
auto decl = type->getAnyNominal();
2955-
auto option = DeclName(ctx, DeclBaseName(ctx.getIdentifier(methodName)),
2956-
{ ctx.getIdentifier(argumentName) });
2957-
auto matches = CS.performMemberLookup(ConstraintKind::ValueMember,
2958-
option, type,
2959-
FunctionRefKind::SingleApply,
2960-
CS.getConstraintLocator(locator),
2961-
/*includeInaccessibleMembers*/ false);
2962-
// Filter valid candidates.
2963-
auto candidates = matches.ViableCandidates;
2964-
auto filter = [&](OverloadChoice choice) {
2965-
auto cand = cast<FuncDecl>(choice.getDecl());
2966-
return !isValidDynamicCallableMethod(cand, decl, CS.TC, hasKeywordArgs);
2967-
};
2968-
candidates.erase(std::remove_if(candidates.begin(), candidates.end(), filter),
2969-
candidates.end());
2970-
2971-
// If there is one candidate, return it. Otherwise, return nullptr.
2972-
auto size = candidates.size();
2973-
if (size == 1) return cast<FuncDecl>(candidates.front().getDecl());
2974-
// If there are >1 candidates, it is an overload error.
2975-
else if (size > 1) error = true;
2976-
return nullptr;
2977-
}
2978-
29792945
/// Return true if the specified type or a super-class/super-protocol has the
29802946
/// @dynamicMemberLookup attribute on it. This implementation is not
29812947
/// particularly fast in the face of deep class hierarchies or lots of protocol
@@ -4287,22 +4253,54 @@ ConstraintSystem::simplifyKeyPathApplicationConstraint(
42874253
return unsolved();
42884254
}
42894255

4256+
/// Returns the function declaration corresponding to a @dynamicCallable
4257+
/// attribute required method (if it exists) implemented by a type. Otherwise,
4258+
/// return nullptr.
4259+
static FuncDecl *
4260+
lookupDynamicCallableMethod(Type type, ConstraintSystem &CS,
4261+
const ConstraintLocatorBuilder &locator,
4262+
Identifier argumentName, bool hasKeywordArgs,
4263+
bool &error) {
4264+
auto &ctx = CS.getASTContext();
4265+
auto decl = type->getAnyNominal();
4266+
auto methodName = DeclName(ctx, ctx.Id_dynamicallyCall, { argumentName });
4267+
auto matches = CS.performMemberLookup(ConstraintKind::ValueMember,
4268+
methodName, type,
4269+
FunctionRefKind::SingleApply,
4270+
CS.getConstraintLocator(locator),
4271+
/*includeInaccessibleMembers*/ false);
4272+
// Filter valid candidates.
4273+
auto candidates = matches.ViableCandidates;
4274+
auto filter = [&](OverloadChoice choice) {
4275+
auto cand = cast<FuncDecl>(choice.getDecl());
4276+
return !isValidDynamicCallableMethod(cand, decl, CS.TC, hasKeywordArgs);
4277+
};
4278+
candidates.erase(std::remove_if(candidates.begin(), candidates.end(), filter),
4279+
candidates.end());
4280+
4281+
// If there is one candidate, return it. Otherwise, return nullptr.
4282+
auto size = candidates.size();
4283+
if (size == 1) return cast<FuncDecl>(candidates.front().getDecl());
4284+
// If there are >1 candidates, it is an overload error.
4285+
else if (size > 1) error = true;
4286+
return nullptr;
4287+
}
4288+
42904289
/// Looks up and returns the @dynamicCallable required methods (if they exist)
42914290
/// implemented by a type. This function should not be called directly: instead,
42924291
/// call `getDynamicCallableMethods` which performs caching.
42934292
static DynamicCallableMethods
42944293
lookupDynamicCallableMethods(Type type, ConstraintSystem &CS,
42954294
const ConstraintLocatorBuilder &locator,
42964295
bool &error) {
4296+
auto &ctx = CS.getASTContext();
42974297
DynamicCallableMethods methods;
42984298
methods.argumentsMethod =
4299-
lookupDynamicCallableMethod(type, CS, locator, "dynamicallyCall",
4300-
"withArguments", /*hasKeywordArgs*/ false,
4301-
error);
4299+
lookupDynamicCallableMethod(type, CS, locator, ctx.Id_withArguments,
4300+
/*hasKeywordArgs*/ false, error);
43024301
methods.keywordArgumentsMethod =
4303-
lookupDynamicCallableMethod(type, CS, locator, "dynamicallyCall",
4304-
"withKeywordArguments", /*hasKeywordArgs*/ true,
4305-
error);
4302+
lookupDynamicCallableMethod(type, CS, locator, ctx.Id_withKeywordArguments,
4303+
/*hasKeywordArgs*/ true, error);
43064304
return methods;
43074305
}
43084306

0 commit comments

Comments
 (0)