[ConstraintSystem] Refactor spots that create ApplicationFunction directly

This is required to make it easier to add "use dc" to the application.
This commit is contained in:
Pavel Yaskevich
2025-02-09 13:59:46 -08:00
parent f4101c43ab
commit d24f22ec54
7 changed files with 84 additions and 45 deletions

View File

@@ -8364,8 +8364,9 @@ ConstraintSystem::simplifyConstructionConstraint(
paramTypeVar, locator);
}
addConstraint(ConstraintKind::ApplicableFunction, fnType, memberType,
fnLocator);
addApplicationConstraint(fnType, memberType,
/*trailingClosureMatching=*/std::nullopt, useDC,
fnLocator);
return SolutionKind::Solved;
}
@@ -13108,6 +13109,7 @@ createImplicitRootForCallAsFunction(ConstraintSystem &cs, Type refType,
ConstraintSystem::SolutionKind ConstraintSystem::simplifyApplicableFnConstraint(
Type type1, Type type2,
std::optional<TrailingClosureMatching> trailingClosureMatching,
DeclContext *useDC,
TypeMatchOptions flags, ConstraintLocatorBuilder locator) {
auto &ctx = getASTContext();
@@ -13171,7 +13173,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyApplicableFnConstraint(
auto formUnsolved = [&](bool activate = false) {
if (flags.contains(TMF_GenerateConstraints)) {
auto *application = Constraint::createApplicableFunction(
*this, type1, type2, trailingClosureMatching,
*this, type1, type2, trailingClosureMatching, useDC,
getConstraintLocator(locator));
addUnsolvedConstraint(application);
@@ -13232,8 +13234,8 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyApplicableFnConstraint(
/*outerAlternatives*/ {}, memberLoc);
// Add new applicable function constraint based on the member type
// variable.
addConstraint(ConstraintKind::ApplicableFunction, func1, memberTy,
locator);
addApplicationConstraint(func1, memberTy, trailingClosureMatching, useDC,
locator);
return SolutionKind::Solved;
}
@@ -13348,9 +13350,9 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyApplicableFnConstraint(
// Form an unsolved constraint to apply trailing closures to a
// callable type produced by `.init`. This constraint would become
// active when `callableType` is bound.
addUnsolvedConstraint(Constraint::create(
*this, ConstraintKind::ApplicableFunction, callAsFunctionArguments,
callableType,
addUnsolvedConstraint(Constraint::createApplicableFunction(
*this, callAsFunctionArguments, callableType,
trailingClosureMatching, useDC,
getConstraintLocator(implicitRef,
ConstraintLocator::ApplyFunction)));
break;
@@ -13368,12 +13370,13 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyApplicableFnConstraint(
auto applyLocator = getConstraintLocator(locator);
auto forwardConstraint = Constraint::createApplicableFunction(
*this, type1, type2, TrailingClosureMatching::Forward, applyLocator);
auto backwardConstraint = Constraint::createApplicableFunction(
*this, type1, type2, TrailingClosureMatching::Backward,
*this, type1, type2, TrailingClosureMatching::Forward, useDC,
applyLocator);
addDisjunctionConstraint(
{ forwardConstraint, backwardConstraint}, applyLocator);
auto backwardConstraint = Constraint::createApplicableFunction(
*this, type1, type2, TrailingClosureMatching::Backward, useDC,
applyLocator);
addDisjunctionConstraint({forwardConstraint, backwardConstraint},
applyLocator);
break;
}
@@ -13450,7 +13453,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyApplicableFnConstraint(
// Construct the instance from the input arguments.
auto simplified = simplifyConstructionConstraint(
instance2, func1, subflags,
/*FIXME?*/ DC, FunctionRefInfo::singleBaseNameApply(),
useDC, FunctionRefInfo::singleBaseNameApply(),
getConstraintLocator(outerLocator));
// Record any fixes we attempted to get to the correct solution.
@@ -15673,15 +15676,6 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
case ConstraintKind::BridgingConversion:
return simplifyBridgingConstraint(first, second, subflags, locator);
case ConstraintKind::ApplicableFunction: {
// First try to simplify the overload set for the function being applied.
if (simplifyAppliedOverloads(second, first->castTo<FunctionType>(),
locator)) {
return SolutionKind::Error;
}
return simplifyApplicableFnConstraint(first, second, std::nullopt, subflags,
locator);
}
case ConstraintKind::DynamicCallableApplicableFunction:
return simplifyDynamicCallableApplicableFnConstraint(first, second,
subflags, locator);
@@ -15762,6 +15756,7 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
case ConstraintKind::KeyPathApplication:
case ConstraintKind::FallbackType:
case ConstraintKind::SyntacticElement:
case ConstraintKind::ApplicableFunction:
llvm_unreachable("Use the correct addConstraint()");
}
@@ -15990,6 +15985,41 @@ void ConstraintSystem::addConstraint(ConstraintKind kind, Type first,
}
}
void ConstraintSystem::addApplicationConstraint(
FunctionType *appliedFn, Type calleeType,
std::optional<TrailingClosureMatching> trailingClosureMatching,
DeclContext *useDC,
ConstraintLocatorBuilder locator) {
auto recordFailure = [&]() {
if (shouldRecordFailedConstraint()) {
auto *c = Constraint::createApplicableFunction(
*this, appliedFn, calleeType, trailingClosureMatching, useDC,
getConstraintLocator(locator));
recordFailedConstraint(c);
}
};
// First try to simplify the overload set for the function being applied.
if (simplifyAppliedOverloads(calleeType, appliedFn, locator)) {
recordFailure();
return;
}
switch (simplifyApplicableFnConstraint(appliedFn, calleeType,
trailingClosureMatching, useDC,
TMF_GenerateConstraints, locator)) {
case SolutionKind::Error:
recordFailure();
break;
case SolutionKind::Unsolved:
llvm_unreachable("should have generated constraints");
case SolutionKind::Solved:
return;
}
}
void ConstraintSystem::addContextualConversionConstraint(
Expr *expr, Type conversionType, ContextualTypePurpose purpose,
ConstraintLocator *locator) {
@@ -16175,7 +16205,8 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
case ConstraintKind::ApplicableFunction:
return simplifyApplicableFnConstraint(
constraint.getFirstType(), constraint.getSecondType(),
constraint.getTrailingClosureMatching(), std::nullopt,
constraint.getTrailingClosureMatching(),
/*FIXME*/DC, /*flags=*/std::nullopt,
constraint.getLocator());
case ConstraintKind::DynamicCallableApplicableFunction: