Skip to content

Commit b122977

Browse files
committed
AST: Record both LHS and RHS index in PackExpansionMatcher.cpp
1 parent 914e0c4 commit b122977

File tree

3 files changed

+57
-32
lines changed

3 files changed

+57
-32
lines changed

include/swift/AST/PackExpansionMatcher.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ struct MatchedPair {
3434
Type lhs;
3535
Type rhs;
3636

37-
// An index into the original left-hand side.
38-
unsigned idx;
37+
unsigned lhsIdx;
38+
unsigned rhsIdx;
3939

40-
MatchedPair(Type lhs, Type rhs, unsigned idx)
41-
: lhs(lhs), rhs(rhs), idx(idx) {}
40+
MatchedPair(Type lhs, Type rhs, unsigned lhsIdx, unsigned rhsIdx)
41+
: lhs(lhs), rhs(rhs), lhsIdx(lhsIdx), rhsIdx(rhsIdx) {}
4242
};
4343

4444
/// Performs a structural match of two lists of tuple elements. The invariant

lib/AST/PackExpansionMatcher.cpp

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ TuplePackMatcher::TuplePackMatcher(TupleType *lhsTuple, TupleType *rhsTuple)
6060
ctx(lhsTuple->getASTContext()) {}
6161

6262
bool TuplePackMatcher::match() {
63-
unsigned idx = 0;
63+
unsigned lhsIdx = 0;
64+
unsigned rhsIdx = 0;
6465

6566
// Iterate over the two tuples in parallel, popping elements from
6667
// the start.
@@ -85,7 +86,7 @@ bool TuplePackMatcher::match() {
8586
"by an unlabeled element");
8687

8788
auto rhs = gatherTupleElements(rhsElts, lhsElt.getName(), ctx);
88-
pairs.emplace_back(lhsExpansionType, rhs, idx++);
89+
pairs.emplace_back(lhsExpansionType, rhs, lhsIdx++, rhsIdx);
8990
continue;
9091
}
9192

@@ -105,7 +106,7 @@ bool TuplePackMatcher::match() {
105106
"by an unlabeled element");
106107

107108
auto lhs = gatherTupleElements(lhsElts, rhsElt.getName(), ctx);
108-
pairs.emplace_back(lhs, rhsExpansionType, idx++);
109+
pairs.emplace_back(lhs, rhsExpansionType, lhsIdx, rhsIdx++);
109110
continue;
110111
}
111112

@@ -116,7 +117,7 @@ bool TuplePackMatcher::match() {
116117
lhsElts = lhsElts.slice(1);
117118
rhsElts = rhsElts.slice(1);
118119

119-
pairs.emplace_back(lhsElt.getType(), rhsElt.getType(), idx++);
120+
pairs.emplace_back(lhsElt.getType(), rhsElt.getType(), lhsIdx++, rhsIdx++);
120121
}
121122

122123
return false;
@@ -135,8 +136,11 @@ bool ParamPackMatcher::match() {
135136
// the pair is a pack expansion type.
136137
unsigned prefixLength = 0;
137138
for (unsigned i = 0; i < minLength; ++i) {
138-
auto lhsParam = lhsParams[i];
139-
auto rhsParam = rhsParams[i];
139+
unsigned lhsIdx = i;
140+
unsigned rhsIdx = i;
141+
142+
auto lhsParam = lhsParams[lhsIdx];
143+
auto rhsParam = rhsParams[rhsIdx];
140144

141145
// FIXME: Check flags
142146

@@ -150,16 +154,19 @@ bool ParamPackMatcher::match() {
150154

151155
// FIXME: Check flags
152156

153-
pairs.emplace_back(lhsType, rhsType, i);
157+
pairs.emplace_back(lhsType, rhsType, lhsIdx, rhsIdx);
154158
++prefixLength;
155159
}
156160

157161
// Consume the longest possible suffix where neither type in
158162
// the pair is a pack expansion type.
159163
unsigned suffixLength = 0;
160164
for (unsigned i = 0; i < minLength - prefixLength; ++i) {
161-
auto lhsParam = lhsParams[lhsParams.size() - i - 1];
162-
auto rhsParam = rhsParams[rhsParams.size() - i - 1];
165+
unsigned lhsIdx = lhsParams.size() - i - 1;
166+
unsigned rhsIdx = rhsParams.size() - i - 1;
167+
168+
auto lhsParam = lhsParams[lhsIdx];
169+
auto rhsParam = rhsParams[rhsIdx];
163170

164171
// FIXME: Check flags
165172

@@ -171,7 +178,7 @@ bool ParamPackMatcher::match() {
171178
break;
172179
}
173180

174-
pairs.emplace_back(lhsType, rhsType, i);
181+
pairs.emplace_back(lhsType, rhsType, lhsIdx, rhsIdx);
175182
++suffixLength;
176183
}
177184

@@ -191,6 +198,9 @@ bool ParamPackMatcher::match() {
191198
if (lhsParams.size() == 1) {
192199
auto lhsType = lhsParams[0].getPlainType();
193200
if (auto *lhsExpansion = lhsType->getAs<PackExpansionType>()) {
201+
unsigned lhsIdx = prefixLength;
202+
unsigned rhsIdx = prefixLength;
203+
194204
SmallVector<Type, 2> rhsTypes;
195205
for (auto rhsParam : rhsParams) {
196206
// FIXME: Check rhs flags
@@ -199,7 +209,7 @@ bool ParamPackMatcher::match() {
199209
auto rhs = createPackBinding(ctx, rhsTypes);
200210

201211
// FIXME: Check lhs flags
202-
pairs.emplace_back(lhsExpansion, rhs, prefixLength);
212+
pairs.emplace_back(lhsExpansion, rhs, lhsIdx, rhsIdx);
203213
return false;
204214
}
205215
}
@@ -209,6 +219,9 @@ bool ParamPackMatcher::match() {
209219
if (rhsParams.size() == 1) {
210220
auto rhsType = rhsParams[0].getPlainType();
211221
if (auto *rhsExpansion = rhsType->getAs<PackExpansionType>()) {
222+
unsigned lhsIdx = prefixLength;
223+
unsigned rhsIdx = prefixLength;
224+
212225
SmallVector<Type, 2> lhsTypes;
213226
for (auto lhsParam : lhsParams) {
214227
// FIXME: Check lhs flags
@@ -217,7 +230,7 @@ bool ParamPackMatcher::match() {
217230
auto lhs = createPackBinding(ctx, lhsTypes);
218231

219232
// FIXME: Check rhs flags
220-
pairs.emplace_back(lhs, rhsExpansion, prefixLength);
233+
pairs.emplace_back(lhs, rhsExpansion, lhsIdx, rhsIdx);
221234
return false;
222235
}
223236
}
@@ -244,31 +257,37 @@ bool PackMatcher::match() {
244257
// the pair is a pack expansion type.
245258
unsigned prefixLength = 0;
246259
for (unsigned i = 0; i < minLength; ++i) {
247-
auto lhsType = lhsTypes[i];
248-
auto rhsType = rhsTypes[i];
260+
unsigned lhsIdx = i;
261+
unsigned rhsIdx = i;
262+
263+
auto lhsType = lhsTypes[lhsIdx];
264+
auto rhsType = rhsTypes[rhsIdx];
249265

250266
if (lhsType->is<PackExpansionType>() ||
251267
rhsType->is<PackExpansionType>()) {
252268
break;
253269
}
254270

255-
pairs.emplace_back(lhsType, rhsType, i);
271+
pairs.emplace_back(lhsType, rhsType, lhsIdx, rhsIdx);
256272
++prefixLength;
257273
}
258274

259275
// Consume the longest possible suffix where neither type in
260276
// the pair is a pack expansion type.
261277
unsigned suffixLength = 0;
262278
for (unsigned i = 0; i < minLength - prefixLength; ++i) {
263-
auto lhsType = lhsTypes[lhsTypes.size() - i - 1];
264-
auto rhsType = rhsTypes[rhsTypes.size() - i - 1];
279+
unsigned lhsIdx = lhsTypes.size() - i - 1;
280+
unsigned rhsIdx = rhsTypes.size() - i - 1;
281+
282+
auto lhsType = lhsTypes[lhsIdx];
283+
auto rhsType = rhsTypes[rhsIdx];
265284

266285
if (lhsType->is<PackExpansionType>() ||
267286
rhsType->is<PackExpansionType>()) {
268287
break;
269288
}
270289

271-
pairs.emplace_back(lhsType, rhsType, i);
290+
pairs.emplace_back(lhsType, rhsType, lhsIdx, rhsIdx);
272291
++suffixLength;
273292
}
274293

@@ -288,9 +307,12 @@ bool PackMatcher::match() {
288307
if (lhsTypes.size() == 1) {
289308
auto lhsType = lhsTypes[0];
290309
if (auto *lhsExpansion = lhsType->getAs<PackExpansionType>()) {
310+
unsigned lhsIdx = prefixLength;
311+
unsigned rhsIdx = prefixLength;
312+
291313
auto rhs = createPackBinding(ctx, rhsTypes);
292314

293-
pairs.emplace_back(lhsExpansion, rhs, prefixLength);
315+
pairs.emplace_back(lhsExpansion, rhs, lhsIdx, rhsIdx);
294316
return false;
295317
}
296318
}
@@ -300,9 +322,12 @@ bool PackMatcher::match() {
300322
if (rhsTypes.size() == 1) {
301323
auto rhsType = rhsTypes[0];
302324
if (auto *rhsExpansion = rhsType->getAs<PackExpansionType>()) {
325+
unsigned lhsIdx = prefixLength;
326+
unsigned rhsIdx = prefixLength;
327+
303328
auto lhs = createPackBinding(ctx, lhsTypes);
304329

305-
pairs.emplace_back(lhs, rhsExpansion, prefixLength);
330+
pairs.emplace_back(lhs, rhsExpansion, lhsIdx, rhsIdx);
306331
return false;
307332
}
308333
}

lib/Sema/CSSimplify.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,7 +2113,7 @@ class TupleMatcher {
21132113
if (elt1.getName() != elt2.getName())
21142114
return true;
21152115

2116-
pairs.emplace_back(elt1.getType(), elt2.getType(), i);
2116+
pairs.emplace_back(elt1.getType(), elt2.getType(), i, i);
21172117
}
21182118

21192119
return false;
@@ -2142,7 +2142,7 @@ class TupleMatcher {
21422142
if (elt1.hasName() && elt1.getName() != elt2.getName())
21432143
return true;
21442144

2145-
pairs.emplace_back(elt1.getType(), elt2.getType(), i);
2145+
pairs.emplace_back(elt1.getType(), elt2.getType(), i, i);
21462146
}
21472147

21482148
return false;
@@ -2180,7 +2180,7 @@ class TupleMatcher {
21802180
hasLabelMismatch = true;
21812181
}
21822182

2183-
pairs.emplace_back(elt1.getType(), elt2.getType(), i);
2183+
pairs.emplace_back(elt1.getType(), elt2.getType(), i, i);
21842184
}
21852185

21862186
return false;
@@ -2209,7 +2209,7 @@ class TupleMatcher {
22092209
auto lhs = tuple1->getElementType(idx1);
22102210
auto rhs = tuple2->getElementType(idx2);
22112211

2212-
pairs.emplace_back(lhs, rhs, idx1);
2212+
pairs.emplace_back(lhs, rhs, idx1, idx2);
22132213
}
22142214

22152215
return false;
@@ -2315,7 +2315,7 @@ ConstraintSystem::matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
23152315
for (auto pair : matcher.pairs) {
23162316
auto result = matchTypes(pair.lhs, pair.rhs, subkind, subflags,
23172317
locator.withPathElement(
2318-
LocatorPathElt::TupleElement(pair.idx)));
2318+
LocatorPathElt::TupleElement(pair.lhsIdx)));
23192319
if (result.isFailure())
23202320
return result;
23212321
}
@@ -2337,7 +2337,7 @@ ConstraintSystem::matchPackTypes(PackType *pack1, PackType *pack2,
23372337
for (auto pair : matcher.pairs) {
23382338
auto result = matchTypes(pair.lhs, pair.rhs, kind, subflags,
23392339
locator.withPathElement(
2340-
LocatorPathElt::PackElement(pair.idx)));
2340+
LocatorPathElt::PackElement(pair.lhsIdx)));
23412341
if (result.isFailure())
23422342
return result;
23432343
}
@@ -3303,7 +3303,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
33033303
(func1Params.size() == 1
33043304
? argumentLocator
33053305
: argumentLocator.withPathElement(
3306-
LocatorPathElt::TupleElement(pair.idx))));
3306+
LocatorPathElt::TupleElement(pair.lhsIdx))));
33073307
if (result.isFailure())
33083308
return result;
33093309
}
@@ -12714,7 +12714,7 @@ ConstraintSystem::simplifyExplicitGenericArgumentsConstraint(
1271412714
addConstraint(
1271512715
ConstraintKind::Bind, pair.lhs, pair.rhs,
1271612716
getConstraintLocator(
12717-
locator, LocatorPathElt::GenericArgument(pair.idx)));
12717+
locator, LocatorPathElt::GenericArgument(pair.lhsIdx)));
1271812718
}
1271912719

1272012720
return SolutionKind::Solved;

0 commit comments

Comments
 (0)