Skip to content

Commit 69aec44

Browse files
authored
Avoid debug assertions bout adding the same symbol to the path hierarchy twice (#814)
1 parent 86ea063 commit 69aec44

File tree

3 files changed

+88
-36
lines changed

3 files changed

+88
-36
lines changed

Sources/SwiftDocC/Infrastructure/Link Resolution/PathHierarchy.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2022-2023 Apple Inc. and the Swift project authors
4+
Copyright (c) 2022-2024 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -120,7 +120,8 @@ struct PathHierarchy {
120120
}
121121
}
122122

123-
var topLevelCandidates = nodes
123+
// If there are multiple symbol graphs (for example for different source languages or platforms) then the nodes may have already been added to the hierarchy.
124+
var topLevelCandidates = nodes.filter { _, node in node.parent == nil }
124125
for relationship in graph.relationships where relationship.kind.formsHierarchy {
125126
guard let sourceNode = nodes[relationship.source], let expectedContainerName = sourceNode.symbol?.pathComponents.dropLast().last else {
126127
continue
@@ -180,7 +181,14 @@ struct PathHierarchy {
180181
moduleNode.add(symbolChild: topLevelNode)
181182
}
182183

183-
for node in topLevelCandidates.values where node.symbol!.pathComponents.count > 1 {
184+
assert(
185+
topLevelCandidates.values.filter({ $0.symbol!.pathComponents.count > 1 }).allSatisfy({ $0.parent == nil }), """
186+
Top-level candidates shouldn't already exist in the hierarchy. \
187+
This wasn't true for \(topLevelCandidates.filter({ $0.value.symbol!.pathComponents.count > 1 && $0.value.parent != nil }).map(\.key).sorted())
188+
"""
189+
)
190+
191+
for node in topLevelCandidates.values where node.symbol!.pathComponents.count > 1 && node.parent == nil {
184192
var parent = moduleNode
185193
var components = { (symbol: SymbolGraph.Symbol) -> [String] in
186194
let original = symbol.pathComponents

Sources/SwiftDocC/Infrastructure/Link Resolution/PathHierarchyBasedLinkResolver.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2022-2023 Apple Inc. and the Swift project authors
4+
Copyright (c) 2022-2024 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -14,7 +14,7 @@ import SymbolKit
1414
/// A type that encapsulates resolving links by searching a hierarchy of path components.
1515
final class PathHierarchyBasedLinkResolver {
1616
/// A hierarchy of path components used to resolve links in the documentation.
17-
private(set) var pathHierarchy: PathHierarchy!
17+
private(set) var pathHierarchy: PathHierarchy
1818

1919
/// Map between resolved identifiers and resolved topic references.
2020
private(set) var resolvedReferenceMap = BidirectionalMap<ResolvedIdentifier, ResolvedTopicReference>()

0 commit comments

Comments
 (0)