|
15 | 15 | //===----------------------------------------------------------------------===//
|
16 | 16 | #include "swift/Sema/CSBindings.h"
|
17 | 17 | #include "TypeChecker.h"
|
| 18 | +#include "swift/AST/GenericEnvironment.h" |
18 | 19 | #include "swift/Sema/ConstraintGraph.h"
|
19 | 20 | #include "swift/Sema/ConstraintSystem.h"
|
20 | 21 | #include "llvm/ADT/SetVector.h"
|
@@ -1404,12 +1405,51 @@ void PotentialBindings::infer(Constraint *constraint) {
|
1404 | 1405 | case ConstraintKind::SyntacticElement:
|
1405 | 1406 | case ConstraintKind::Conjunction:
|
1406 | 1407 | case ConstraintKind::BindTupleOfFunctionParams:
|
1407 |
| - case ConstraintKind::PackElementOf: |
1408 | 1408 | case ConstraintKind::ShapeOf:
|
1409 | 1409 | case ConstraintKind::ExplicitGenericArguments:
|
1410 | 1410 | // Constraints from which we can't do anything.
|
1411 | 1411 | break;
|
1412 | 1412 |
|
| 1413 | + case ConstraintKind::PackElementOf: { |
| 1414 | + auto elementType = CS.simplifyType(constraint->getFirstType()); |
| 1415 | + auto packType = CS.simplifyType(constraint->getSecondType()); |
| 1416 | + |
| 1417 | + auto *loc = constraint->getLocator(); |
| 1418 | + auto openedElement = |
| 1419 | + loc->getLastElementAs<LocatorPathElt::OpenedPackElement>(); |
| 1420 | + |
| 1421 | + if (elementType->isTypeVariableOrMember() && packType->isTypeVariableOrMember()) |
| 1422 | + break; |
| 1423 | + |
| 1424 | + auto *elementVar = elementType->getAs<TypeVariableType>(); |
| 1425 | + auto *packVar = packType->getAs<TypeVariableType>(); |
| 1426 | + |
| 1427 | + if (elementVar == TypeVar && !packVar) { |
| 1428 | + // Produce a potential binding to the opened element archetype corresponding |
| 1429 | + // to the pack type. |
| 1430 | + packType = packType->mapTypeOutOfContext(); |
| 1431 | + if (auto *expansion = packType->getAs<PackExpansionType>()) |
| 1432 | + packType = expansion->getPatternType(); |
| 1433 | + |
| 1434 | + auto *elementEnv = openedElement->getGenericEnvironment(); |
| 1435 | + auto elementType = elementEnv->mapPackTypeIntoElementContext(packType); |
| 1436 | + addPotentialBinding({elementType, AllowedBindingKind::Exact, constraint}); |
| 1437 | + |
| 1438 | + break; |
| 1439 | + } else if (packVar == TypeVar && !elementVar) { |
| 1440 | + // Produce a potential binding to the pack archetype corresponding to |
| 1441 | + // the opened element type. |
| 1442 | + auto *packEnv = CS.DC->getGenericEnvironmentOfContext(); |
| 1443 | + elementType = elementType->mapTypeOutOfContext(); |
| 1444 | + auto patternType = packEnv->mapElementTypeIntoPackContext(elementType); |
| 1445 | + addPotentialBinding({patternType, AllowedBindingKind::Exact, constraint}); |
| 1446 | + |
| 1447 | + break; |
| 1448 | + } |
| 1449 | + |
| 1450 | + break; |
| 1451 | + } |
| 1452 | + |
1413 | 1453 | // For now let's avoid inferring protocol requirements from
|
1414 | 1454 | // this constraint, but in the future we could do that to
|
1415 | 1455 | // to filter bindings.
|
|
0 commit comments