Skip to content

Commit 8573c99

Browse files
authored
Merge pull request swiftlang#33690 from xedin/csbindings-protocol-propagation
[ConstraintSystem] NFC: Clarify why upward propagation of literal conforma…
2 parents 3e8278a + ebc03a1 commit 8573c99

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,33 @@ void ConstraintSystem::PotentialBindings::inferTransitiveBindings(
5656

5757
auto &bindings = relatedBindings->getSecond();
5858

59-
// Infer transitive protocol requirements.
60-
llvm::copy(bindings.Protocols, std::back_inserter(Protocols));
59+
// FIXME: This is a workaround necessary because solver doesn't filter
60+
// bindings based on protocol requirements placed on a type variable.
61+
//
62+
// Forward propagate (subtype -> supertype) only literal conformance
63+
// requirements since that helps solver to infer more types at
64+
// parameter positions.
65+
//
66+
// \code
67+
// func foo<T: ExpressibleByStringLiteral>(_: String, _: T) -> T {
68+
// fatalError()
69+
// }
70+
//
71+
// func bar(_: Any?) {}
72+
//
73+
// func test() {
74+
// bar(foo("", ""))
75+
// }
76+
// \endcode
77+
//
78+
// If one of the literal arguments doesn't propagate its
79+
// `ExpressibleByStringLiteral` conformance, we'd end up picking
80+
// `T` with only one type `Any?` which is incorrect.
81+
llvm::copy_if(bindings.Protocols, std::back_inserter(Protocols),
82+
[](const Constraint *protocol) {
83+
return protocol->getKind() ==
84+
ConstraintKind::LiteralConformsTo;
85+
});
6186

6287
// Infer transitive defaults.
6388
llvm::copy(bindings.Defaults, std::back_inserter(Defaults));
@@ -906,15 +931,7 @@ bool ConstraintSystem::PotentialBindings::infer(
906931

907932
case ConstraintKind::ConformsTo:
908933
case ConstraintKind::SelfObjectOfProtocol:
909-
// Swift 3 allowed the use of default types for normal conformances
910-
// to expressible-by-literal protocols.
911-
if (cs.getASTContext().LangOpts.EffectiveLanguageVersion[0] >= 4)
912-
return false;
913-
914-
if (!constraint->getSecondType()->is<ProtocolType>())
915-
return false;
916-
917-
LLVM_FALLTHROUGH;
934+
return false;
918935

919936
case ConstraintKind::LiteralConformsTo: {
920937
// Record constraint where protocol requirement originated

0 commit comments

Comments
 (0)