Skip to content

Commit 3c67271

Browse files
committed
[AST] Handle a few more cases in getStartLoc()
Handle PatternBindingDecls with missing var locations, which can happen for loop iterator vars, and FuncDecls with missing name and func locations, which can happen for `defer`. Also while here make sure we set the source location of a parser-produced ErrorExpr.
1 parent 98c5fde commit 3c67271

File tree

7 files changed

+41
-14
lines changed

7 files changed

+41
-14
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,9 +2523,7 @@ class PatternBindingDecl final : public Decl,
25232523
Pattern *Pat, Expr *E,
25242524
DeclContext *Parent);
25252525

2526-
SourceLoc getStartLoc() const {
2527-
return StaticLoc.isValid() ? StaticLoc : VarLoc;
2528-
}
2526+
SourceLoc getStartLoc() const;
25292527
SourceRange getSourceRange() const;
25302528

25312529
unsigned getNumPatternEntries() const {
@@ -8413,9 +8411,7 @@ class FuncDecl : public AbstractFunctionDecl {
84138411
SourceLoc getStaticLoc() const { return StaticLoc; }
84148412
SourceLoc getFuncLoc() const { return FuncLoc; }
84158413

8416-
SourceLoc getStartLoc() const {
8417-
return StaticLoc.isValid() ? StaticLoc : FuncLoc;
8418-
}
8414+
SourceLoc getStartLoc() const;
84198415
SourceRange getSourceRange() const;
84208416

84218417
TypeRepr *getResultTypeRepr() const { return FnRetType.getTypeRepr(); }

lib/AST/Decl.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,6 +2528,19 @@ StringRef PatternBindingEntry::getInitStringRepresentation(
25282528
return extractInlinableText(ctx, init, scratch);
25292529
}
25302530

2531+
SourceLoc PatternBindingDecl::getStartLoc() const {
2532+
if (StaticLoc.isValid())
2533+
return StaticLoc;
2534+
2535+
if (VarLoc.isValid())
2536+
return VarLoc;
2537+
2538+
if (getPatternList().empty())
2539+
return SourceLoc();
2540+
2541+
return getPatternList().front().getStartLoc();
2542+
}
2543+
25312544
SourceRange PatternBindingDecl::getSourceRange() const {
25322545
SourceLoc startLoc = getStartLoc();
25332546
SourceLoc endLoc = getPatternList().empty()
@@ -11444,6 +11457,24 @@ DestructorDecl *DestructorDecl::getSuperDeinit() const {
1144411457
return nullptr;
1144511458
}
1144611459

11460+
SourceLoc FuncDecl::getStartLoc() const {
11461+
auto startLoc = StaticLoc;
11462+
if (startLoc.isInvalid())
11463+
startLoc = FuncLoc;
11464+
if (startLoc.isInvalid())
11465+
startLoc = getNameLoc();
11466+
if (startLoc.isInvalid())
11467+
startLoc = getSignatureSourceRange().Start;
11468+
if (startLoc.isInvalid())
11469+
startLoc = getResultTypeSourceRange().Start;
11470+
if (startLoc.isInvalid())
11471+
startLoc = getGenericTrailingWhereClauseSourceRange().Start;
11472+
if (startLoc.isInvalid())
11473+
startLoc = getOriginalBodySourceRange().Start;
11474+
11475+
return startLoc;
11476+
}
11477+
1144711478
SourceRange FuncDecl::getSourceRange() const {
1144811479
SourceLoc startLoc = getStartLoc();
1144911480

lib/Parse/ParseExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3639,7 +3639,7 @@ Parser::parseExprCollectionElement(std::optional<bool> &isDictionary) {
36393639
} else {
36403640
diagnose(Tok, diag::expected_colon_in_dictionary_literal);
36413641
Value = makeParserResult(makeParserError(),
3642-
new (Context) ErrorExpr(SourceRange()));
3642+
new (Context) ErrorExpr(PreviousLoc));
36433643
}
36443644

36453645
// Make a tuple of Key Value pair.

test/Concurrency/async_main_resolution.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ extension MainProtocol {
6363
#endif
6464

6565
// CHECK-IS-SYNC-LABEL: "MyMain" interface_type="MyMain.Type"
66-
// CHECK-IS-SYNC: (func_decl {{.*}}implicit "$main()" interface_type="(MyMain.Type) -> () -> ()"
66+
// CHECK-IS-SYNC: (func_decl {{.*}}implicit range={{.*}} "$main()" interface_type="(MyMain.Type) -> () -> ()"
6767
// CHECK-IS-SYNC: (declref_expr implicit type="(MyMain.Type) -> () -> ()"
6868

6969
// CHECK-IS-ASYNC-LABEL: "MyMain" interface_type="MyMain.Type"
70-
// CHECK-IS-ASYNC: (func_decl {{.*}}implicit "$main()" interface_type="(MyMain.Type) -> () async -> ()"
70+
// CHECK-IS-ASYNC: (func_decl {{.*}}implicit range={{.*}} "$main()" interface_type="(MyMain.Type) -> () async -> ()"
7171
// CHECK-IS-ASYNC: (declref_expr implicit type="(MyMain.Type) -> () async -> ()"
7272

7373
// CHECK-IS-ERROR1: error: 'MyMain' is annotated with '@main' and must provide a main static function of type {{\(\) -> Void or \(\) throws -> Void|\(\) -> Void, \(\) throws -> Void, \(\) async -> Void, or \(\) async throws -> Void}}

test/Concurrency/where_clause_main_resolution.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ protocol App {
2525
// CHECK-SAME: interface_type="<Self where Self : App> (Self.Type) -> () async -> ()"
2626

2727
extension App where Configuration == Config1 {
28-
// CHECK-CONFIG1: (func_decl {{.*}}implicit "$main()" interface_type="(MainType.Type) -> () -> ()"
28+
// CHECK-CONFIG1: (func_decl {{.*}}implicit range=[{{.*}}:[[@LINE+20]]:1 - line:[[@LINE+20]]:1] "$main()" interface_type="(MainType.Type) -> () -> ()"
2929
// CHECK-CONFIG1: [[SOURCE_FILE]]:[[# @LINE+1 ]]
3030
static func main() { }
3131
}
3232

3333
extension App where Configuration == Config2 {
34-
// CHECK-CONFIG2: (func_decl {{.*}}implicit "$main()" interface_type="(MainType.Type) -> () async -> ()"
34+
// CHECK-CONFIG2: (func_decl {{.*}}implicit range=[{{.*}}:[[@LINE+14]]:1 - line:[[@LINE+14]]:1] "$main()" interface_type="(MainType.Type) -> () async -> ()"
3535
// CHECK-CONFIG2: [[SOURCE_FILE]]:[[# @LINE+1 ]]
3636
static func main() async { }
3737
}
3838

3939
extension App where Configuration == Config3 {
40-
// CHECK-CONFIG3-ASYNC: (func_decl {{.*}}implicit "$main()" interface_type="(MainType.Type) -> () async -> ()"
40+
// CHECK-CONFIG3-ASYNC: (func_decl {{.*}}implicit range=[{{.*}}:[[@LINE+8]]:1 - line:[[@LINE+8]]:1] "$main()" interface_type="(MainType.Type) -> () async -> ()"
4141
// CHECK-CONFIG3-ASYNC: [[SOURCE_FILE]]:[[DEFAULT_ASYNCHRONOUS_MAIN_LINE]]
4242
}
4343

test/attr/ApplicationMain/attr_main_throws.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct MyBase {
77
}
88
}
99

10-
// CHECK-AST: (func_decl {{.*}} implicit "$main()" interface_type="(MyBase.Type) -> () throws -> ()" access=internal static
10+
// CHECK-AST: (func_decl {{.*}} implicit range=[{{.*}}:[[@LINE-6]]:1 - line:[[@LINE-6]]:1] "$main()" interface_type="(MyBase.Type) -> () throws -> ()" access=internal static
1111
// CHECK-AST-NEXT: (parameter "self" {{.*}})
1212
// CHECK-AST-NEXT: (parameter_list)
1313
// CHECK-AST-NEXT: (brace_stmt implicit

test/expr/capture/top-level-guard.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ let closureCapture: () -> Void = { [x] in
3939
}
4040

4141
// CHECK-LABEL: (defer_stmt
42-
// CHECK-NEXT: (func_decl{{.*}}implicit "$defer()" interface_type="() -> ()" access=fileprivate captures=(x<direct><noescape>)
42+
// CHECK-NEXT: (func_decl{{.*}}implicit range={{.*}} "$defer()" interface_type="() -> ()" access=fileprivate captures=(x<direct><noescape>)
4343
defer {
4444
_ = x
4545
}

0 commit comments

Comments
 (0)