Skip to content

Commit 2f25d8f

Browse files
committed
[AST] Compute SubscriptDecl EndLoc
Instead of having the parser set it, compute it using the recorded brace location for the accessor record.
1 parent 8fdb48d commit 2f25d8f

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6814,7 +6814,6 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
68146814

68156815
SourceLoc StaticLoc;
68166816
SourceLoc ArrowLoc;
6817-
SourceLoc EndLoc;
68186817
ParameterList *Indices;
68196818
TypeLoc ElementTy;
68206819

@@ -6873,13 +6872,7 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
68736872

68746873
SourceLoc getStaticLoc() const { return StaticLoc; }
68756874
SourceLoc getSubscriptLoc() const { return getNameLoc(); }
6876-
6877-
SourceLoc getStartLoc() const {
6878-
return getStaticLoc().isValid() ? getStaticLoc() : getSubscriptLoc();
6879-
}
6880-
SourceLoc getEndLoc() const { return EndLoc; }
68816875

6882-
void setEndLoc(SourceLoc sl) { EndLoc = sl; }
68836876
SourceRange getSourceRange() const;
68846877
SourceRange getSignatureSourceRange() const;
68856878

lib/AST/Decl.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8860,7 +8860,29 @@ SubscriptDecl *SubscriptDecl::createImported(ASTContext &Context, DeclName Name,
88608860
}
88618861

88628862
SourceRange SubscriptDecl::getSourceRange() const {
8863-
return {getSubscriptLoc(), getEndLoc()};
8863+
auto Start = getStaticLoc().isValid() ? getStaticLoc() : getSubscriptLoc();
8864+
if (Start.isInvalid())
8865+
return SourceRange();
8866+
8867+
if (auto End = getBracesRange().End)
8868+
return SourceRange(Start, End);
8869+
8870+
if (auto *Where = getTrailingWhereClause()) {
8871+
if (auto End = Where->getSourceRange().End)
8872+
return SourceRange(Start, End);
8873+
}
8874+
if (auto *ElementTy = getElementTypeRepr()) {
8875+
if (auto End = ElementTy->getEndLoc())
8876+
return SourceRange(Start, End);
8877+
}
8878+
if (ArrowLoc)
8879+
return SourceRange(Start, ArrowLoc);
8880+
8881+
if (auto *Indices = getIndices()) {
8882+
if (auto End = Indices->getEndLoc())
8883+
return SourceRange(Start, End);
8884+
}
8885+
return SourceRange(Start);
88648886
}
88658887

88668888
SourceRange SubscriptDecl::getSignatureSourceRange() const {

lib/Parse/ParseDecl.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9607,9 +9607,6 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
96079607
Subscript);
96089608
}
96099609

9610-
// Now that it's been parsed, set the end location.
9611-
Subscript->setEndLoc(PreviousLoc);
9612-
96139610
bool Invalid = false;
96149611
// Reject 'subscript' functions outside of type decls
96159612
if (!(Flags & PD_HasContainerType)) {

test/attr/attr_override.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class B : A {
152152
}
153153
}
154154

155-
static subscript (i: String) -> String { // expected-error{{overriding declaration requires an 'override' keyword}} {{10-10=override }}
155+
static subscript (i: String) -> String { // expected-error{{overriding declaration requires an 'override' keyword}} {{3-3=override }}
156156
get {
157157
return "hello"
158158
}

0 commit comments

Comments
 (0)