Skip to content

Commit 55cd99b

Browse files
committed
[CSBindings] NFC: Adjust static member lookup feature to recent PotentialBindings changes
- `ConstraintSystem` is now referenced as a member of `PotentialBindings`; - Literals and defaults are no longer added to the `Bindings` list, so we to add a new method `hasViableBindings` to make sure that protocol types are added only when there are no other bindings.
1 parent e8f7a71 commit 55cd99b

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,20 @@ struct PotentialBindings {
276276

277277
/// Determine whether the set of bindings is non-empty.
278278
explicit operator bool() const {
279+
return hasViableBindings()|| isDirectHole();
280+
}
281+
282+
/// Determine whether this set has any "viable" (or non-hole) bindings.
283+
///
284+
/// A viable binding could be - a direct or transitive binding
285+
/// inferred from a constraint, literal binding, or defaltable
286+
/// binding.
287+
///
288+
/// A hole is not considered a viable binding since it doesn't
289+
/// add any new type information to constraint system.
290+
bool hasViableBindings() const {
279291
return !Bindings.empty() || getNumViableLiteralBindings() > 0 ||
280-
!Defaults.empty() || isDirectHole();
292+
!Defaults.empty();
281293
}
282294

283295
/// Determines whether this type variable could be `nil`,

lib/Sema/CSBindings.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ void PotentialBindings::finalize(
448448
// func foo<T: P>(_: T) {}
449449
// foo(.bar) <- `.bar` should be a static member of `P`.
450450
// \endcode
451-
if (Bindings.empty() && TransitiveProtocols.hasValue()) {
451+
if (!hasViableBindings() && TransitiveProtocols.hasValue()) {
452452
for (auto *constraint : *TransitiveProtocols) {
453453
auto protocolTy = constraint->getSecondType();
454454
addPotentialBinding(
@@ -457,29 +457,25 @@ void PotentialBindings::finalize(
457457
}
458458
}
459459

460-
if (cs.shouldAttemptFixes() &&
460+
if (CS.shouldAttemptFixes() &&
461461
locator->isLastElement<LocatorPathElt::UnresolvedMemberChainResult>()) {
462-
// Let's see whether this chain is valid, is it isn't then to avoid
462+
// Let's see whether this chain is valid, if it isn't then to avoid
463463
// diagnosing the same issue multiple different ways, let's infer
464464
// result of the chain to be a hole.
465465
auto *resultExpr =
466466
castToExpr<UnresolvedMemberChainResultExpr>(locator->getAnchor());
467-
auto *baseLocator = cs.getConstraintLocator(
467+
auto *baseLocator = CS.getConstraintLocator(
468468
resultExpr->getChainBase(), ConstraintLocator::UnresolvedMember);
469469

470-
if (cs.hasFixFor(
470+
if (CS.hasFixFor(
471471
baseLocator,
472472
FixKind::AllowInvalidStaticMemberRefOnProtocolMetatype)) {
473-
cs.recordPotentialHole(TypeVar);
474-
// Clear all of the previously inferred bindings which are inferred
473+
CS.recordPotentialHole(TypeVar);
474+
// Clear all of the previously collected bindings which are inferred
475475
// from inside of a member chain.
476-
Bindings.erase(
477-
llvm::remove_if(
478-
Bindings,
479-
[](const ConstraintSystem::PotentialBinding &binding) {
480-
return binding.Kind == AllowedBindingKind::Supertypes;
481-
}),
482-
Bindings.end());
476+
Bindings.remove_if([](const PotentialBinding &binding) {
477+
return binding.Kind == AllowedBindingKind::Supertypes;
478+
});
483479
}
484480
}
485481
}

0 commit comments

Comments
 (0)