@@ -1089,6 +1089,23 @@ namespace {
1089
1089
return outputTy;
1090
1090
}
1091
1091
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
+
1092
1109
public:
1093
1110
ConstraintGenerator (ConstraintSystem &CS, DeclContext *DC)
1094
1111
: CS(CS), CurDC(DC ? DC : CS.DC), CurrPhase(CS.getPhase()) {
@@ -1407,6 +1424,20 @@ namespace {
1407
1424
return invalidateReference ();
1408
1425
}
1409
1426
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
+
1410
1441
if (!knownType->hasPlaceholder ()) {
1411
1442
// Set the favored type for this expression to the known type.
1412
1443
CS.setFavoredType (E, knownType.getPointer ());
@@ -3067,10 +3098,12 @@ namespace {
3067
3098
SmallVectorImpl<ASTNode> &packs) {
3068
3099
struct PackCollector : public ASTWalker {
3069
3100
private:
3101
+ ConstraintSystem &CS;
3070
3102
SmallVectorImpl<ASTNode> &Packs;
3071
3103
3072
3104
public:
3073
- PackCollector (SmallVectorImpl<ASTNode> &packs) : Packs(packs) {}
3105
+ PackCollector (ConstraintSystem &cs, SmallVectorImpl<ASTNode> &packs)
3106
+ : CS(cs), Packs(packs) {}
3074
3107
3075
3108
// / Walk everything that's available.
3076
3109
MacroWalking getMacroWalkingBehavior () const override {
@@ -3087,6 +3120,21 @@ namespace {
3087
3120
Packs.push_back (E);
3088
3121
}
3089
3122
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
+
3090
3138
return Action::Continue (E);
3091
3139
}
3092
3140
@@ -3102,7 +3150,7 @@ namespace {
3102
3150
3103
3151
return Action::Continue ();
3104
3152
}
3105
- } packCollector (packs);
3153
+ } packCollector (CS, packs);
3106
3154
3107
3155
expansion->getPatternExpr ()->walk (packCollector);
3108
3156
}
@@ -3162,23 +3210,8 @@ namespace {
3162
3210
}
3163
3211
3164
3212
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));
3182
3215
}
3183
3216
3184
3217
Type visitMaterializePackExpr (MaterializePackExpr *expr) {
0 commit comments