Skip to content

Commit 94916de

Browse files
author
Nathan Hawes
committed
[AST] Restore getSourceRange() on DefaultArgumentExpr.
This restores getSourceRange() on DefaultArgumentExpr after it was removed in swiftlang#31184. It was originally removed to solve the issues it was causing when computing the source range of its parent TupleExpr. To account for trailing closures we walk back through the tuple's arguments until one with a valid location is found, which we use as the end location. If the last argument was a DefaultArgumentExpr though that meant the end loc would end up being the tuple's start location, so none of the tuple's other arguments were contained in its range, triggering an ASTVerifier assertion. Source tooling and diagnostics don't care about default arg expression locations as nothing can reference them, but their locations are output in the debug info. Added a regression test to catch that in future, and updated TupleExpr::getSourceRange() to ignore them when computing the end loc. Resolves rdar://problem/63195504.
1 parent 01dcc29 commit 94916de

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

include/swift/AST/Expr.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4227,9 +4227,7 @@ class DefaultArgumentExpr final : public Expr {
42274227
DefaultArgsOwner(defaultArgsOwner), ParamIndex(paramIndex), Loc(loc),
42284228
ContextOrCallerSideExpr(dc) { }
42294229

4230-
SourceRange getSourceRange() const { return {}; }
4231-
4232-
SourceLoc getArgumentListLoc() const { return Loc; }
4230+
SourceRange getSourceRange() const { return Loc; }
42334231

42344232
ConcreteDeclRef getDefaultArgsOwner() const {
42354233
return DefaultArgsOwner;

lib/AST/Expr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,10 @@ SourceRange TupleExpr::getSourceRange() const {
12791279
} else {
12801280
// Scan backwards for a valid source loc.
12811281
for (Expr *expr : llvm::reverse(getElements())) {
1282+
// Default arguments are located at the start of their parent tuple, so
1283+
// skip over them.
1284+
if (isa<DefaultArgumentExpr>(expr))
1285+
continue;
12821286
end = expr->getEndLoc();
12831287
if (end.isValid()) {
12841288
break;

lib/Sema/TypeCheckExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ Expr *CallerSideDefaultArgExprRequest::evaluate(
789789

790790
// Re-create the default argument using the location info of the call site.
791791
auto *initExpr =
792-
synthesizeCallerSideDefault(param, defaultExpr->getArgumentListLoc());
792+
synthesizeCallerSideDefault(param, defaultExpr->getLoc());
793793
auto *dc = defaultExpr->ContextOrCallerSideExpr.get<DeclContext *>();
794794
assert(dc && "Expected a DeclContext before type-checking caller-side arg");
795795

test/DebugInfo/callexpr.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %target-swift-frontend %s -g -emit-ir -o - | %FileCheck %s
2+
// RUN: %target-swift-frontend %s -g -emit-ir -o - | %FileCheck --check-prefix=CHECK2 %s
23

34
func markUsed<T>(_ t: T) {}
45

@@ -15,3 +16,11 @@ let r = foo(
1516
) // CHECK: ![[OUTER]] = !DILocation(line: [[@LINE-3]],
1617
markUsed(r)
1718

19+
struct MyType {}
20+
func bar(x: MyType = MyType()) {}
21+
22+
// CHECK2: call {{.*}}MyType{{.*}}, !dbg ![[DEFAULTARG:.*]]
23+
// CHECK2: call {{.*}}bar{{.*}}, !dbg ![[BARCALL:.*]]
24+
bar() // CHECK2: ![[DEFAULTARG]] = !DILocation(line: [[@LINE]], column: 4
25+
// CHECK2: ![[BARCALL]] = !DILocation(line: [[@LINE-1]], column: 1
26+

0 commit comments

Comments
 (0)