Skip to content

Commit 403a11f

Browse files
committed
Sema: Infer key path bindings before calling isViableForRanking()
This allows us to remove the key path check from isViableForRanking().
1 parent c0238db commit 403a11f

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,12 +1150,6 @@ std::optional<BindingSet> ConstraintSystem::determineBestBindings(
11501150
auto isViableForRanking = [this](const BindingSet &bindings) -> bool {
11511151
auto *typeVar = bindings.getTypeVariable();
11521152

1153-
// Key path root type variable is always viable because it can be
1154-
// transitively inferred from key path type during binding set
1155-
// finalization.
1156-
if (typeVar->getImpl().isKeyPathRoot())
1157-
return true;
1158-
11591153
// Type variable representing a base of unresolved member chain should
11601154
// always be considered viable for ranking since it's allow to infer
11611155
// types from transitive protocol requirements.
@@ -1181,6 +1175,11 @@ std::optional<BindingSet> ConstraintSystem::determineBestBindings(
11811175

11821176
auto &bindings = node.getBindingSet();
11831177

1178+
// Special handling for key paths.
1179+
bindings.inferTransitiveKeyPathBindings();
1180+
if (!bindings.finalizeKeyPathBindings())
1181+
continue;
1182+
11841183
// Before attempting to infer transitive bindings let's check
11851184
// whether there are any viable "direct" bindings associated with
11861185
// current type variable, if there are none - it means that this type
@@ -1193,14 +1192,13 @@ std::optional<BindingSet> ConstraintSystem::determineBestBindings(
11931192
// produce a default type.
11941193
bool isViable = isViableForRanking(bindings);
11951194

1196-
bindings.inferTransitiveKeyPathBindings();
11971195
bindings.inferTransitiveSupertypeBindings();
1198-
bindings.inferTransitiveUnresolvedMemberRefBindings();
1199-
1200-
if (!bindings.finalizeKeyPathBindings())
1201-
continue;
12021196

1197+
// Special handling for "leading-dot" unresolved member references,
1198+
// like .foo.
1199+
bindings.inferTransitiveUnresolvedMemberRefBindings();
12031200
bindings.finalizeUnresolvedMemberChainResult();
1201+
12041202
bindings.determineLiteralCoverage();
12051203

12061204
if (!bindings.hasViableBindings() && !bindings.isDirectHole())

unittests/Sema/BindingInferenceTests.cpp

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

128128
bindings.inferTransitiveKeyPathBindings();
129+
(void) bindings.finalizeKeyPathBindings();
130+
129131
bindings.inferTransitiveSupertypeBindings();
132+
130133
bindings.inferTransitiveUnresolvedMemberRefBindings();
131-
(void) bindings.finalizeKeyPathBindings();
132134
bindings.finalizeUnresolvedMemberChainResult();
135+
133136
bindings.determineLiteralCoverage();
134137

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

unittests/Sema/SemaFixture.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,13 @@ BindingSet SemaTest::inferBindings(ConstraintSystem &cs,
146146
bindings.inferTransitiveProtocolRequirements();
147147

148148
bindings.inferTransitiveKeyPathBindings();
149+
(void) bindings.finalizeKeyPathBindings();
150+
149151
bindings.inferTransitiveSupertypeBindings();
150-
bindings.inferTransitiveUnresolvedMemberRefBindings();
151152

152-
(void) bindings.finalizeKeyPathBindings();
153+
bindings.inferTransitiveUnresolvedMemberRefBindings();
153154
bindings.finalizeUnresolvedMemberChainResult();
155+
154156
bindings.determineLiteralCoverage();
155157
}
156158

0 commit comments

Comments
 (0)