Skip to content

Commit 49a852b

Browse files
author
Amritpan Kaur
committed
[CSBindings] Refactor getting KnownProtocolKind into new function for reuse when printing LiteralBindingKind::Collection type.
1 parent b43086b commit 49a852b

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,11 @@ class BindingSet {
470470
return Info.AssociatedCodeCompletionToken;
471471
}
472472

473-
LiteralBindingKind getLiteralKind() const;
473+
void forEachLiteralRequirement(
474+
llvm::function_ref<void(KnownProtocolKind)> callback) const;
475+
476+
/// Return a literal requirement that has the most impact on the binding score.
477+
LiteralBindingKind getLiteralForScore() const;
474478

475479
/// Check if this binding is favored over a disjunction e.g.
476480
/// if it has only concrete types or would resolve a closure.

lib/Sema/CSBindings.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ BindingSet::BindingScore BindingSet::formBindingScore(const BindingSet &b) {
731731
return std::make_tuple(b.isHole(), numNonDefaultableBindings == 0,
732732
b.isDelayed(), b.isSubtypeOfExistentialType(),
733733
b.involvesTypeVariables(),
734-
static_cast<unsigned char>(b.getLiteralKind()),
734+
static_cast<unsigned char>(b.getLiteralForScore()),
735735
-numNonDefaultableBindings);
736736
}
737737

@@ -1567,18 +1567,26 @@ void PotentialBindings::retract(Constraint *constraint) {
15671567
EquivalentTo.remove_if(hasMatchingSource);
15681568
}
15691569

1570-
LiteralBindingKind BindingSet::getLiteralKind() const {
1571-
LiteralBindingKind kind = LiteralBindingKind::None;
1572-
1570+
void BindingSet::forEachLiteralRequirement(
1571+
llvm::function_ref<void(KnownProtocolKind)> callback) const {
15731572
for (const auto &literal : Literals) {
15741573
auto *protocol = literal.first;
15751574
const auto &info = literal.second;
15761575

15771576
// Only uncovered defaultable literal protocols participate.
15781577
if (!info.viableAsBinding())
15791578
continue;
1579+
1580+
if (auto protocolKind = protocol->getKnownProtocolKind())
1581+
callback(*protocolKind);
1582+
}
1583+
}
15801584

1581-
switch (*protocol->getKnownProtocolKind()) {
1585+
LiteralBindingKind BindingSet::getLiteralForScore() const {
1586+
LiteralBindingKind kind = LiteralBindingKind::None;
1587+
1588+
forEachLiteralRequirement([&](KnownProtocolKind protocolKind) {
1589+
switch (protocolKind) {
15821590
case KnownProtocolKind::ExpressibleByDictionaryLiteral:
15831591
case KnownProtocolKind::ExpressibleByArrayLiteral:
15841592
case KnownProtocolKind::ExpressibleByStringInterpolation:
@@ -1594,8 +1602,7 @@ LiteralBindingKind BindingSet::getLiteralKind() const {
15941602
kind = LiteralBindingKind::Atom;
15951603
break;
15961604
}
1597-
}
1598-
1605+
});
15991606
return kind;
16001607
}
16011608

@@ -1662,7 +1669,7 @@ void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
16621669
attributes.push_back("delayed");
16631670
if (isSubtypeOfExistentialType())
16641671
attributes.push_back("subtype_of_existential");
1665-
auto literalKind = getLiteralKind();
1672+
auto literalKind = getLiteralForScore();
16661673
if (literalKind != LiteralBindingKind::None) {
16671674
std::string literalAttrStr;
16681675
literalAttrStr.append("[literal: ");

0 commit comments

Comments
 (0)