Skip to content

Commit 45d03e1

Browse files
committed
Sema: Split up BindingSet::finalize() into finalizeKeyPathBindings() and finalizeUnresolvedMemberChain()
1 parent 02129f2 commit 45d03e1

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,12 +560,14 @@ class BindingSet {
560560
/// literal protocols associated with this type variable.
561561
void determineLiteralCoverage();
562562

563-
/// Finalize binding computation for this type variable by
564-
/// inferring bindings from context e.g. transitive bindings.
563+
/// Finalize binding computation for key path type variables.
565564
///
566565
/// \returns true if finalization successful (which makes binding set viable),
567566
/// and false otherwise.
568-
bool finalize();
567+
bool finalizeKeyPathBindings();
568+
569+
/// Handle diagnostics of unresolved member chains.
570+
void finalizeUnresolvedMemberChainResult();
569571

570572
static BindingScore formBindingScore(const BindingSet &b);
571573

lib/Sema/CSBindings.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -699,13 +699,11 @@ static Type getKeyPathType(ASTContext &ctx, KeyPathCapability capability,
699699
return keyPathTy;
700700
}
701701

702-
bool BindingSet::finalize() {
702+
bool BindingSet::finalizeKeyPathBindings() {
703703
if (auto *locator = TypeVar->getImpl().getLocator()) {
704704
if (TypeVar->getImpl().isKeyPathType()) {
705705
auto &ctx = CS.getASTContext();
706-
707-
auto *keyPathLoc = TypeVar->getImpl().getLocator();
708-
auto *keyPath = castToExpr<KeyPathExpr>(keyPathLoc->getAnchor());
706+
auto *keyPath = castToExpr<KeyPathExpr>(locator->getAnchor());
709707

710708
bool isValid;
711709
std::optional<KeyPathCapability> capability;
@@ -772,7 +770,7 @@ bool BindingSet::finalize() {
772770
auto keyPathTy = getKeyPathType(ctx, *capability, rootTy,
773771
CS.getKeyPathValueType(keyPath));
774772
updatedBindings.insert(
775-
{keyPathTy, AllowedBindingKind::Exact, keyPathLoc});
773+
{keyPathTy, AllowedBindingKind::Exact, locator});
776774
} else if (CS.shouldAttemptFixes()) {
777775
auto fixedRootTy = CS.getFixedType(rootTy);
778776
// If key path is structurally correct and has a resolved root
@@ -799,10 +797,14 @@ bool BindingSet::finalize() {
799797

800798
Bindings = std::move(updatedBindings);
801799
Defaults.clear();
802-
803-
return true;
804800
}
801+
}
805802

803+
return true;
804+
}
805+
806+
void BindingSet::finalizeUnresolvedMemberChainResult() {
807+
if (auto *locator = TypeVar->getImpl().getLocator()) {
806808
if (CS.shouldAttemptFixes() &&
807809
locator->isLastElement<LocatorPathElt::UnresolvedMemberChainResult>()) {
808810
// Let's see whether this chain is valid, if it isn't then to avoid
@@ -825,8 +827,6 @@ bool BindingSet::finalize() {
825827
}
826828
}
827829
}
828-
829-
return true;
830830
}
831831

832832
void BindingSet::addBinding(PotentialBinding binding, bool isTransitive) {
@@ -1194,9 +1194,10 @@ std::optional<BindingSet> ConstraintSystem::determineBestBindings(
11941194

11951195
bindings.inferTransitiveBindings();
11961196

1197-
if (!bindings.finalize())
1197+
if (!bindings.finalizeKeyPathBindings())
11981198
continue;
11991199

1200+
bindings.finalizeUnresolvedMemberChainResult();
12001201
bindings.determineLiteralCoverage();
12011202

12021203
if (!bindings.hasViableBindings() && !bindings.isDirectHole())
@@ -1595,7 +1596,9 @@ BindingSet ConstraintSystem::getBindingsFor(TypeVariableType *typeVar) {
15951596
assert(!typeVar->getImpl().getFixedType(nullptr) && "has a fixed type");
15961597

15971598
BindingSet bindings(*this, typeVar, CG[typeVar].getPotentialBindings());
1598-
bindings.finalize();
1599+
1600+
(void) bindings.finalizeKeyPathBindings();
1601+
bindings.finalizeUnresolvedMemberChainResult();
15991602
bindings.determineLiteralCoverage();
16001603

16011604
return bindings;

unittests/Sema/BindingInferenceTests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ TEST_F(SemaTest, TestIntLiteralBindingInference) {
126126
cs.getConstraintGraph()[floatLiteralTy].initBindingSet();
127127

128128
bindings.inferTransitiveBindings();
129-
bindings.finalize();
129+
(void) bindings.finalizeKeyPathBindings();
130+
bindings.finalizeUnresolvedMemberChainResult();
130131
bindings.determineLiteralCoverage();
131132

132133
// Inferred a single transitive binding through `$T_float`.

unittests/Sema/SemaFixture.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,14 @@ BindingSet SemaTest::inferBindings(ConstraintSystem &cs,
140140
continue;
141141

142142
auto &bindings = node.getBindingSet();
143+
144+
// FIXME: This is also called in inferTransitiveBindings(), why do we need
145+
// to call it again?
143146
bindings.inferTransitiveProtocolRequirements();
147+
144148
bindings.inferTransitiveBindings();
145-
bindings.finalize();
149+
(void) bindings.finalizeKeyPathBindings();
150+
bindings.finalizeUnresolvedMemberChainResult();
146151
bindings.determineLiteralCoverage();
147152
}
148153

0 commit comments

Comments
 (0)