Skip to content

Commit 8caf6f3

Browse files
author
David Ungar
committed
Fixes for ASTScope lookup
1. Track real EndLoc of TypeAliasDecl and SubscriptDecl. 2. Ensure TypeAliasDecl gets added to AST when doing completion.
1 parent da38e25 commit 8caf6f3

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

include/swift/AST/Decl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2919,6 +2919,9 @@ class TypeAliasDecl : public GenericTypeDecl {
29192919
/// The location of the equal '=' token
29202920
SourceLoc EqualLoc;
29212921

2922+
/// The end of the type
2923+
SourceLoc EndLoc;
2924+
29222925
/// The location of the right-hand side of the typealias binding
29232926
TypeLoc UnderlyingTy;
29242927

@@ -2935,6 +2938,8 @@ class TypeAliasDecl : public GenericTypeDecl {
29352938
return EqualLoc;
29362939
}
29372940

2941+
void setEndLoc(SourceLoc e) { EndLoc = e; }
2942+
29382943
TypeLoc &getUnderlyingTypeLoc() {
29392944
return UnderlyingTy;
29402945
}
@@ -5410,6 +5415,7 @@ enum class ObjCSubscriptKind {
54105415
class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
54115416
SourceLoc StaticLoc;
54125417
SourceLoc ArrowLoc;
5418+
SourceLoc EndLoc;
54135419
ParameterList *Indices;
54145420
TypeLoc ElementTy;
54155421

@@ -5442,6 +5448,9 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
54425448
SourceLoc getStartLoc() const {
54435449
return getStaticLoc().isValid() ? getStaticLoc() : getSubscriptLoc();
54445450
}
5451+
SourceLoc getEndLoc() const { return EndLoc; }
5452+
5453+
void setEndLoc(SourceLoc sl) { EndLoc = sl; }
54455454
SourceRange getSourceRange() const;
54465455
SourceRange getSignatureSourceRange() const;
54475456

lib/AST/Decl.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3503,7 +3503,7 @@ SourceRange TypeAliasDecl::getSourceRange() const {
35033503
return { TypeAliasLoc, TrailingWhereClauseSourceRange.End };
35043504
if (UnderlyingTy.hasLocation())
35053505
return { TypeAliasLoc, UnderlyingTy.getSourceRange().End };
3506-
return { TypeAliasLoc, getNameLoc() };
3506+
return {TypeAliasLoc, EndLoc};
35073507
}
35083508

35093509
void TypeAliasDecl::setUnderlyingType(Type underlying) {
@@ -6069,15 +6069,7 @@ ObjCSubscriptKind SubscriptDecl::getObjCSubscriptKind() const {
60696069
}
60706070

60716071
SourceRange SubscriptDecl::getSourceRange() const {
6072-
if (getBracesRange().isValid()) {
6073-
return { getSubscriptLoc(), getBracesRange().End };
6074-
} else if (ElementTy.getSourceRange().End.isValid()) {
6075-
return { getSubscriptLoc(), ElementTy.getSourceRange().End };
6076-
} else if (ArrowLoc.isValid()) {
6077-
return { getSubscriptLoc(), ArrowLoc };
6078-
} else {
6079-
return getSubscriptLoc();
6080-
}
6072+
return {getSubscriptLoc(), getEndLoc()};
60816073
}
60826074

60836075
SourceRange SubscriptDecl::getSignatureSourceRange() const {

lib/Parse/ParseDecl.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3256,6 +3256,8 @@ void Parser::parseDeclDelayed() {
32563256
} else if (auto *ED = dyn_cast<ExtensionDecl>(parent)) {
32573257
ED->addMember(D);
32583258
} else if (auto *SF = dyn_cast<SourceFile>(parent)) {
3259+
// FIXME: unify notification to ASTSourceFileScope with addMember
3260+
// mechanism used above
32593261
SF->Decls.push_back(D);
32603262
}
32613263
}
@@ -4118,8 +4120,8 @@ parseDeclTypeAlias(Parser::ParseDeclOptions Flags, DeclAttributes &Attributes) {
41184120
if (EqualLoc.isInvalid()) {
41194121
diagnose(Tok, diag::expected_equal_in_typealias);
41204122
Status.setIsParseError();
4123+
return Status;
41214124
}
4122-
return Status;
41234125
}
41244126

41254127
// Exit the scope introduced for the generic parameters.
@@ -6391,6 +6393,7 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
63916393
SignatureHasCodeCompletion |= whereStatus.hasCodeCompletion();
63926394
if (whereStatus.hasCodeCompletion() && !CodeCompletion) {
63936395
// Trigger delayed parsing, no need to continue.
6396+
Subscript->setEndLoc(getEndOfPreviousLoc());
63946397
return whereStatus;
63956398
}
63966399
}
@@ -6434,6 +6437,8 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
64346437

64356438
accessors.record(*this, Subscript, (Invalid || !Status.isSuccess()), Decls);
64366439

6440+
Subscript->setEndLoc(getEndOfPreviousLoc());
6441+
64376442
// No need to setLocalDiscriminator because subscripts cannot
64386443
// validly appear outside of type decls.
64396444
return makeParserResult(Status, Subscript);

test/SourceKit/CodeFormat/indent-implicit-getter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class var foo: Int {
6363
// CHECK: key.sourcetext: " subscript(a: Int) -> Int {"
6464
// CHECK: key.sourcetext: " let x = 3"
6565
// CHECK: key.sourcetext: " return x"
66-
// CHECK: key.sourcetext: " }"
66+
// CHECK: key.sourcetext: " }"
6767
// CHECK: key.sourcetext: "}"
6868
// CHECK: key.sourcetext: " var foo: Int {"
6969
// CHECK: key.sourcetext: " class var foo: Int {"

test/SourceKit/DocSupport/doc_source_file.swift.response

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2188,7 +2188,7 @@
21882188
key.name: "subscript(_:)",
21892189
key.usr: "s:4main3CC2CyS2icip",
21902190
key.offset: 1129,
2191-
key.length: 99,
2191+
key.length: 100,
21922192
key.fully_annotated_decl: "<decl.function.subscript><syntaxtype.keyword>subscript</syntaxtype.keyword>(<decl.var.parameter><decl.var.parameter.name>i</decl.var.parameter.name>: <decl.var.parameter.type><ref.struct usr=\"s:Si\">Int</ref.struct></decl.var.parameter.type></decl.var.parameter>) -&gt; <decl.function.returntype><ref.struct usr=\"s:Si\">Int</ref.struct></decl.function.returntype> { <syntaxtype.keyword>get</syntaxtype.keyword> <syntaxtype.keyword>set</syntaxtype.keyword> }</decl.function.subscript>",
21932193
key.entities: [
21942194
{

0 commit comments

Comments
 (0)