Skip to content

Commit 9c04fd8

Browse files
committed
[CS] NFC: Factor out matchPackElementType
1 parent 29e9f42 commit 9c04fd8

File tree

2 files changed

+50
-40
lines changed

2 files changed

+50
-40
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5024,6 +5024,11 @@ class ConstraintSystem {
50245024
TypeMatchOptions flags,
50255025
ConstraintLocatorBuilder locator);
50265026

5027+
/// Attempt to match a pack element type with the fully resolved pattern type
5028+
/// for the pack expansion.
5029+
SolutionKind matchPackElementType(Type elementType, Type patternType,
5030+
ConstraintLocatorBuilder locator);
5031+
50275032
/// Attempt to simplify a PackElementOf constraint.
50285033
///
50295034
/// Solving this constraint is delayed until the element type is fully

lib/Sema/CSSimplify.cpp

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9626,6 +9626,49 @@ ConstraintSystem::simplifyBindTupleOfFunctionParamsConstraint(
96269626
return SolutionKind::Solved;
96279627
}
96289628

9629+
ConstraintSystem::SolutionKind
9630+
ConstraintSystem::matchPackElementType(Type elementType, Type patternType,
9631+
ConstraintLocatorBuilder locator) {
9632+
auto *loc = getConstraintLocator(locator);
9633+
auto shapeClass = patternType->getReducedShape();
9634+
auto *elementEnv = getPackElementEnvironment(loc, shapeClass);
9635+
9636+
// Without an opened element environment, we cannot derive the
9637+
// element binding.
9638+
if (!elementEnv) {
9639+
if (!shouldAttemptFixes())
9640+
return SolutionKind::Error;
9641+
9642+
// `each` was applied to a concrete type.
9643+
if (!shapeClass->is<PackArchetypeType>()) {
9644+
if (recordFix(AllowInvalidPackElement::create(*this, patternType, loc)))
9645+
return SolutionKind::Error;
9646+
} else {
9647+
auto envShape = PackExpansionEnvironments.find(loc);
9648+
if (envShape == PackExpansionEnvironments.end()) {
9649+
return SolutionKind::Error;
9650+
}
9651+
auto *fix = SkipSameShapeRequirement::create(
9652+
*this, envShape->second.second, shapeClass,
9653+
getConstraintLocator(loc, ConstraintLocator::PackShape));
9654+
if (recordFix(fix)) {
9655+
return SolutionKind::Error;
9656+
}
9657+
}
9658+
9659+
recordAnyTypeVarAsPotentialHole(elementType);
9660+
return SolutionKind::Solved;
9661+
}
9662+
9663+
auto expectedElementTy =
9664+
elementEnv->mapContextualPackTypeIntoElementContext(patternType);
9665+
assert(!expectedElementTy->is<PackType>());
9666+
9667+
addConstraint(ConstraintKind::Equal, elementType, expectedElementTy,
9668+
locator);
9669+
return SolutionKind::Solved;
9670+
}
9671+
96299672
ConstraintSystem::SolutionKind
96309673
ConstraintSystem::simplifyPackElementOfConstraint(Type first, Type second,
96319674
TypeMatchOptions flags,
@@ -9660,46 +9703,8 @@ ConstraintSystem::simplifyPackElementOfConstraint(Type first, Type second,
96609703
}
96619704

96629705
// Let's try to resolve element type based on the pattern type.
9663-
if (!patternType->hasTypeVariable()) {
9664-
auto *loc = getConstraintLocator(locator);
9665-
auto shapeClass = patternType->getReducedShape();
9666-
auto *elementEnv = getPackElementEnvironment(loc, shapeClass);
9667-
9668-
// Without an opened element environment, we cannot derive the
9669-
// element binding.
9670-
if (!elementEnv) {
9671-
if (!shouldAttemptFixes())
9672-
return SolutionKind::Error;
9673-
9674-
// `each` was applied to a concrete type.
9675-
if (!shapeClass->is<PackArchetypeType>()) {
9676-
if (recordFix(AllowInvalidPackElement::create(*this, patternType, loc)))
9677-
return SolutionKind::Error;
9678-
} else {
9679-
auto envShape = PackExpansionEnvironments.find(loc);
9680-
if (envShape == PackExpansionEnvironments.end()) {
9681-
return SolutionKind::Error;
9682-
}
9683-
auto *fix = SkipSameShapeRequirement::create(
9684-
*this, envShape->second.second, shapeClass,
9685-
getConstraintLocator(loc, ConstraintLocator::PackShape));
9686-
if (recordFix(fix)) {
9687-
return SolutionKind::Error;
9688-
}
9689-
}
9690-
9691-
recordAnyTypeVarAsPotentialHole(elementType);
9692-
return SolutionKind::Solved;
9693-
}
9694-
9695-
auto expectedElementTy =
9696-
elementEnv->mapContextualPackTypeIntoElementContext(patternType);
9697-
assert(!expectedElementTy->is<PackType>());
9698-
9699-
addConstraint(ConstraintKind::Equal, elementType, expectedElementTy,
9700-
locator);
9701-
return SolutionKind::Solved;
9702-
}
9706+
if (!patternType->hasTypeVariable())
9707+
return matchPackElementType(elementType, patternType, locator);
97039708

97049709
// Otherwise we are inferred or checking pattern type.
97059710

0 commit comments

Comments
 (0)