Skip to content
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
97 changes: 47 additions & 50 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4587,57 +4587,54 @@ ConstraintSystem::simplifyApplicableFnConstraint(

// SWIFT_ENABLE_TENSORFLOW
// Handle applications of types with `call` methods.
if (auto nominal = desugar2->getAnyNominal()) {
auto &ctx = getASTContext();
// Get all `call` methods of the nominal type.
SmallVector<CallDecl *, 4> callDecls;
auto candidates = TC.lookupMember(DC, nominal->getDeclaredTypeInContext(),
DeclName(ctx.getIdentifier("$call")));
for (auto entry : candidates) {
auto callDecl = dyn_cast<CallDecl>(entry.getValueDecl());
if (!callDecl)
continue;
callDecls.push_back(callDecl);
}

// Handle `call` methods calls.
if (!callDecls.empty()) {
// Create a type variable for the `call` method.
auto loc = getConstraintLocator(locator);
auto tv = createTypeVariable(loc, TVO_CanBindToLValue);

// Record the `call` method overload set.
SmallVector<OverloadChoice, 4> choices;
for (auto candidate : callDecls) {
TC.validateDecl(candidate);
if (candidate->isInvalid()) continue;
choices.push_back(
OverloadChoice(type2, candidate, FunctionRefKind::SingleApply));
}
if (choices.empty()) return SolutionKind::Error;
addOverloadSet(tv, choices, DC, loc);

// Create type variables for each parameter type.
SmallVector<AnyFunctionType::Param, 4> tvParams;
for (unsigned i : range(func1->getNumParams())) {
auto param = func1->getParams()[i];
auto paramType = param.getPlainType();

auto *tvParam = createTypeVariable(loc);
auto locatorBuilder =
locator.withPathElement(LocatorPathElt::getTupleElement(i));
addConstraint(ConstraintKind::ArgumentConversion, paramType,
tvParam, locatorBuilder);
tvParams.push_back(AnyFunctionType::Param(tvParam));
}
// Create target function type and an applicable function constraint.
AnyFunctionType *funcType =
FunctionType::get(tvParams, func1->getResult());
addConstraint(ConstraintKind::ApplicableFunction,
funcType, tv, locator);
auto &ctx = getASTContext();
// Get all `call` methods of the nominal type.
SmallVector<CallDecl *, 4> callDecls;
auto candidates = TC.lookupMember(
DC, desugar2, DeclName(ctx.getIdentifier("$call")));
for (auto entry : candidates) {
auto callDecl = dyn_cast<CallDecl>(entry.getValueDecl());
if (!callDecl)
continue;
callDecls.push_back(callDecl);
}

// Handle `call` methods calls.
if (!callDecls.empty()) {
// Create a type variable for the `call` method.
auto loc = getConstraintLocator(locator);
auto tv = createTypeVariable(loc, TVO_CanBindToLValue);

// Record the `call` method overload set.
SmallVector<OverloadChoice, 4> choices;
for (auto candidate : callDecls) {
TC.validateDecl(candidate);
if (candidate->isInvalid()) continue;
choices.push_back(
OverloadChoice(type2, candidate, FunctionRefKind::SingleApply));
}
if (choices.empty()) return SolutionKind::Error;
addOverloadSet(tv, choices, DC, loc);

// Create type variables for each parameter type.
SmallVector<AnyFunctionType::Param, 4> tvParams;
for (unsigned i : range(func1->getNumParams())) {
auto param = func1->getParams()[i];
auto paramType = param.getPlainType();

auto *tvParam = createTypeVariable(loc);
auto locatorBuilder =
locator.withPathElement(LocatorPathElt::getTupleElement(i));
addConstraint(ConstraintKind::ArgumentConversion, paramType,
tvParam, locatorBuilder);
tvParams.push_back(AnyFunctionType::Param(tvParam));
}
// Create target function type and an applicable function constraint.
AnyFunctionType *funcType =
FunctionType::get(tvParams, func1->getResult());
addConstraint(ConstraintKind::ApplicableFunction, funcType, tv, locator);

return SolutionKind::Solved;
}
return SolutionKind::Solved;
}

// Handle applications of @dynamicCallable types.
Expand Down
3 changes: 3 additions & 0 deletions test/decl/call/protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ protocol P0 {
func testProtocol(_ x: P0) {
_ = x()
}
func testGeneric<T : P0>(_ x: T) {
_ = x()
}

protocol P1 {
call func() -> Self
Expand Down