Skip to content

Commit eab0e13

Browse files
author
David Ungar
authored
[Incremental] Improve the interface to DependencySource (#490)
* Improve the interface to DependencySource * Fixup tests * rm IfPresents
1 parent 83c3282 commit eab0e13

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

Sources/SwiftDriver/IncrementalCompilation/DependencyKey.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import TSCBasic
1919
try? fileSystem.getFileInfo(file).modTime
2020
}
2121

22+
var swiftModuleFile: TypedVirtualPath? {
23+
isSwiftModule ? TypedVirtualPath(file: file, type: .swiftModule) : nil
24+
}
25+
2226
public var description: String {
2327
file.name
2428
}
@@ -39,8 +43,13 @@ public struct FingerprintedExternalDependency: Hashable, Equatable, ExternalDepe
3943
assert(verifyExternalDependencyAndFingerprint())
4044
}
4145
var externalDependencyToCheck: ExternalDependency? { externalDependency }
42-
var isIncremental: Bool {
43-
fingerprint != nil && externalDependency.isSwiftModule
46+
var incrementalDependencySource: DependencySource? {
47+
guard let _ = fingerprint,
48+
let swiftModuleFile = externalDependency.swiftModuleFile
49+
else {
50+
return nil
51+
}
52+
return DependencySource(swiftModuleFile)
4453
}
4554
}
4655

Sources/SwiftDriver/IncrementalCompilation/ModuleDependencyGraph.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,9 @@ extension ModuleDependencyGraph {
209209
externalDependency: FingerprintedExternalDependency,
210210
fileSystem: FileSystem
211211
) -> Integrator.Results? {
212-
// Save time; don't even try
213-
guard externalDependency.isIncremental else {
212+
guard let dependencySource = externalDependency.incrementalDependencySource else {
214213
return Integrator.Results()
215214
}
216-
let file = externalDependency.externalDependency.file
217-
let dependencySource = DependencySource(file)
218215
reporter?.report("integrating incremental external dependency",
219216
dependencySource.typedFile)
220217
guard let sourceGraph = dependencySource.read(
@@ -445,6 +442,7 @@ extension ModuleDependencyGraph {
445442
case unexpectedSubblock
446443
case bogusNameOrContext
447444
case unknownKind
445+
case unknownDependencySourceExtension
448446
}
449447

450448
/// Attempts to read a serialized dependency graph from the given path.
@@ -559,9 +557,12 @@ extension ModuleDependencyGraph {
559557
let swiftDepsStr = hasSwiftDeps ? identifiers[Int(record.fields[5])] : nil
560558
let hasFingerprint = Int(record.fields[6]) != 0
561559
let fingerprint = hasFingerprint ? fingerprintStr : nil
562-
let dependencySource = try swiftDepsStr
563-
.map({ try VirtualPath(path: $0) })
564-
.map(DependencySource.init)
560+
guard let dependencySource = try swiftDepsStr
561+
.map({ try VirtualPath(path: $0) })
562+
.map(DependencySource.init)
563+
else {
564+
throw ReadError.unknownDependencySourceExtension
565+
}
565566
self.finalize(node: Node(key: key,
566567
fingerprint: fingerprint,
567568
dependencySource: dependencySource))

Sources/SwiftDriver/IncrementalCompilation/ModuleDependencyGraphParts/DependencySource.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ public struct DependencySource: Hashable, CustomStringConvertible {
2626
}
2727

2828
/*@_spi(Testing)*/
29-
public init(_ file: VirtualPath) {
29+
/// Returns nil if cannot be a source
30+
public init?(_ file: VirtualPath) {
3031
let ext = file.extension
31-
let typeIfExpected =
32+
guard let type =
3233
ext == FileType.swiftDeps .rawValue ? FileType.swiftDeps :
3334
ext == FileType.swiftModule.rawValue ? FileType.swiftModule
3435
: nil
35-
guard let type = typeIfExpected else {
36-
fatalError("unexpected dependencySource extension: \(String(describing: ext))")
36+
else {
37+
return nil
3738
}
3839
self.init(TypedVirtualPath(file: file, type: type))
3940
}
@@ -48,7 +49,7 @@ public struct DependencySource: Hashable, CustomStringConvertible {
4849
// MARK: - mocking
4950
extension DependencySource {
5051
/*@_spi(Testing)*/ public init(mock i: Int) {
51-
self.init(try! VirtualPath(path: String(i) + "." + FileType.swiftDeps.rawValue))
52+
self.init(try! VirtualPath(path: String(i) + "." + FileType.swiftDeps.rawValue))!
5253
}
5354

5455
/*@_spi(Testing)*/ public var mockID: Int {

Tests/SwiftDriverTests/IncrementalCompilationTests.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ final class NonincrementalCompilationTests: XCTestCase {
6868
func testReadBinarySourceFileDependencyGraph() throws {
6969
let absolutePath = try XCTUnwrap(Fixture.fixturePath(at: RelativePath("Incremental"),
7070
for: "main.swiftdeps"))
71-
let dependencySource = DependencySource(VirtualPath.absolute(absolutePath))
71+
let dependencySource = DependencySource(VirtualPath.absolute(absolutePath))!
7272
let graph = try XCTUnwrap(
7373
try SourceFileDependencyGraph(
7474
contentsOf: dependencySource,
@@ -117,7 +117,7 @@ final class NonincrementalCompilationTests: XCTestCase {
117117
for: "hello.swiftdeps"))
118118
let graph = try XCTUnwrap(
119119
try SourceFileDependencyGraph(
120-
contentsOf: DependencySource(VirtualPath.absolute(absolutePath)),
120+
contentsOf: DependencySource(VirtualPath.absolute(absolutePath))!,
121121
on: localFileSystem))
122122
XCTAssertEqual(graph.majorVersion, 1)
123123
XCTAssertEqual(graph.minorVersion, 0)
@@ -171,7 +171,7 @@ final class NonincrementalCompilationTests: XCTestCase {
171171
let data = try localFileSystem.readFileContents(absolutePath)
172172
let graph = try XCTUnwrap(
173173
try SourceFileDependencyGraph(data: data,
174-
from: DependencySource(.absolute(absolutePath)),
174+
from: DependencySource(.absolute(absolutePath))!,
175175
fromSwiftModule: true))
176176
XCTAssertEqual(graph.majorVersion, 1)
177177
XCTAssertEqual(graph.minorVersion, 0)
@@ -822,7 +822,8 @@ class CrossModuleIncrementalBuildTests: XCTestCase {
822822

823823
let sourcePath = path.appending(component: "main.swiftdeps")
824824
let data = try localFileSystem.readFileContents(sourcePath)
825-
let graph = try XCTUnwrap(SourceFileDependencyGraph(data: data, from: DependencySource(.absolute(sourcePath)), fromSwiftModule: false))
825+
let graph = try XCTUnwrap(SourceFileDependencyGraph(data: data, from: DependencySource(.absolute(sourcePath))!,
826+
fromSwiftModule: false))
826827
XCTAssertEqual(graph.majorVersion, 1)
827828
XCTAssertEqual(graph.minorVersion, 0)
828829
graph.verify()

0 commit comments

Comments
 (0)