Skip to content

Commit 3651147

Browse files
committedSep 30, 2021
[AST] Preserve source location in packIntoImplicitTupleOrParen
When type-checking a tuple construction such as `Void()`, make sure to preserve the source info from the argument list in the resulting type-checked TupleExpr `()`. This is needed for serialization to be able to grab the textual representation. SR-15181 rdar://83202870
1 parent c2a9272 commit 3651147

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed
 

‎lib/AST/ArgumentList.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -220,28 +220,35 @@ Optional<unsigned> ArgumentList::findArgumentExpr(Expr *expr,
220220
Expr *ArgumentList::packIntoImplicitTupleOrParen(
221221
ASTContext &ctx, llvm::function_ref<Type(Expr *)> getType) const {
222222
assert(!hasAnyInOutArgs() && "Cannot construct bare tuple/paren with inout");
223+
224+
// Make sure to preserve the source location info here and below as it may be
225+
// needed for e.g serialization of its textual representation.
223226
if (auto *unary = getUnlabeledUnaryExpr()) {
224-
auto *paren = new (ctx) ParenExpr(SourceLoc(), unary, SourceLoc());
227+
auto *paren = new (ctx) ParenExpr(getLParenLoc(), unary, getRParenLoc());
225228
if (auto ty = getType(unary))
226229
paren->setType(ParenType::get(ctx, ty));
227230
paren->setImplicit();
228231
return paren;
229232
}
230233

231-
SmallVector<Expr *, 8> argExprs;
232-
SmallVector<Identifier, 8> argLabels;
233-
SmallVector<TupleTypeElt, 8> tupleEltTypes;
234+
SmallVector<Expr *, 2> argExprs;
235+
SmallVector<Identifier, 2> argLabels;
236+
SmallVector<SourceLoc, 2> argLabelLocs;
237+
SmallVector<TupleTypeElt, 2> tupleEltTypes;
234238

235239
for (auto arg : *this) {
236240
auto *argExpr = arg.getExpr();
237241
argExprs.push_back(argExpr);
238242
argLabels.push_back(arg.getLabel());
243+
argLabelLocs.push_back(arg.getLabelLoc());
239244
if (auto ty = getType(argExpr))
240245
tupleEltTypes.emplace_back(ty, arg.getLabel());
241246
}
242247
assert(tupleEltTypes.empty() || tupleEltTypes.size() == argExprs.size());
243248

244-
auto *tuple = TupleExpr::createImplicit(ctx, argExprs, argLabels);
249+
auto *tuple =
250+
TupleExpr::create(ctx, getLParenLoc(), argExprs, argLabels, argLabelLocs,
251+
getRParenLoc(), /*implicit*/ true);
245252
if (empty() || !tupleEltTypes.empty())
246253
tuple->setType(TupleType::get(tupleEltTypes, ctx));
247254

‎test/ModuleInterface/default-args.swift

+4
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,7 @@ public func hasMagicDefaultArgs(_ f: String = #file, _ fu: String = #function, _
5454
// CHECK: func hasSimpleDefaultArgs(_ x: Swift.Int = 0, b: Swift.Int = 1)
5555
public func hasSimpleDefaultArgs(_ x: Int = 0, b: Int = 1) {
5656
}
57+
58+
// rdar://83202870 (SR-15181): Make sure we can extract the textual representation here.
59+
// CHECK: func hasTupleConstructionDefaultArgs(_ x: Any = (), y: (Swift.String, Swift.Int) = ("", 0))
60+
public func hasTupleConstructionDefaultArgs(_ x: Any = Void(), y: (String, Int) = (String, Int)("", 0)) {}

0 commit comments

Comments
 (0)
Please sign in to comment.