|
23 | 23 | #include "swift/AST/ASTWalker.h"
|
24 | 24 | #include "swift/AST/Expr.h"
|
25 | 25 | #include "swift/AST/GenericSignature.h"
|
| 26 | +#include "swift/AST/GenericEnvironment.h" |
26 | 27 | #include "swift/AST/ParameterList.h"
|
27 | 28 | #include "swift/AST/PrettyStackTrace.h"
|
28 | 29 | #include "swift/AST/SubstitutionMap.h"
|
@@ -1416,11 +1417,23 @@ namespace {
|
1416 | 1417 | Type
|
1417 | 1418 | resolveTypeReferenceInExpression(TypeRepr *repr, TypeResolverContext resCtx,
|
1418 | 1419 | const ConstraintLocatorBuilder &locator) {
|
| 1420 | + TypeResolutionOptions options(resCtx); |
| 1421 | + |
1419 | 1422 | // Introduce type variables for unbound generics.
|
1420 | 1423 | const auto genericOpener = OpenUnboundGenericType(CS, locator);
|
1421 | 1424 | const auto placeholderHandler = HandlePlaceholderType(CS, locator);
|
| 1425 | + |
| 1426 | + // Add a PackElementOf constraint for 'each T' type reprs. |
| 1427 | + GenericEnvironment *elementEnv = nullptr; |
| 1428 | + if (!PackElementEnvironments.empty()) { |
| 1429 | + options |= TypeResolutionFlags::AllowPackReferences; |
| 1430 | + elementEnv = PackElementEnvironments.back(); |
| 1431 | + } |
| 1432 | + const auto packElementOpener = OpenPackElementType(CS, locator, elementEnv); |
| 1433 | + |
1422 | 1434 | const auto result = TypeResolution::resolveContextualType(
|
1423 |
| - repr, CS.DC, resCtx, genericOpener, placeholderHandler); |
| 1435 | + repr, CS.DC, options, genericOpener, placeholderHandler, |
| 1436 | + packElementOpener); |
1424 | 1437 | if (result->hasError()) {
|
1425 | 1438 | CS.recordFix(
|
1426 | 1439 | IgnoreInvalidASTNode::create(CS, CS.getConstraintLocator(locator)));
|
@@ -1659,14 +1672,20 @@ namespace {
|
1659 | 1672 | ArrayRef<TypeRepr *> specializationArgs) {
|
1660 | 1673 | // Resolve each type.
|
1661 | 1674 | SmallVector<Type, 2> specializationArgTypes;
|
1662 |
| - const auto options = |
| 1675 | + auto options = |
1663 | 1676 | TypeResolutionOptions(TypeResolverContext::InExpression);
|
1664 | 1677 | for (auto specializationArg : specializationArgs) {
|
| 1678 | + GenericEnvironment *elementEnv = nullptr; |
| 1679 | + if (!PackElementEnvironments.empty()) { |
| 1680 | + options |= TypeResolutionFlags::AllowPackReferences; |
| 1681 | + elementEnv = PackElementEnvironments.back(); |
| 1682 | + } |
1665 | 1683 | const auto result = TypeResolution::resolveContextualType(
|
1666 | 1684 | specializationArg, CurDC, options,
|
1667 | 1685 | // Introduce type variables for unbound generics.
|
1668 | 1686 | OpenUnboundGenericType(CS, locator),
|
1669 |
| - HandlePlaceholderType(CS, locator)); |
| 1687 | + HandlePlaceholderType(CS, locator), |
| 1688 | + OpenPackElementType(CS, locator, elementEnv)); |
1670 | 1689 | if (result->hasError())
|
1671 | 1690 | return true;
|
1672 | 1691 |
|
@@ -1723,14 +1742,21 @@ namespace {
|
1723 | 1742 | // Bind the specified generic arguments to the type variables in the
|
1724 | 1743 | // open type.
|
1725 | 1744 | auto *const locator = CS.getConstraintLocator(expr);
|
1726 |
| - const auto options = |
| 1745 | + auto options = |
1727 | 1746 | TypeResolutionOptions(TypeResolverContext::InExpression);
|
1728 | 1747 | for (size_t i = 0, e = specializations.size(); i < e; ++i) {
|
| 1748 | + GenericEnvironment *elementEnv = nullptr; |
| 1749 | + if (!PackElementEnvironments.empty()) { |
| 1750 | + options |= TypeResolutionFlags::AllowPackReferences; |
| 1751 | + elementEnv = PackElementEnvironments.back(); |
| 1752 | + } |
| 1753 | + |
1729 | 1754 | const auto result = TypeResolution::resolveContextualType(
|
1730 | 1755 | specializations[i], CS.DC, options,
|
1731 | 1756 | // Introduce type variables for unbound generics.
|
1732 | 1757 | OpenUnboundGenericType(CS, locator),
|
1733 |
| - HandlePlaceholderType(CS, locator)); |
| 1758 | + HandlePlaceholderType(CS, locator), |
| 1759 | + OpenPackElementType(CS, locator, elementEnv)); |
1734 | 1760 | if (result->hasError())
|
1735 | 1761 | return Type();
|
1736 | 1762 |
|
@@ -2983,29 +3009,36 @@ namespace {
|
2983 | 3009 | auto *shapeTypeVar = CS.createTypeVariable(shapeLoc,
|
2984 | 3010 | TVO_CanBindToPack |
|
2985 | 3011 | TVO_CanBindToHole);
|
2986 |
| - auto packReference = expr->getPackElements().front()->getPackRefExpr(); |
2987 |
| - auto packType = CS.simplifyType(CS.getType(packReference)) |
2988 |
| - ->castTo<PackExpansionType>()->getPatternType(); |
2989 |
| - CS.addConstraint(ConstraintKind::ShapeOf, packType, shapeTypeVar, |
2990 |
| - CS.getConstraintLocator(expr)); |
| 3012 | + |
| 3013 | + // Generate ShapeOf constraints between all packs expanded by this |
| 3014 | + // pack expansion expression through the shape type variable. |
| 3015 | + SmallVector<ASTNode, 2> expandedPacks; |
| 3016 | + expr->getExpandedPacks(expandedPacks); |
| 3017 | + for (auto pack : expandedPacks) { |
| 3018 | + Type packType; |
| 3019 | + if (auto *elementExpr = getAsExpr<PackElementExpr>(pack)) { |
| 3020 | + packType = CS.getType(elementExpr->getPackRefExpr()); |
| 3021 | + } else if (auto *elementType = getAsTypeRepr<PackReferenceTypeRepr>(pack)) { |
| 3022 | + packType = CS.getType(elementType->getPackType()); |
| 3023 | + } else { |
| 3024 | + llvm_unreachable("unsupported pack reference ASTNode"); |
| 3025 | + } |
| 3026 | + |
| 3027 | + CS.addConstraint(ConstraintKind::ShapeOf, packType, shapeTypeVar, |
| 3028 | + CS.getConstraintLocator(expr)); |
| 3029 | + } |
2991 | 3030 |
|
2992 | 3031 | return PackExpansionType::get(patternTy, shapeTypeVar);
|
2993 | 3032 | }
|
2994 | 3033 |
|
2995 | 3034 | Type visitPackElementExpr(PackElementExpr *expr) {
|
2996 | 3035 | auto packType = CS.getType(expr->getPackRefExpr());
|
2997 |
| - auto *elementType = |
2998 |
| - CS.createTypeVariable(CS.getConstraintLocator(expr), |
2999 |
| - TVO_CanBindToHole); |
3000 |
| - auto *elementEnv = PackElementEnvironments.back(); |
3001 |
| - auto *elementLoc = CS.getConstraintLocator( |
3002 |
| - expr, LocatorPathElt::OpenedPackElement(elementEnv)); |
3003 | 3036 |
|
3004 | 3037 | // The type of a PackElementExpr is the opened pack element archetype
|
3005 | 3038 | // of the pack reference.
|
3006 |
| - CS.addConstraint(ConstraintKind::PackElementOf, elementType, packType, |
3007 |
| - elementLoc); |
3008 |
| - return elementType; |
| 3039 | + OpenPackElementType openPackElement(CS, CS.getConstraintLocator(expr), |
| 3040 | + PackElementEnvironments.back()); |
| 3041 | + return openPackElement(packType, /*packRepr*/ nullptr); |
3009 | 3042 | }
|
3010 | 3043 |
|
3011 | 3044 | Type visitDynamicTypeExpr(DynamicTypeExpr *expr) {
|
|
0 commit comments