Skip to content

Commit d236a17

Browse files
author
David Ungar
authored
Merge pull request #26095 from davidungar/parser-changes
[Parser] Fixes for ASTScope lookup
2 parents f1850ba + 634a6eb commit d236a17

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

include/swift/AST/Decl.h

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

2920+
/// The end of the type, valid even when the type cannot be parsed
2921+
SourceLoc TypeEndLoc;
2922+
29202923
/// The location of the right-hand side of the typealias binding
29212924
TypeLoc UnderlyingTy;
29222925

@@ -2933,6 +2936,8 @@ class TypeAliasDecl : public GenericTypeDecl {
29332936
return EqualLoc;
29342937
}
29352938

2939+
void setTypeEndLoc(SourceLoc e) { TypeEndLoc = e; }
2940+
29362941
TypeLoc &getUnderlyingTypeLoc() {
29372942
return UnderlyingTy;
29382943
}
@@ -5413,6 +5418,7 @@ enum class ObjCSubscriptKind {
54135418
class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
54145419
SourceLoc StaticLoc;
54155420
SourceLoc ArrowLoc;
5421+
SourceLoc EndLoc;
54165422
ParameterList *Indices;
54175423
TypeLoc ElementTy;
54185424

@@ -5445,6 +5451,9 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
54455451
SourceLoc getStartLoc() const {
54465452
return getStaticLoc().isValid() ? getStaticLoc() : getSubscriptLoc();
54475453
}
5454+
SourceLoc getEndLoc() const { return EndLoc; }
5455+
5456+
void setEndLoc(SourceLoc sl) { EndLoc = sl; }
54485457
SourceRange getSourceRange() const;
54495458
SourceRange getSignatureSourceRange() const;
54505459

lib/AST/Decl.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3503,6 +3503,8 @@ SourceRange TypeAliasDecl::getSourceRange() const {
35033503
return { TypeAliasLoc, TrailingWhereClauseSourceRange.End };
35043504
if (UnderlyingTy.hasLocation())
35053505
return { TypeAliasLoc, UnderlyingTy.getSourceRange().End };
3506+
if (TypeEndLoc.isValid())
3507+
return { TypeAliasLoc, TypeEndLoc };
35063508
return { TypeAliasLoc, getNameLoc() };
35073509
}
35083510

@@ -6080,15 +6082,7 @@ ObjCSubscriptKind SubscriptDecl::getObjCSubscriptKind() const {
60806082
}
60816083

60826084
SourceRange SubscriptDecl::getSourceRange() const {
6083-
if (getBracesRange().isValid()) {
6084-
return { getSubscriptLoc(), getBracesRange().End };
6085-
} else if (ElementTy.getSourceRange().End.isValid()) {
6086-
return { getSubscriptLoc(), ElementTy.getSourceRange().End };
6087-
} else if (ArrowLoc.isValid()) {
6088-
return { getSubscriptLoc(), ArrowLoc };
6089-
} else {
6090-
return getSubscriptLoc();
6091-
}
6085+
return {getSubscriptLoc(), getEndLoc()};
60926086
}
60936087

60946088
SourceRange SubscriptDecl::getSignatureSourceRange() const {

lib/Parse/ParseDecl.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4096,6 +4096,7 @@ parseDeclTypeAlias(Parser::ParseDeclOptions Flags, DeclAttributes &Attributes) {
40964096
}
40974097

40984098
UnderlyingTy = parseType(diag::expected_type_in_typealias);
4099+
TAD->setTypeEndLoc(PreviousLoc);
40994100
Status |= UnderlyingTy;
41004101
}
41014102

@@ -4118,8 +4119,8 @@ parseDeclTypeAlias(Parser::ParseDeclOptions Flags, DeclAttributes &Attributes) {
41184119
if (EqualLoc.isInvalid()) {
41194120
diagnose(Tok, diag::expected_equal_in_typealias);
41204121
Status.setIsParseError();
4122+
return Status;
41214123
}
4122-
return Status;
41234124
}
41244125

41254126
// Exit the scope introduced for the generic parameters.
@@ -4867,7 +4868,8 @@ Parser::parseDeclVarGetSet(Pattern *pattern, ParseDeclOptions Flags,
48674868
// If we have an invalid case, bail out now.
48684869
if (!PrimaryVar) {
48694870
fillInAccessorTypeErrors(*this, accessors);
4870-
Decls.append(accessors.Accessors.begin(), accessors.Accessors.end());
4871+
// Preserve the invariant that an accessor can be found from its VarDecl
4872+
accessors.record(*this, storage, Invalid, Decls);
48714873
return nullptr;
48724874
}
48734875

@@ -6425,6 +6427,9 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
64256427
accessors, Subscript, StaticLoc);
64266428
}
64276429

6430+
// Now that it's been parsed, set the end location.
6431+
Subscript->setEndLoc(PreviousLoc);
6432+
64286433
bool Invalid = false;
64296434
// Reject 'subscript' functions outside of type decls
64306435
if (!(Flags & PD_HasContainerType)) {

0 commit comments

Comments
 (0)