Skip to content

Commit 23297c9

Browse files
committed
[ConstraintSystem] NFC: Extract opening of individual requirements into a method
It's a convenient way to use existing logic for default argument inference because suhc inference cannot open whole signature but only conformance and layout constraints associated generic parameters used in a particular parameter position.
1 parent 6c8a05d commit 23297c9

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4176,6 +4176,14 @@ class ConstraintSystem {
41764176
ConstraintLocatorBuilder locator,
41774177
llvm::function_ref<Type(Type)> subst);
41784178

4179+
// Record the given requirement in the constraint system.
4180+
void openGenericRequirement(DeclContext *outerDC,
4181+
unsigned index,
4182+
const Requirement &requirement,
4183+
bool skipProtocolSelfConstraint,
4184+
ConstraintLocatorBuilder locator,
4185+
llvm::function_ref<Type(Type)> subst);
4186+
41794187
/// Record the set of opened types for the given locator.
41804188
void recordOpenedTypes(
41814189
ConstraintLocatorBuilder locator,

lib/Sema/ConstraintSystem.cpp

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,38 +1615,46 @@ void ConstraintSystem::openGenericRequirements(
16151615
llvm::function_ref<Type(Type)> substFn) {
16161616
auto requirements = signature.getRequirements();
16171617
for (unsigned pos = 0, n = requirements.size(); pos != n; ++pos) {
1618-
const auto &req = requirements[pos];
1619-
1620-
Optional<Requirement> openedReq;
1621-
auto openedFirst = substFn(req.getFirstType());
1622-
1623-
auto kind = req.getKind();
1624-
switch (kind) {
1625-
case RequirementKind::Conformance: {
1626-
auto protoDecl = req.getProtocolDecl();
1627-
// Determine whether this is the protocol 'Self' constraint we should
1628-
// skip.
1629-
if (skipProtocolSelfConstraint && protoDecl == outerDC &&
1630-
protoDecl->getSelfInterfaceType()->isEqual(req.getFirstType()))
1631-
continue;
1632-
openedReq = Requirement(kind, openedFirst, req.getSecondType());
1633-
break;
1634-
}
1635-
case RequirementKind::Superclass:
1636-
case RequirementKind::SameType:
1637-
openedReq = Requirement(kind, openedFirst, substFn(req.getSecondType()));
1638-
break;
1639-
case RequirementKind::Layout:
1640-
openedReq = Requirement(kind, openedFirst, req.getLayoutConstraint());
1641-
break;
1642-
}
1643-
16441618
auto openedGenericLoc =
1645-
locator.withPathElement(LocatorPathElt::OpenedGeneric(signature));
1646-
addConstraint(*openedReq,
1647-
openedGenericLoc.withPathElement(
1648-
LocatorPathElt::TypeParameterRequirement(pos, kind)));
1619+
locator.withPathElement(LocatorPathElt::OpenedGeneric(signature));
1620+
openGenericRequirement(outerDC, pos, requirements[pos],
1621+
skipProtocolSelfConstraint, openedGenericLoc,
1622+
substFn);
1623+
}
1624+
}
1625+
1626+
void ConstraintSystem::openGenericRequirement(
1627+
DeclContext *outerDC, unsigned index, const Requirement &req,
1628+
bool skipProtocolSelfConstraint, ConstraintLocatorBuilder locator,
1629+
llvm::function_ref<Type(Type)> substFn) {
1630+
Optional<Requirement> openedReq;
1631+
auto openedFirst = substFn(req.getFirstType());
1632+
1633+
auto kind = req.getKind();
1634+
switch (kind) {
1635+
case RequirementKind::Conformance: {
1636+
auto protoDecl = req.getProtocolDecl();
1637+
// Determine whether this is the protocol 'Self' constraint we should
1638+
// skip.
1639+
if (skipProtocolSelfConstraint && protoDecl == outerDC &&
1640+
protoDecl->getSelfInterfaceType()->isEqual(req.getFirstType()))
1641+
return;
1642+
1643+
openedReq = Requirement(kind, openedFirst, req.getSecondType());
1644+
break;
16491645
}
1646+
case RequirementKind::Superclass:
1647+
case RequirementKind::SameType:
1648+
openedReq = Requirement(kind, openedFirst, substFn(req.getSecondType()));
1649+
break;
1650+
case RequirementKind::Layout:
1651+
openedReq = Requirement(kind, openedFirst, req.getLayoutConstraint());
1652+
break;
1653+
}
1654+
1655+
addConstraint(*openedReq,
1656+
locator.withPathElement(
1657+
LocatorPathElt::TypeParameterRequirement(index, kind)));
16501658
}
16511659

16521660
/// Add the constraint on the type used for the 'Self' type for a member

0 commit comments

Comments
 (0)