Skip to content

SyntaxTreeDeserializer: Hold on to omitted nodes across multiple updates #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions Sources/SwiftSyntax/RawSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,6 @@ final class RawSyntax {
}
}).value
}

/// Creates a copy of `other`.
init(_ other: RawSyntax) {
self.data = other.data
self.presence = other.presence
self.id = other.id
}

init(kind: SyntaxKind, layout: [RawSyntax?], presence: SourcePresence,
id: SyntaxNodeId? = nil) {
Expand Down Expand Up @@ -341,7 +334,17 @@ extension RawSyntax {
}
}

extension RawSyntax: Codable {
// This is needed purely to have a self assignment initializer for
// RawSyntax.init(from:Decoder) so we can reuse omitted nodes, instead of
// copying them.
private protocol _RawSyntaxFactory {}
extension _RawSyntaxFactory {
init(_instance: Self) {
self = _instance
}
}

extension RawSyntax: Codable, _RawSyntaxFactory {
/// Creates a RawSyntax from the provided Foundation Decoder.
convenience init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
Expand All @@ -359,7 +362,7 @@ extension RawSyntax: Codable {
guard let lookupNode = lookupFunc(id) else {
throw IncrementalDecodingError.nodeLookupFailed(id)
}
self.init(lookupNode)
self.init(_instance: lookupNode)
return
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/SwiftSyntax/WeakLookupTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ class WeakLookupTable<Element: Identifiable & AnyObject> {
return reserveCapacity(estimatedCount &+ 1)
}

/// Inserts the given object into the table.
/// Inserts the given object into the table if the table doesn't already
/// contain an object with the same identifier.
/// - returns: true if the object was inserted
@discardableResult
func insert(_ obj: Element) -> Bool {
var (pos, alreadyExists) = _findHole(obj.id)
Expand Down
14 changes: 14 additions & 0 deletions Tests/SwiftSyntaxTest/DeserializeFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,18 @@ public class DecodeSyntaxTestCase: XCTestCase {
XCTAssertEqual("\(parsed)", source)
}())
}

public func testIncrementalDeserialize() {
XCTAssertNoThrow(try {
let deserializer = SyntaxTreeDeserializer()
var tree: SourceFileSyntax? = nil
let responses = try (1...3).map {
try Data(contentsOf: getInput("incremental-deserialize-\($0).json"))
}
for data in responses {
tree = try deserializer.deserialize(data, serializationFormat: .json)
}
XCTAssertEqual(tree?.description, "x\ny")
}())
}
}
51 changes: 51 additions & 0 deletions Tests/SwiftSyntaxTest/Inputs/incremental-deserialize-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"id":39,
"kind":"SourceFile",
"layout":[
{
"id":38,
"kind":"CodeBlockItemList",
"layout":[
{
"id":35,
"kind":"CodeBlockItem",
"layout":[
{
"id":34,
"kind":"IdentifierExpr",
"layout":[
{
"id":33,
"tokenKind":{
"kind":"identifier",
"text":"x"
},
"leadingTrivia":[],
"trailingTrivia":[],
"presence":"Present"
},
null
],
"presence":"Present"
},
null,
null
],
"presence":"Present"
}
],
"presence":"Present"
},
{
"id":37,
"tokenKind":{
"kind":"eof",
"text":""
},
"leadingTrivia":[],
"trailingTrivia":[],
"presence":"Present"
}
],
"presence":"Present"
}
33 changes: 33 additions & 0 deletions Tests/SwiftSyntaxTest/Inputs/incremental-deserialize-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"id":45,
"kind":"SourceFile",
"layout":[
{
"id":44,
"kind":"CodeBlockItemList",
"layout":[
{
"id":35,
"omitted":true
}
],
"presence":"Present"
},
{
"id":43,
"tokenKind":{
"kind":"eof",
"text":""
},
"leadingTrivia":[
{
"kind":"Newline",
"value":1
}
],
"trailingTrivia":[],
"presence":"Present"
}
],
"presence":"Present"
}
60 changes: 60 additions & 0 deletions Tests/SwiftSyntaxTest/Inputs/incremental-deserialize-3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"id":54,
"kind":"SourceFile",
"layout":[
{
"id":53,
"kind":"CodeBlockItemList",
"layout":[
{
"id":35,
"omitted":true
},
{
"id":50,
"kind":"CodeBlockItem",
"layout":[
{
"id":49,
"kind":"IdentifierExpr",
"layout":[
{
"id":48,
"tokenKind":{
"kind":"identifier",
"text":"y"
},
"leadingTrivia":[
{
"kind":"Newline",
"value":1
}
],
"trailingTrivia":[],
"presence":"Present"
},
null
],
"presence":"Present"
},
null,
null
],
"presence":"Present"
}
],
"presence":"Present"
},
{
"id":52,
"tokenKind":{
"kind":"eof",
"text":""
},
"leadingTrivia":[],
"trailingTrivia":[],
"presence":"Present"
}
],
"presence":"Present"
}
12 changes: 12 additions & 0 deletions Tests/SwiftSyntaxTest/WeakLookupTableTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ public class WeakLookupTableTestCase: XCTestCase {

_ = tree // Prevents 'never read' warning
}

func testSameIdentifierDifferentObject() {
let table = WeakLookupTable<MinimalNode>()

let object1 = MinimalNode(id: "1", children: [])
let object2 = MinimalNode(id: "1", children: [])

XCTAssertTrue(table.insert(object1))
XCTAssert(table["1"] === object1)
XCTAssertFalse(table.insert(object2))
XCTAssert(table["1"] === object1)
}
}

#endif // DEBUG