Skip to content

Commit e06ccad

Browse files
committed
[CS] Factor out more logic into getOpenedFunctionBuilderTypeFor
Factor out the callee locator computation and function builder opening logic.
1 parent 71f6797 commit e06ccad

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

lib/Sema/BuilderTransform.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,22 +1635,6 @@ ConstraintSystem::matchFunctionBuilder(
16351635
}
16361636
}
16371637

1638-
// If the builder type has a type parameter, substitute in the type
1639-
// variables.
1640-
if (builderType->hasTypeParameter()) {
1641-
// Find the opened type for this callee and substitute in the type
1642-
// parametes.
1643-
for (const auto &opened : OpenedTypes) {
1644-
if (opened.first == calleeLocator) {
1645-
OpenedTypeMap replacements(opened.second.begin(),
1646-
opened.second.end());
1647-
builderType = openType(builderType, replacements);
1648-
break;
1649-
}
1650-
}
1651-
assert(!builderType->hasTypeParameter());
1652-
}
1653-
16541638
BuilderClosureVisitor visitor(getASTContext(), this, dc, builderType,
16551639
bodyResultType);
16561640

lib/Sema/CSSimplify.cpp

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7274,8 +7274,17 @@ ConstraintSystem::simplifyOneWayConstraint(
72747274
secondSimplified, first, ConstraintKind::BindParam, flags, locator);
72757275
}
72767276

7277-
static Type getFunctionBuilderTypeFor(ConstraintSystem &cs, unsigned paramIdx,
7278-
ConstraintLocator *calleeLocator) {
7277+
static Type getOpenedFunctionBuilderTypeFor(ConstraintSystem &cs,
7278+
ConstraintLocatorBuilder locator) {
7279+
auto lastElt = locator.last();
7280+
if (!lastElt)
7281+
return Type();
7282+
7283+
auto argToParamElt = lastElt->getAs<LocatorPathElt::ApplyArgToParam>();
7284+
if (!argToParamElt)
7285+
return Type();
7286+
7287+
auto *calleeLocator = cs.getCalleeLocator(cs.getConstraintLocator(locator));
72797288
auto selectedOverload = cs.findSelectedOverloadFor(calleeLocator);
72807289
if (!(selectedOverload &&
72817290
selectedOverload->choice.getKind() == OverloadChoiceKind::Decl))
@@ -7290,8 +7299,27 @@ static Type getFunctionBuilderTypeFor(ConstraintSystem &cs, unsigned paramIdx,
72907299
if (!choice->hasParameterList())
72917300
return Type();
72927301

7293-
auto *PD = getParameterAt(choice, paramIdx);
7294-
return PD->getFunctionBuilderType();
7302+
auto *PD = getParameterAt(choice, argToParamElt->getParamIdx());
7303+
auto builderType = PD->getFunctionBuilderType();
7304+
if (!builderType)
7305+
return Type();
7306+
7307+
// If the builder type has a type parameter, substitute in the type
7308+
// variables.
7309+
if (builderType->hasTypeParameter()) {
7310+
// Find the opened type for this callee and substitute in the type
7311+
// parametes.
7312+
// FIXME: We should consider changing OpenedTypes to a MapVector.
7313+
for (const auto &opened : cs.getOpenedTypes()) {
7314+
if (opened.first == calleeLocator) {
7315+
OpenedTypeMap replacements(opened.second.begin(), opened.second.end());
7316+
builderType = cs.openType(builderType, replacements);
7317+
break;
7318+
}
7319+
}
7320+
assert(!builderType->hasTypeParameter());
7321+
}
7322+
return builderType;
72957323
}
72967324

72977325
bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
@@ -7310,15 +7338,7 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
73107338
auto *inferredClosureType = getClosureType(closure);
73117339

73127340
// Determine whether a function builder will be applied.
7313-
Type functionBuilderType;
7314-
ConstraintLocator *calleeLocator = nullptr;
7315-
if (auto last = locator.last()) {
7316-
if (auto argToParam = last->getAs<LocatorPathElt::ApplyArgToParam>()) {
7317-
calleeLocator = getCalleeLocator(getConstraintLocator(locator));
7318-
functionBuilderType = getFunctionBuilderTypeFor(
7319-
*this, argToParam->getParamIdx(), calleeLocator);
7320-
}
7321-
}
7341+
auto functionBuilderType = getOpenedFunctionBuilderTypeFor(*this, locator);
73227342

73237343
// Determine whether to introduce one-way constraints between the parameter's
73247344
// type as seen in the body of the closure and the external parameter
@@ -7392,6 +7412,7 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
73927412

73937413
// If there is a function builder to apply, do so now.
73947414
if (functionBuilderType) {
7415+
auto *calleeLocator = getCalleeLocator(getConstraintLocator(locator));
73957416
if (auto result = matchFunctionBuilder(
73967417
closure, functionBuilderType, closureType->getResult(),
73977418
ConstraintKind::Conversion, calleeLocator, locator)) {

0 commit comments

Comments
 (0)