Skip to content

Commit 655aeef

Browse files
committed
[CSBindings] NFC: Associate isCoveredBy(PotentialBinding) with literal requirement
1 parent b3284c6 commit 655aeef

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,33 @@ struct LiteralRequirement {
185185
CoveredBy = coveredBy;
186186
}
187187

188-
bool isCoveredBy(Type type, DeclContext *useDC) const;
188+
/// Determines whether this literal requirement is "covered"
189+
/// by the given binding - type of the binding could either be
190+
/// equal (in canonical sense) to the protocol's default type,
191+
/// or conform to a protocol.
192+
///
193+
/// \param binding The binding to check for coverage.
194+
///
195+
/// \param canBeNil The flag that determines whether given type
196+
/// variable requires all of its bindings to be optional.
197+
///
198+
/// \param useDC The declaration context in which this literal
199+
/// requirement is used.
200+
///
201+
/// \returns a pair of bool and a type:
202+
/// - bool, true if binding covers given literal protocol;
203+
/// - type, non-null if binding type has to be adjusted
204+
/// to cover given literal protocol;
205+
std::pair<bool, Type> isCoveredBy(const PotentialBinding &binding,
206+
bool canBeNil,
207+
DeclContext *useDC) const;
189208

190209
/// Determines whether literal protocol associated with this
191210
/// meta-information is viable for inclusion as a defaultable binding.
192211
bool viableAsBinding() const { return !isCovered() && hasDefaultType(); }
212+
213+
private:
214+
bool isCoveredBy(Type type, DeclContext *useDC) const;
193215
};
194216

195217
struct PotentialBindings {
@@ -392,26 +414,6 @@ struct PotentialBindings {
392414

393415
void addLiteral(Constraint *constraint);
394416

395-
/// Determines whether the given literal protocol is "covered"
396-
/// by the given binding - type of the binding could either be
397-
/// equal (in canonical sense) to the protocol's default type,
398-
/// or conform to a protocol.
399-
///
400-
/// \param literal The literal protocol requirement to check.
401-
///
402-
/// \param binding The binding to check for coverage.
403-
///
404-
/// \param canBeNil The flag that determines whether given type
405-
/// variable requires all of its bindings to be optional.
406-
///
407-
/// \returns a pair of bool and a type:
408-
/// - bool, true if binding covers given literal protocol;
409-
/// - type, non-null if binding type has to be adjusted
410-
/// to cover given literal protocol;
411-
std::pair<bool, Type> isLiteralCoveredBy(const LiteralRequirement &literal,
412-
const PotentialBinding &binding,
413-
bool canBeNil) const;
414-
415417
/// Add a potential binding to the list of bindings,
416418
/// coalescing supertype bounds when we are able to compute the meet.
417419
void addPotentialBinding(PotentialBinding binding, bool allowJoinMeet = true);

lib/Sema/CSBindings.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,9 @@ bool LiteralRequirement::isCoveredBy(Type type, DeclContext *useDC) const {
594594
}
595595

596596
std::pair<bool, Type>
597-
PotentialBindings::isLiteralCoveredBy(const LiteralRequirement &literal,
598-
const PotentialBinding &binding,
599-
bool canBeNil) const {
597+
LiteralRequirement::isCoveredBy(const PotentialBinding &binding,
598+
bool canBeNil,
599+
DeclContext *useDC) const {
600600
auto type = binding.BindingType;
601601
switch (binding.Kind) {
602602
case AllowedBindingKind::Exact:
@@ -616,7 +616,7 @@ PotentialBindings::isLiteralCoveredBy(const LiteralRequirement &literal,
616616
if (type->isTypeVariableOrMember() || type->isHole())
617617
return std::make_pair(false, Type());
618618

619-
if (literal.isCoveredBy(type, CS.DC)) {
619+
if (isCoveredBy(type, useDC)) {
620620
return std::make_pair(true, requiresUnwrap ? type : binding.BindingType);
621621
}
622622

@@ -628,7 +628,7 @@ PotentialBindings::isLiteralCoveredBy(const LiteralRequirement &literal,
628628
// If this literal protocol is not a direct requirement it
629629
// would not be possible to change optionality while inferring
630630
// bindings for a supertype, so this hack doesn't apply.
631-
if (!literal.isDirectRequirement())
631+
if (!isDirectRequirement())
632632
return std::make_pair(false, Type());
633633

634634
// If we're allowed to bind to subtypes, look through optionals.
@@ -726,7 +726,7 @@ void PotentialBindings::addPotentialBinding(PotentialBinding binding,
726726
Type adjustedTy;
727727

728728
std::tie(isCovered, adjustedTy) =
729-
isLiteralCoveredBy(info, binding, allowsNil);
729+
info.isCoveredBy(binding, allowsNil, CS.DC);
730730

731731
if (!isCovered)
732732
continue;
@@ -800,7 +800,7 @@ void PotentialBindings::addLiteral(Constraint *constraint) {
800800
Type adjustedTy;
801801

802802
std::tie(isCovered, adjustedTy) =
803-
isLiteralCoveredBy(literal, *binding, allowsNil);
803+
literal.isCoveredBy(*binding, allowsNil, CS.DC);
804804

805805
// No luck here, let's try next literal requirement.
806806
if (!isCovered)

0 commit comments

Comments
 (0)