Skip to content

Commit 389cfe7

Browse files
committed
[ConstraintSystem] NFC: Check whether opened type has a binding before printing
In some circumstances opened type might not have a fixed binding. A good example could be dependent sub-component produced for result builder transformed code (connected via one-way constraints), in such a case `OpenedTypes` would have outer generic parameters but they might not be bound yet, so they have to be printed as type variables.
1 parent 633cc34 commit 389cfe7

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,10 @@ class Solution {
15451545
/// Retrieve the fixed score of this solution
15461546
Score &getFixedScore() { return FixedScore; }
15471547

1548+
/// Check whether this solution has a fixed binding for the given type
1549+
/// variable.
1550+
bool hasFixedType(TypeVariableType *typeVar) const;
1551+
15481552
/// Retrieve the fixed type for the given type variable.
15491553
Type getFixedType(TypeVariableType *typeVar) const;
15501554

lib/Sema/CSApply.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353
using namespace swift;
5454
using namespace constraints;
5555

56+
bool Solution::hasFixedType(TypeVariableType *typeVar) const {
57+
auto knownBinding = typeBindings.find(typeVar);
58+
return knownBinding != typeBindings.end();
59+
}
60+
5661
/// Retrieve the fixed type for the given type variable.
5762
Type Solution::getFixedType(TypeVariableType *typeVar) const {
5863
auto knownBinding = typeBindings.find(typeVar);

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,10 +1362,19 @@ void Solution::dump(raw_ostream &out) const {
13621362
Type(opened.first).print(out, PO);
13631363
out << ")";
13641364
out << " -> ";
1365-
out << getFixedType(opened.second);
1366-
out << " [from ";
1367-
Type(opened.second).print(out, PO);
1368-
out << "]";
1365+
// Let's check whether the type variable has been bound.
1366+
// This is important when solver is working on result
1367+
// builder transformed code, because dependent sub-components
1368+
// would not have parent type variables but `OpenedTypes`
1369+
// cannot be erased, so we'll just print them as unbound.
1370+
if (hasFixedType(opened.second)) {
1371+
out << getFixedType(opened.second);
1372+
out << " [from ";
1373+
Type(opened.second).print(out, PO);
1374+
out << "]";
1375+
} else {
1376+
Type(opened.second).print(out, PO);
1377+
}
13691378
},
13701379
[&]() { out << ", "; });
13711380
out << "\n";

0 commit comments

Comments
 (0)