Skip to content

Commit 12693d5

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 e8df655 commit 12693d5

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
@@ -4133,9 +4133,7 @@ class DefaultArgumentExpr final : public Expr {
41334133
DefaultArgsOwner(defaultArgsOwner), ParamIndex(paramIndex), Loc(loc),
41344134
ContextOrCallerSideExpr(dc) { }
41354135

4136-
SourceRange getSourceRange() const { return {}; }
4137-
4138-
SourceLoc getArgumentListLoc() const { return Loc; }
4136+
SourceRange getSourceRange() const { return Loc; }
41394137

41404138
ConcreteDeclRef getDefaultArgsOwner() const {
41414139
return DefaultArgsOwner;

lib/AST/Expr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,10 @@ SourceRange TupleExpr::getSourceRange() const {
12441244
} else {
12451245
// Scan backwards for a valid source loc.
12461246
for (Expr *expr : llvm::reverse(getElements())) {
1247+
// Default arguments are located at the start of their parent tuple, so
1248+
// skip over them.
1249+
if (isa<DefaultArgumentExpr>(expr))
1250+
continue;
12471251
end = expr->getEndLoc();
12481252
if (end.isValid()) {
12491253
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)