Skip to content

Commit 5b6c008

Browse files
authored
Merge pull request swiftlang#21909 from xedin/rdar-47266563
[ConstraintSystem] Skip literal protocols without default types from …
2 parents 5182104 + e26fc1e commit 5b6c008

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

lib/Sema/CSSolver.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,17 +1504,33 @@ void ConstraintSystem::ArgumentInfoCollector::minimizeLiteralProtocols() {
15041504
if (LiteralProtocols.size() <= 1)
15051505
return;
15061506

1507-
llvm::SmallVector<Type, 2> defaultTypes;
1508-
for (auto *protocol : LiteralProtocols)
1509-
defaultTypes.push_back(CS.TC.getDefaultType(protocol, CS.DC));
1507+
llvm::SmallVector<std::pair<ProtocolDecl *, Type>, 2> candidates;
1508+
llvm::SmallVector<ProtocolDecl *, 2> skippedProtocols;
1509+
1510+
for (auto *protocol : LiteralProtocols) {
1511+
if (auto defaultType = CS.TC.getDefaultType(protocol, CS.DC)) {
1512+
candidates.push_back({protocol, defaultType});
1513+
continue;
1514+
}
1515+
1516+
// Looks like argument expected to conform to something like
1517+
// `ExpressibleByNilLiteral` which doesn't have a default
1518+
// type and as a result can't participate in minimalization.
1519+
skippedProtocols.push_back(protocol);
1520+
}
1521+
1522+
if (candidates.size() <= 1)
1523+
return;
1524+
1525+
unsigned result = 0;
1526+
for (unsigned i = 1, n = candidates.size(); i != n; ++i) {
1527+
const auto &candidate = candidates[i];
15101528

1511-
auto result = 0;
1512-
for (unsigned long i = 1; i < LiteralProtocols.size(); ++i) {
15131529
auto first =
1514-
CS.TC.conformsToProtocol(defaultTypes[i], LiteralProtocols[result],
1530+
CS.TC.conformsToProtocol(candidate.second, candidates[result].first,
15151531
CS.DC, ConformanceCheckFlags::InExpression);
15161532
auto second =
1517-
CS.TC.conformsToProtocol(defaultTypes[result], LiteralProtocols[i],
1533+
CS.TC.conformsToProtocol(candidates[result].second, candidate.first,
15181534
CS.DC, ConformanceCheckFlags::InExpression);
15191535
if ((first && second) || (!first && !second))
15201536
return;
@@ -1523,9 +1539,9 @@ void ConstraintSystem::ArgumentInfoCollector::minimizeLiteralProtocols() {
15231539
result = i;
15241540
}
15251541

1526-
auto *protocol = LiteralProtocols[result];
15271542
LiteralProtocols.clear();
1528-
LiteralProtocols.insert(protocol);
1543+
LiteralProtocols.insert(candidates[result].first);
1544+
LiteralProtocols.insert(skippedProtocols.begin(), skippedProtocols.end());
15291545
}
15301546

15311547
void ConstraintSystem::ArgumentInfoCollector::dump() const {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// RUN: %target-swift-frontend %s -typecheck
2+
3+
print(true ? "readline" : nil)

0 commit comments

Comments
 (0)