Skip to content

Commit

Permalink
Parse: Don't create PatternBindingDecls with overlapping source ranges
Browse files Browse the repository at this point in the history
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 }
  • Loading branch information
slavapestov committed Sep 22, 2020
1 parent 4d875ee commit fa4f7dd
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 27 deletions.
2 changes: 1 addition & 1 deletion include/swift/Parse/Parser.h
Expand Up @@ -1116,7 +1116,7 @@ class Parser {
ParsedAccessors &accessors,
AbstractStorageDecl *storage,
SourceLoc StaticLoc);
ParserResult<VarDecl> parseDeclVarGetSet(Pattern *pattern,
ParserResult<VarDecl> parseDeclVarGetSet(PatternBindingEntry &entry,
ParseDeclOptions Flags,
SourceLoc StaticLoc,
StaticSpellingKind StaticSpelling,
Expand Down
4 changes: 3 additions & 1 deletion lib/IDE/SyntaxModel.cpp
Expand Up @@ -990,7 +990,9 @@ bool ModelASTWalker::walkToDeclPre(Decl *D) {
if (bracesRange.isValid())
SN.BodyRange = innerCharSourceRangeFromSourceRange(SM, bracesRange);
SourceLoc NRStart = VD->getNameLoc();
SourceLoc NREnd = NRStart.getAdvancedLoc(VD->getName().getLength());
SourceLoc NREnd = (!VD->getName().empty()
? NRStart.getAdvancedLoc(VD->getName().getLength())
: NRStart);
SN.NameRange = CharSourceRange(SM, NRStart, NREnd);
SN.TypeRange = charSourceRangeFromSourceRange(SM,
VD->getTypeSourceRangeForDiagnostics());
Expand Down
23 changes: 8 additions & 15 deletions lib/Parse/ParseDecl.cpp
Expand Up @@ -5680,14 +5680,16 @@ ParserStatus Parser::parseGetSet(ParseDeclOptions Flags,

/// Parse the brace-enclosed getter and setter for a variable.
ParserResult<VarDecl>
Parser::parseDeclVarGetSet(Pattern *pattern, ParseDeclOptions Flags,
Parser::parseDeclVarGetSet(PatternBindingEntry &entry, ParseDeclOptions Flags,
SourceLoc StaticLoc,
StaticSpellingKind StaticSpelling,
SourceLoc VarLoc, bool hasInitializer,
const DeclAttributes &Attributes,
SmallVectorImpl<Decl *> &Decls) {
bool Invalid = false;


auto *pattern = entry.getPattern();

// The grammar syntactically requires a simple identifier for the variable
// name. Complain if that isn't what we got. But for recovery purposes,
// make an effort to look through other things anyway.
Expand Down Expand Up @@ -5730,22 +5732,12 @@ Parser::parseDeclVarGetSet(Pattern *pattern, ParseDeclOptions Flags,
VarDecl::Introducer::Var,
VarLoc, Identifier(),
CurDeclContext);
storage->setImplicit(true);
storage->setInvalid();

Pattern *pattern =
pattern =
TypedPattern::createImplicit(Context, new (Context) NamedPattern(storage),
ErrorType::get(Context));
PatternBindingEntry entry(pattern, /*EqualLoc*/ SourceLoc(),
/*Init*/ nullptr, /*InitContext*/ nullptr);
auto binding = PatternBindingDecl::create(Context, StaticLoc,
StaticSpelling,
VarLoc, entry, CurDeclContext);
binding->setInvalid();
storage->setParentPatternBinding(binding);

Decls.push_back(binding);
Decls.push_back(storage);
entry.setPattern(pattern);
}

// Parse getter and setter.
Expand Down Expand Up @@ -6157,7 +6149,8 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
if (Tok.is(tok::l_brace)) {
HasAccessors = true;
auto boundVar =
parseDeclVarGetSet(pattern, Flags, StaticLoc, StaticSpelling, VarLoc,
parseDeclVarGetSet(PBDEntries.back(),
Flags, StaticLoc, StaticSpelling, VarLoc,
PatternInit != nullptr, Attributes, Decls);
if (boundVar.hasCodeCompletion())
return makeResult(makeParserCodeCompletionStatus());
Expand Down
1 change: 0 additions & 1 deletion test/Index/invalid_code.swift
@@ -1,6 +1,5 @@
// RUN: %target-swift-ide-test -print-indexed-symbols -include-locals -source-filename %s | %FileCheck %s

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

func test() {
Expand Down
1 change: 0 additions & 1 deletion test/Parse/pattern_without_variables.swift
Expand Up @@ -40,5 +40,4 @@ func testVarLetPattern(a : SimpleEnum) {

class SR10903 {
static var _: Int { 0 } //expected-error {{getter/setter can only be defined for a single variable}}
//expected-error@-1 {{property declaration does not bind any variables}}
}
Expand Up @@ -573,11 +573,10 @@
key.kind: source.lang.swift.decl.var.global,
key.accessibility: source.lang.swift.accessibility.internal,
key.setter_accessibility: source.lang.swift.accessibility.internal,
key.name: "Qtys",
key.offset: 1079,
key.length: 15,
key.nameoffset: 1089,
key.namelength: 4
key.length: 3,
key.nameoffset: 1079,
key.namelength: 0
},
{
key.kind: source.lang.swift.stmt.foreach,
Expand Down
7 changes: 3 additions & 4 deletions test/SourceKit/DocumentStructure/structure.swift.response
Expand Up @@ -573,11 +573,10 @@
key.kind: source.lang.swift.decl.var.global,
key.accessibility: source.lang.swift.accessibility.internal,
key.setter_accessibility: source.lang.swift.accessibility.internal,
key.name: "Qtys",
key.offset: 1079,
key.length: 15,
key.nameoffset: 1089,
key.namelength: 4
key.length: 3,
key.nameoffset: 1079,
key.namelength: 0
},
{
key.kind: source.lang.swift.stmt.foreach,
Expand Down

0 comments on commit fa4f7dd

Please sign in to comment.