Skip to content

Commit 5e3a55d

Browse files
committed
Sema: Extract coalesceIntegerAndFloatLiteralRequirements() from BindingSet::BindingSet, and do it at the end
1 parent 77346c7 commit 5e3a55d

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,11 @@ class BindingSet {
574574
/// requirements down the subtype or equivalence chain.
575575
void inferTransitiveProtocolRequirements();
576576

577+
/// Try to coalesce integer and floating point literal protocols
578+
/// if they appear together because the only possible default type that
579+
/// could satisfy both requirements is `Double`.
580+
void coalesceIntegerAndFloatLiteralRequirements();
581+
577582
/// Check whether the given binding set covers any of the literal protocols
578583
/// associated with this type variable. The idea is that if a type variable
579584
/// has a binding like Int and also it has a conformance requirement to

lib/Sema/CSBindings.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,28 +1031,32 @@ void BindingSet::determineLiteralCoverage() {
10311031
}
10321032
}
10331033

1034-
void BindingSet::addLiteralRequirement(Constraint *constraint) {
1035-
auto *protocol = constraint->getProtocol();
1034+
void BindingSet::coalesceIntegerAndFloatLiteralRequirements() {
1035+
for (const auto &pair : Literals) {
1036+
auto *protocol = pair.first;
10361037

1037-
// Let's try to coalesce integer and floating point literal protocols
1038-
// if they appear together because the only possible default type that
1039-
// could satisfy both requirements is `Double`.
1040-
{
10411038
if (protocol->isSpecificProtocol(
10421039
KnownProtocolKind::ExpressibleByIntegerLiteral)) {
10431040
auto *floatLiteral = CS.getASTContext().getProtocol(
10441041
KnownProtocolKind::ExpressibleByFloatLiteral);
1045-
if (Literals.count(floatLiteral))
1042+
if (Literals.count(floatLiteral)) {
1043+
Literals.erase(protocol);
10461044
return;
1045+
}
10471046
}
10481047

10491048
if (protocol->isSpecificProtocol(
10501049
KnownProtocolKind::ExpressibleByFloatLiteral)) {
10511050
auto *intLiteral = CS.getASTContext().getProtocol(
10521051
KnownProtocolKind::ExpressibleByIntegerLiteral);
10531052
Literals.erase(intLiteral);
1053+
return;
10541054
}
10551055
}
1056+
}
1057+
1058+
void BindingSet::addLiteralRequirement(Constraint *constraint) {
1059+
auto *protocol = constraint->getProtocol();
10561060

10571061
if (Literals.count(protocol) > 0)
10581062
return;
@@ -1235,6 +1239,9 @@ const BindingSet *ConstraintSystem::determineBestBindings(
12351239
bestBindings = &bindings;
12361240
}
12371241

1242+
if (bestBindings)
1243+
bestBindings->coalesceIntegerAndFloatLiteralRequirements();
1244+
12381245
return bestBindings;
12391246
}
12401247

@@ -1667,6 +1674,7 @@ BindingSet ConstraintSystem::getBindingsFor(TypeVariableType *typeVar) {
16671674
(void) bindings.finalizeKeyPathBindings();
16681675
bindings.finalizeUnresolvedMemberChainResult();
16691676
bindings.determineLiteralCoverage();
1677+
bindings.coalesceIntegerAndFloatLiteralRequirements();
16701678

16711679
return bindings;
16721680
}

0 commit comments

Comments
 (0)