Skip to content

Commit fa4f7dd

Browse files
committed
Parse: Don't create PatternBindingDecls with overlapping source ranges
This was happening in the error recovery path when parsing accessors on a pattern binding declaration that does not bind any variables, eg let _: Int { 0 }
1 parent 4d875ee commit fa4f7dd

File tree

7 files changed

+18
-27
lines changed

7 files changed

+18
-27
lines changed

include/swift/Parse/Parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ class Parser {
11161116
ParsedAccessors &accessors,
11171117
AbstractStorageDecl *storage,
11181118
SourceLoc StaticLoc);
1119-
ParserResult<VarDecl> parseDeclVarGetSet(Pattern *pattern,
1119+
ParserResult<VarDecl> parseDeclVarGetSet(PatternBindingEntry &entry,
11201120
ParseDeclOptions Flags,
11211121
SourceLoc StaticLoc,
11221122
StaticSpellingKind StaticSpelling,

lib/IDE/SyntaxModel.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,9 @@ bool ModelASTWalker::walkToDeclPre(Decl *D) {
990990
if (bracesRange.isValid())
991991
SN.BodyRange = innerCharSourceRangeFromSourceRange(SM, bracesRange);
992992
SourceLoc NRStart = VD->getNameLoc();
993-
SourceLoc NREnd = NRStart.getAdvancedLoc(VD->getName().getLength());
993+
SourceLoc NREnd = (!VD->getName().empty()
994+
? NRStart.getAdvancedLoc(VD->getName().getLength())
995+
: NRStart);
994996
SN.NameRange = CharSourceRange(SM, NRStart, NREnd);
995997
SN.TypeRange = charSourceRangeFromSourceRange(SM,
996998
VD->getTypeSourceRangeForDiagnostics());

lib/Parse/ParseDecl.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5680,14 +5680,16 @@ ParserStatus Parser::parseGetSet(ParseDeclOptions Flags,
56805680

56815681
/// Parse the brace-enclosed getter and setter for a variable.
56825682
ParserResult<VarDecl>
5683-
Parser::parseDeclVarGetSet(Pattern *pattern, ParseDeclOptions Flags,
5683+
Parser::parseDeclVarGetSet(PatternBindingEntry &entry, ParseDeclOptions Flags,
56845684
SourceLoc StaticLoc,
56855685
StaticSpellingKind StaticSpelling,
56865686
SourceLoc VarLoc, bool hasInitializer,
56875687
const DeclAttributes &Attributes,
56885688
SmallVectorImpl<Decl *> &Decls) {
56895689
bool Invalid = false;
5690-
5690+
5691+
auto *pattern = entry.getPattern();
5692+
56915693
// The grammar syntactically requires a simple identifier for the variable
56925694
// name. Complain if that isn't what we got. But for recovery purposes,
56935695
// make an effort to look through other things anyway.
@@ -5730,22 +5732,12 @@ Parser::parseDeclVarGetSet(Pattern *pattern, ParseDeclOptions Flags,
57305732
VarDecl::Introducer::Var,
57315733
VarLoc, Identifier(),
57325734
CurDeclContext);
5733-
storage->setImplicit(true);
57345735
storage->setInvalid();
57355736

5736-
Pattern *pattern =
5737+
pattern =
57375738
TypedPattern::createImplicit(Context, new (Context) NamedPattern(storage),
57385739
ErrorType::get(Context));
5739-
PatternBindingEntry entry(pattern, /*EqualLoc*/ SourceLoc(),
5740-
/*Init*/ nullptr, /*InitContext*/ nullptr);
5741-
auto binding = PatternBindingDecl::create(Context, StaticLoc,
5742-
StaticSpelling,
5743-
VarLoc, entry, CurDeclContext);
5744-
binding->setInvalid();
5745-
storage->setParentPatternBinding(binding);
5746-
5747-
Decls.push_back(binding);
5748-
Decls.push_back(storage);
5740+
entry.setPattern(pattern);
57495741
}
57505742

57515743
// Parse getter and setter.
@@ -6157,7 +6149,8 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
61576149
if (Tok.is(tok::l_brace)) {
61586150
HasAccessors = true;
61596151
auto boundVar =
6160-
parseDeclVarGetSet(pattern, Flags, StaticLoc, StaticSpelling, VarLoc,
6152+
parseDeclVarGetSet(PBDEntries.back(),
6153+
Flags, StaticLoc, StaticSpelling, VarLoc,
61616154
PatternInit != nullptr, Attributes, Decls);
61626155
if (boundVar.hasCodeCompletion())
61636156
return makeResult(makeParserCodeCompletionStatus());

test/Index/invalid_code.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// RUN: %target-swift-ide-test -print-indexed-symbols -include-locals -source-filename %s | %FileCheck %s
22

3-
// CHECK: [[@LINE+1]]:8 | struct/Swift | Int | {{.*}} | Ref | rel: 0
43
var _: Int { get { return 1 } }
54

65
func test() {

test/Parse/pattern_without_variables.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,4 @@ func testVarLetPattern(a : SimpleEnum) {
4040

4141
class SR10903 {
4242
static var _: Int { 0 } //expected-error {{getter/setter can only be defined for a single variable}}
43-
//expected-error@-1 {{property declaration does not bind any variables}}
4443
}

test/SourceKit/DocumentStructure/structure.swift.foobar.response

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,11 +573,10 @@
573573
key.kind: source.lang.swift.decl.var.global,
574574
key.accessibility: source.lang.swift.accessibility.internal,
575575
key.setter_accessibility: source.lang.swift.accessibility.internal,
576-
key.name: "Qtys",
577576
key.offset: 1079,
578-
key.length: 15,
579-
key.nameoffset: 1089,
580-
key.namelength: 4
577+
key.length: 3,
578+
key.nameoffset: 1079,
579+
key.namelength: 0
581580
},
582581
{
583582
key.kind: source.lang.swift.stmt.foreach,

test/SourceKit/DocumentStructure/structure.swift.response

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,11 +573,10 @@
573573
key.kind: source.lang.swift.decl.var.global,
574574
key.accessibility: source.lang.swift.accessibility.internal,
575575
key.setter_accessibility: source.lang.swift.accessibility.internal,
576-
key.name: "Qtys",
577576
key.offset: 1079,
578-
key.length: 15,
579-
key.nameoffset: 1089,
580-
key.namelength: 4
577+
key.length: 3,
578+
key.nameoffset: 1079,
579+
key.namelength: 0
581580
},
582581
{
583582
key.kind: source.lang.swift.stmt.foreach,

0 commit comments

Comments
 (0)