Skip to content

Commit a862f2b

Browse files
committed
[Parser/libSyntax] Avoid doing lookup for a previous parsed node when we are in backtracking mode
This both addresses a crash during incremental re-parse (rdar://57679731) and generally avoids violating invariants for interacting with the parser library.
1 parent dee6c0b commit a862f2b

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

lib/Parse/SyntaxParsingContext.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ size_t SyntaxParsingContext::lookupNode(size_t LexerOffset, SourceLoc Loc) {
5050
if (!Enabled)
5151
return 0;
5252

53+
// Avoid doing lookup for a previous parsed node when we are in backtracking
54+
// mode. This is because if the parser library client give us a node pointer
55+
// and we discard it due to backtracking then we are violating this invariant:
56+
//
57+
// The parser guarantees that any \c swiftparse_client_node_t, given to the
58+
// parser by \c swiftparse_node_handler_t or \c swiftparse_node_lookup_t,
59+
// will be returned back to the client.
60+
//
61+
// which will end up likely creating a memory leak for the client because
62+
// the semantics is that the parser accepts ownership of the object that the
63+
// node pointer represents.
64+
//
65+
// Note that the fact that backtracking mode is disabling incremental parse
66+
// node re-use is another reason that we should keep backtracking state as
67+
// minimal as possible.
68+
if (isBacktracking())
69+
return 0;
70+
5371
assert(getStorage().size() == Offset &&
5472
"Cannot do lookup if nodes have already been gathered");
5573
assert(Mode == AccumulationMode::CreateSyntax &&
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %validate-incrparse %s --test-case REPLACE
3+
4+
5+
var value: Int {
6+
get { fatalError() }
7+
<<REPLACE<|||}>>>
8+
9+
let x = 10

0 commit comments

Comments
 (0)