@@ -1089,6 +1089,23 @@ namespace {
10891089 return outputTy;
10901090 }
10911091
1092+ Type openPackElement (Type packType, ConstraintLocator *locator) {
1093+ // If 'each t' is written outside of a pack expansion expression, allow the
1094+ // type to bind to a hole. The invalid pack reference will be diagnosed when
1095+ // attempting to bind the type variable for the underlying pack reference to
1096+ // a pack type without TVO_CanBindToPack.
1097+ if (PackElementEnvironments.empty ()) {
1098+ return CS.createTypeVariable (locator,
1099+ TVO_CanBindToHole | TVO_CanBindToNoEscape);
1100+ }
1101+
1102+ // The type of a PackElementExpr is the opened pack element archetype
1103+ // of the pack reference.
1104+ OpenPackElementType openPackElement (CS, locator,
1105+ PackElementEnvironments.back ());
1106+ return openPackElement (packType, /* packRepr*/ nullptr );
1107+ }
1108+
10921109 public:
10931110 ConstraintGenerator (ConstraintSystem &CS, DeclContext *DC)
10941111 : CS(CS), CurDC(DC ? DC : CS.DC), CurrPhase(CS.getPhase()) {
@@ -1407,6 +1424,20 @@ namespace {
14071424 return invalidateReference ();
14081425 }
14091426
1427+ // value packs cannot be referenced without `each` immediately
1428+ // preceding them.
1429+ if (auto *expansion = knownType->getAs <PackExpansionType>()) {
1430+ if (!PackElementEnvironments.empty () &&
1431+ !isExpr<PackElementExpr>(CS.getParentExpr (E))) {
1432+ auto packType = expansion->getPatternType ();
1433+ (void )CS.recordFix (
1434+ IgnoreMissingEachKeyword::create (CS, packType, locator));
1435+ auto eltType = openPackElement (packType, locator);
1436+ CS.setType (E, eltType);
1437+ return eltType;
1438+ }
1439+ }
1440+
14101441 if (!knownType->hasPlaceholder ()) {
14111442 // Set the favored type for this expression to the known type.
14121443 CS.setFavoredType (E, knownType.getPointer ());
@@ -3067,10 +3098,12 @@ namespace {
30673098 SmallVectorImpl<ASTNode> &packs) {
30683099 struct PackCollector : public ASTWalker {
30693100 private:
3101+ ConstraintSystem &CS;
30703102 SmallVectorImpl<ASTNode> &Packs;
30713103
30723104 public:
3073- PackCollector (SmallVectorImpl<ASTNode> &packs) : Packs(packs) {}
3105+ PackCollector (ConstraintSystem &cs, SmallVectorImpl<ASTNode> &packs)
3106+ : CS(cs), Packs(packs) {}
30743107
30753108 // / Walk everything that's available.
30763109 MacroWalking getMacroWalkingBehavior () const override {
@@ -3087,6 +3120,21 @@ namespace {
30873120 Packs.push_back (E);
30883121 }
30893122
3123+ if (auto *declRef = dyn_cast<DeclRefExpr>(E)) {
3124+ auto type = CS.getTypeIfAvailable (declRef);
3125+ if (!type)
3126+ return Action::Continue (E);
3127+
3128+ if (type->is <ElementArchetypeType>() &&
3129+ CS.hasFixFor (CS.getConstraintLocator (declRef),
3130+ FixKind::IgnoreMissingEachKeyword)) {
3131+ Packs.push_back (PackElementExpr::create (CS.getASTContext (),
3132+ /* eachLoc=*/ SourceLoc (),
3133+ declRef,
3134+ /* implicit=*/ true ));
3135+ }
3136+ }
3137+
30903138 return Action::Continue (E);
30913139 }
30923140
@@ -3102,7 +3150,7 @@ namespace {
31023150
31033151 return Action::Continue ();
31043152 }
3105- } packCollector (packs);
3153+ } packCollector (CS, packs);
31063154
31073155 expansion->getPatternExpr ()->walk (packCollector);
31083156 }
@@ -3162,23 +3210,8 @@ namespace {
31623210 }
31633211
31643212 Type visitPackElementExpr (PackElementExpr *expr) {
3165- auto packType = CS.getType (expr->getPackRefExpr ());
3166-
3167- // If 'each t' is written outside of a pack expansion expression, allow the
3168- // type to bind to a hole. The invalid pack reference will be diagnosed when
3169- // attempting to bind the type variable for the underlying pack reference to
3170- // a pack type without TVO_CanBindToPack.
3171- if (PackElementEnvironments.empty ()) {
3172- return CS.createTypeVariable (CS.getConstraintLocator (expr),
3173- TVO_CanBindToHole |
3174- TVO_CanBindToNoEscape);
3175- }
3176-
3177- // The type of a PackElementExpr is the opened pack element archetype
3178- // of the pack reference.
3179- OpenPackElementType openPackElement (CS, CS.getConstraintLocator (expr),
3180- PackElementEnvironments.back ());
3181- return openPackElement (packType, /* packRepr*/ nullptr );
3213+ return openPackElement (CS.getType (expr->getPackRefExpr ()),
3214+ CS.getConstraintLocator (expr));
31823215 }
31833216
31843217 Type visitMaterializePackExpr (MaterializePackExpr *expr) {
0 commit comments