Skip to content

Commit 8f9d30b

Browse files
committed
Preserve trailing closure index in OriginalArgumentList
1 parent b4bf12f commit 8f9d30b

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

include/swift/Sema/IDETypeChecking.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,32 @@ namespace swift {
243243
SmallVector<SourceLoc, 4> labelLocs;
244244
SourceLoc lParenLoc;
245245
SourceLoc rParenLoc;
246-
bool hasTrailingClosure = false;
246+
Optional<unsigned> unlabeledTrailingClosureIdx;
247+
248+
/// The number of trailing closures in the argument list.
249+
unsigned getNumTrailingClosures() const {
250+
if (!unlabeledTrailingClosureIdx)
251+
return 0;
252+
return args.size() - *unlabeledTrailingClosureIdx;
253+
}
254+
255+
/// Whether any unlabeled or labeled trailing closures are present.
256+
bool hasAnyTrailingClosures() const {
257+
return unlabeledTrailingClosureIdx.hasValue();
258+
}
259+
260+
/// Whether the given index is for an unlabeled trailing closure.
261+
bool isUnlabeledTrailingClosureIdx(unsigned i) const {
262+
return unlabeledTrailingClosureIdx && *unlabeledTrailingClosureIdx == i;
263+
}
264+
265+
/// Whether the given index is for a labeled trailing closure in an
266+
/// argument list with multiple trailing closures.
267+
bool isLabeledTrailingClosureIdx(unsigned i) const {
268+
if (!unlabeledTrailingClosureIdx)
269+
return false;
270+
return i > *unlabeledTrailingClosureIdx && i < args.size();
271+
}
247272
};
248273

249274
/// When applying a solution to a constraint system, the type checker rewrites

lib/Sema/CSGen.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4232,9 +4232,17 @@ OriginalArgumentList
42324232
swift::getOriginalArgumentList(Expr *expr) {
42334233
OriginalArgumentList result;
42344234

4235-
auto add = [&](Expr *arg, Identifier label, SourceLoc labelLoc) {
4236-
if (isa<DefaultArgumentExpr>(arg)) {
4235+
auto oldTrailingClosureIdx =
4236+
expr->getUnlabeledTrailingClosureIndexOfPackedArgument();
4237+
Optional<unsigned> newTrailingClosureIdx;
4238+
4239+
auto add = [&](unsigned i, Expr *arg, Identifier label, SourceLoc labelLoc) {
4240+
if (isa<DefaultArgumentExpr>(arg))
42374241
return;
4242+
4243+
if (oldTrailingClosureIdx && *oldTrailingClosureIdx == i) {
4244+
assert(!newTrailingClosureIdx);
4245+
newTrailingClosureIdx = result.args.size();
42384246
}
42394247

42404248
if (auto *varargExpr = dyn_cast<VarargExpansionExpr>(arg)) {
@@ -4260,24 +4268,25 @@ swift::getOriginalArgumentList(Expr *expr) {
42604268
if (auto *parenExpr = dyn_cast<ParenExpr>(expr)) {
42614269
result.lParenLoc = parenExpr->getLParenLoc();
42624270
result.rParenLoc = parenExpr->getRParenLoc();
4263-
result.hasTrailingClosure = parenExpr->hasTrailingClosure();
4264-
add(parenExpr->getSubExpr(), Identifier(), SourceLoc());
4271+
add(0, parenExpr->getSubExpr(), Identifier(), SourceLoc());
42654272
} else if (auto *tupleExpr = dyn_cast<TupleExpr>(expr)) {
42664273
result.lParenLoc = tupleExpr->getLParenLoc();
42674274
result.rParenLoc = tupleExpr->getRParenLoc();
4268-
result.hasTrailingClosure = tupleExpr->hasTrailingClosure();
42694275

42704276
auto args = tupleExpr->getElements();
42714277
auto labels = tupleExpr->getElementNames();
42724278
auto labelLocs = tupleExpr->getElementNameLocs();
42734279
for (unsigned i = 0, e = args.size(); i != e; ++i) {
42744280
// Implicit TupleExprs don't always store label locations.
4275-
add(args[i], labels[i],
4281+
add(i, args[i], labels[i],
42764282
labelLocs.empty() ? SourceLoc() : labelLocs[i]);
42774283
}
42784284
} else {
4279-
add(expr, Identifier(), SourceLoc());
4285+
add(0, expr, Identifier(), SourceLoc());
42804286
}
4287+
assert(oldTrailingClosureIdx.hasValue() == newTrailingClosureIdx.hasValue());
4288+
assert(!newTrailingClosureIdx || *newTrailingClosureIdx < result.args.size());
42814289

4290+
result.unlabeledTrailingClosureIdx = newTrailingClosureIdx;
42824291
return result;
42834292
}

lib/Sema/TypeCheckCodeCompletion.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,18 +288,18 @@ class SanitizeExpr : public ASTWalker {
288288
new (C) ParenExpr(argList.lParenLoc,
289289
argList.args[0],
290290
argList.rParenLoc,
291-
argList.hasTrailingClosure);
291+
argList.hasAnyTrailingClosures());
292292
result->setImplicit();
293293
return result;
294294
}
295295

296296
return TupleExpr::create(C,
297297
argList.lParenLoc,
298+
argList.rParenLoc,
298299
argList.args,
299300
argList.labels,
300301
argList.labelLocs,
301-
argList.rParenLoc,
302-
argList.hasTrailingClosure,
302+
argList.unlabeledTrailingClosureIdx,
303303
/*implicit=*/true);
304304
}
305305

0 commit comments

Comments
 (0)