Skip to content

Commit 6afc307

Browse files
committed
[CSBindings] Prevent determineBestBindings from selecting unresolved key path type
1 parent f08f86c commit 6afc307

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,10 @@ class BindingSet {
576576

577577
/// Finalize binding computation for this type variable by
578578
/// inferring bindings from context e.g. transitive bindings.
579-
void finalize(
579+
///
580+
/// \returns true if finalization successful (which makes binding set viable),
581+
/// and false otherwise.
582+
bool finalize(
580583
llvm::SmallDenseMap<TypeVariableType *, BindingSet> &inferredBindings);
581584

582585
static BindingScore formBindingScore(const BindingSet &b);

lib/Sema/CSBindings.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ static Type getKeyPathType(ASTContext &ctx, KeyPathCapability capability,
600600
return keyPathTy;
601601
}
602602

603-
void BindingSet::finalize(
603+
bool BindingSet::finalize(
604604
llvm::SmallDenseMap<TypeVariableType *, BindingSet> &inferredBindings) {
605605
inferTransitiveBindings(inferredBindings);
606606

@@ -654,9 +654,10 @@ void BindingSet::finalize(
654654

655655
std::tie(isValid, capability) = CS.inferKeyPathLiteralCapability(TypeVar);
656656

657-
// Key path literal is not yet sufficiently resolved.
657+
// Key path literal is not yet sufficiently resolved, this binding
658+
// set is not viable.
658659
if (isValid && !capability)
659-
return;
660+
return false;
660661

661662
// If the key path is sufficiently resolved we can add inferred binding
662663
// to the set.
@@ -728,7 +729,7 @@ void BindingSet::finalize(
728729
Bindings = std::move(updatedBindings);
729730
Defaults.clear();
730731

731-
return;
732+
return true;
732733
}
733734

734735
if (CS.shouldAttemptFixes() &&
@@ -752,6 +753,8 @@ void BindingSet::finalize(
752753
});
753754
}
754755
}
756+
757+
return true;
755758
}
756759
}
757760

@@ -1030,7 +1033,8 @@ llvm::Optional<BindingSet> ConstraintSystem::determineBestBindings(
10301033
// produce a default type.
10311034
bool isViable = isViableForRanking(bindings);
10321035

1033-
bindings.finalize(cache);
1036+
if (!bindings.finalize(cache))
1037+
continue;
10341038

10351039
if (!bindings || !isViable)
10361040
continue;

lib/Sema/CSSimplify.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12207,13 +12207,6 @@ ConstraintSystem::simplifyKeyPathConstraint(
1220712207
assert(contextualTy);
1220812208
}
1220912209

12210-
// If there are no other options the solver might end up picking
12211-
// `AnyKeyPath` or `PartialKeyPath` based on a contextual conversion.
12212-
// This is an error during normal type-checking but okay in
12213-
// diagnostic mode because root and value are allowed to be holes.
12214-
if (contextualTy->isAnyKeyPath() || contextualTy->isPartialKeyPath())
12215-
return shouldAttemptFixes();
12216-
1221712210
if (auto bgt = contextualTy->getAs<BoundGenericType>()) {
1221812211
// We can get root and value from a concrete key path type.
1221912212
assert(bgt->isKeyPath() || bgt->isWritableKeyPath() ||

0 commit comments

Comments
 (0)