Skip to content

Commit 086cba7

Browse files
authored
[NFC] Fix typo, make formatting in PubGrub code consistent (#7717)
`tracking know assignments` -> `tracking known assignments`
1 parent ea8e8e2 commit 086cba7

7 files changed

+107
-81
lines changed

Sources/PackageGraph/Resolution/PubGrub/Assignment.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ public struct Assignment: Equatable {
3838

3939
/// An assignment made during decision making.
4040
public static func decision(_ term: Term, decisionLevel: Int) -> Assignment {
41-
assert(term.requirement.isExact, "Cannot create a decision assignment with a non-exact version selection: \(term.requirement)")
41+
assert(
42+
term.requirement.isExact,
43+
"Cannot create a decision assignment with a non-exact version selection: \(term.requirement)"
44+
)
4245

4346
return self.init(
4447
term: term,
@@ -68,9 +71,9 @@ extension Assignment: CustomStringConvertible {
6871
public var description: String {
6972
switch self.isDecision {
7073
case true:
71-
return "[Decision \(self.decisionLevel): \(self.term)]"
74+
"[Decision \(self.decisionLevel): \(self.term)]"
7275
case false:
73-
return "[Derivation: \(self.term)\(self.cause?.description ?? "-")]"
76+
"[Derivation: \(self.term)\(self.cause?.description ?? "-")]"
7477
}
7578
}
7679
}

Sources/PackageGraph/Resolution/PubGrub/DiagnosticReportBuilder.swift

+35-27
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ struct DiagnosticReportBuilder {
1919
private var lineNumbers: [Incompatibility: Int] = [:]
2020
private let provider: ContainerProvider
2121

22-
init(root: DependencyResolutionNode, incompatibilities: [DependencyResolutionNode: [Incompatibility]], provider: ContainerProvider) {
22+
init(
23+
root: DependencyResolutionNode,
24+
incompatibilities: [DependencyResolutionNode: [Incompatibility]],
25+
provider: ContainerProvider
26+
) {
2327
self.rootNode = root
2428
self.incompatibilities = incompatibilities
2529
self.provider = provider
@@ -41,20 +45,20 @@ struct DiagnosticReportBuilder {
4145
try self.visit(rootCause)
4246
} else {
4347
assertionFailure("Unimplemented")
44-
self.record(
48+
try self.record(
4549
rootCause,
46-
message: try self.description(for: rootCause),
50+
message: self.description(for: rootCause),
4751
isNumbered: false
4852
)
4953
}
5054

51-
var content: String = ""
55+
var content = ""
5256
let padding = self.lineNumbers.isEmpty ? 0 : "\(Array(self.lineNumbers.values).last!) ".count
5357

5458
for (idx, line) in self.lines.enumerated() {
5559
content += String(repeating: " ", count: padding)
5660
if line.number != -1 {
57-
content += String(repeating: " ", count: padding)
61+
content += String(repeating: " ", count: padding)
5862
content += " (\(line.number)) "
5963
}
6064
content += line.message.prefix(1).capitalized
@@ -86,9 +90,9 @@ struct DiagnosticReportBuilder {
8690
let otherLine = self.lineNumbers[cause.other]
8791

8892
if let conflictLine, let otherLine {
89-
self.record(
93+
try self.record(
9094
incompatibility,
91-
message: "\(incompatibilityDesc) because \(try self.description(for: cause.conflict)) (\(conflictLine)) and \(try self.description(for: cause.other)) (\(otherLine).",
95+
message: "\(incompatibilityDesc) because \(self.description(for: cause.conflict)) (\(conflictLine)) and \(self.description(for: cause.other)) (\(otherLine).",
9296
isNumbered: isNumbered
9397
)
9498
} else if conflictLine != nil || otherLine != nil {
@@ -106,9 +110,9 @@ struct DiagnosticReportBuilder {
106110
}
107111

108112
try self.visit(withoutLine)
109-
self.record(
113+
try self.record(
110114
incompatibility,
111-
message: "\(conjunction)\(incompatibilityDesc) because \(try self.description(for: withLine)) \(line).",
115+
message: "\(conjunction)\(incompatibilityDesc) because \(self.description(for: withLine)) \(line).",
112116
isNumbered: isNumbered
113117
)
114118
} else {
@@ -127,9 +131,9 @@ struct DiagnosticReportBuilder {
127131
} else {
128132
try self.visit(cause.conflict, isConclusion: true)
129133
try self.visit(cause.other)
130-
self.record(
134+
try self.record(
131135
incompatibility,
132-
message: "\(conjunction)\(incompatibilityDesc) because \(try self.description(for: cause.conflict)) (\(self.lineNumbers[cause.conflict]!)).",
136+
message: "\(conjunction)\(incompatibilityDesc) because \(self.description(for: cause.conflict)) (\(self.lineNumbers[cause.conflict]!)).",
133137
isNumbered: isNumbered
134138
)
135139
}
@@ -139,9 +143,9 @@ struct DiagnosticReportBuilder {
139143
let ext = cause.conflict.cause.isConflict ? cause.other : cause.conflict
140144
let derivedLine = self.lineNumbers[derived]
141145
if let derivedLine {
142-
self.record(
146+
try self.record(
143147
incompatibility,
144-
message: "\(incompatibilityDesc) because \(try self.description(for: ext)) and \(try self.description(for: derived)) (\(derivedLine)).",
148+
message: "\(incompatibilityDesc) because \(self.description(for: ext)) and \(self.description(for: derived)) (\(derivedLine)).",
145149
isNumbered: isNumbered
146150
)
147151
} else if self.isCollapsible(derived) {
@@ -150,27 +154,28 @@ struct DiagnosticReportBuilder {
150154
return
151155
}
152156

153-
let collapsedDerived = derivedCause.conflict.cause.isConflict ? derivedCause.conflict : derivedCause.other
157+
let collapsedDerived = derivedCause.conflict.cause.isConflict ? derivedCause.conflict : derivedCause
158+
.other
154159
let collapsedExt = derivedCause.conflict.cause.isConflict ? derivedCause.other : derivedCause.conflict
155160

156161
try self.visit(collapsedDerived)
157-
self.record(
162+
try self.record(
158163
incompatibility,
159-
message: "\(conjunction)\(incompatibilityDesc) because \(try self.description(for: collapsedExt)) and \(try self.description(for: ext)).",
164+
message: "\(conjunction)\(incompatibilityDesc) because \(self.description(for: collapsedExt)) and \(self.description(for: ext)).",
160165
isNumbered: isNumbered
161166
)
162167
} else {
163168
try self.visit(derived)
164-
self.record(
169+
try self.record(
165170
incompatibility,
166-
message: "\(conjunction)\(incompatibilityDesc) because \(try self.description(for: ext)).",
171+
message: "\(conjunction)\(incompatibilityDesc) because \(self.description(for: ext)).",
167172
isNumbered: isNumbered
168173
)
169174
}
170175
} else {
171-
self.record(
176+
try self.record(
172177
incompatibility,
173-
message: "\(incompatibilityDesc) because \(try self.description(for: cause.conflict)) and \(try self.description(for: cause.other)).",
178+
message: "\(incompatibilityDesc) because \(self.description(for: cause.conflict)) and \(self.description(for: cause.other)).",
174179
isNumbered: isNumbered
175180
)
176181
}
@@ -213,13 +218,16 @@ struct DiagnosticReportBuilder {
213218
return "package '\(versionedDependency.identity)' is required using a stable-version but '\(versionedDependency.identity)' depends on an unstable-version package '\(unversionedDependency.identity)'"
214219
case .incompatibleToolsVersion(let version):
215220
let term = incompatibility.terms.first!
216-
return "\(try self.description(for: term, normalizeRange: true)) contains incompatible tools version (\(version))"
221+
return try "\(self.description(for: term, normalizeRange: true)) contains incompatible tools version (\(version))"
217222
}
218223

219224
let terms = incompatibility.terms
220225
if terms.count == 1 {
221226
let term = terms.first!
222-
let prefix = try hasEffectivelyAnyRequirement(term) ? term.node.nameForDiagnostics : self.description(for: term, normalizeRange: true)
227+
let prefix = try hasEffectivelyAnyRequirement(term) ? term.node.nameForDiagnostics : self.description(
228+
for: term,
229+
normalizeRange: true
230+
)
223231
return "\(prefix) " + (term.isPositive ? "cannot be used" : "is required")
224232
} else if terms.count == 2 {
225233
let term1 = terms.first!
@@ -233,12 +241,12 @@ struct DiagnosticReportBuilder {
233241
}
234242
}
235243

236-
let positive = try terms.filter(\.isPositive).map { try description(for: $0) }
237-
let negative = try terms.filter { !$0.isPositive }.map { try description(for: $0) }
244+
let positive = try terms.filter(\.isPositive).map { try self.description(for: $0) }
245+
let negative = try terms.filter { !$0.isPositive }.map { try self.description(for: $0) }
238246
if !positive.isEmpty, !negative.isEmpty {
239247
if positive.count == 1 {
240248
let positiveTerm = terms.first { $0.isPositive }!
241-
return "\(try self.description(for: positiveTerm, normalizeRange: true)) practically depends on \(negative.joined(separator: " or "))"
249+
return try "\(self.description(for: positiveTerm, normalizeRange: true)) practically depends on \(negative.joined(separator: " or "))"
242250
} else {
243251
return "if \(positive.joined(separator: " and ")) then \(negative.joined(separator: " or "))"
244252
}
@@ -351,8 +359,8 @@ struct DiagnosticReportBuilder {
351359
}
352360
}
353361

354-
private extension DependencyResolutionNode {
355-
var nameForDiagnostics: String {
362+
extension DependencyResolutionNode {
363+
fileprivate var nameForDiagnostics: String {
356364
"'\(self.package.identity)'"
357365
}
358366
}

Sources/PackageGraph/Resolution/PubGrub/Incompatibility.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ extension Incompatibility: CustomStringConvertible {
6363
}
6464
}
6565

66-
public extension Incompatibility {
66+
extension Incompatibility {
6767
/// Every incompatibility has a cause to explain its presence in the
6868
/// derivation graph. Only the root incompatibility uses `.root`. All other
6969
/// incompatibilities are either obtained from dependency constraints,
@@ -89,7 +89,7 @@ public extension Incompatibility {
8989
/// │{root 1.0.0}│
9090
/// └────────────┘
9191
/// ```
92-
indirect enum Cause: Equatable, Hashable {
92+
public indirect enum Cause: Equatable, Hashable {
9393
public struct ConflictCause: Hashable {
9494
public let conflict: Incompatibility
9595
public let other: Incompatibility
@@ -110,7 +110,10 @@ public extension Incompatibility {
110110
case noAvailableVersion
111111

112112
/// A version-based dependency contains unversioned-based dependency.
113-
case versionBasedDependencyContainsUnversionedDependency(versionedDependency: PackageReference, unversionedDependency: PackageReference)
113+
case versionBasedDependencyContainsUnversionedDependency(
114+
versionedDependency: PackageReference,
115+
unversionedDependency: PackageReference
116+
)
114117

115118
/// The package's tools version is incompatible.
116119
case incompatibleToolsVersion(ToolsVersion)

Sources/PackageGraph/Resolution/PubGrub/PartialSolution.swift

+21-21
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import OrderedCollections
1616
import struct TSCUtility.Version
1717

1818
/// The partial solution is a constantly updated solution used throughout the
19-
/// dependency resolution process, tracking know assignments.
19+
/// dependency resolution process, tracking known assignments.
2020
public struct PartialSolution {
2121
var root: DependencyResolutionNode?
2222

@@ -37,37 +37,37 @@ public struct PartialSolution {
3737

3838
/// The current decision level.
3939
public var decisionLevel: Int {
40-
decisions.count - 1
40+
self.decisions.count - 1
4141
}
4242

4343
public init(assignments: [Assignment] = []) {
4444
self.assignments = assignments
4545
for assignment in assignments {
46-
register(assignment)
46+
self.register(assignment)
4747
}
4848
}
4949

5050
/// A list of all packages that have been assigned, but are not yet satisfied.
5151
public var undecided: [Term] {
52-
_positive.values.filter { !decisions.keys.contains($0.node) }
52+
self._positive.values.filter { !self.decisions.keys.contains($0.node) }
5353
}
5454

5555
/// Create a new derivation assignment and add it to the partial solution's
5656
/// list of known assignments.
5757
public mutating func derive(_ term: Term, cause: Incompatibility) {
5858
let derivation = Assignment.derivation(term, cause: cause, decisionLevel: self.decisionLevel)
5959
self.assignments.append(derivation)
60-
register(derivation)
60+
self.register(derivation)
6161
}
6262

6363
/// Create a new decision assignment and add it to the partial solution's
6464
/// list of known assignments.
6565
public mutating func decide(_ node: DependencyResolutionNode, at version: Version) {
66-
decisions[node] = version
66+
self.decisions[node] = version
6767
let term = Term(node, .exact(version))
68-
let decision = Assignment.decision(term, decisionLevel: decisionLevel)
68+
let decision = Assignment.decision(term, decisionLevel: self.decisionLevel)
6969
self.assignments.append(decision)
70-
register(decision)
70+
self.register(decision)
7171
}
7272

7373
/// Populates the _positive and _negative properties with the assignment.
@@ -76,17 +76,17 @@ public struct PartialSolution {
7676
let pkg = term.node
7777

7878
if let positive = _positive[pkg] {
79-
_positive[term.node] = positive.intersect(with: term)
79+
self._positive[term.node] = positive.intersect(with: term)
8080
return
8181
}
8282

83-
let newTerm = _negative[pkg].flatMap { term.intersect(with: $0) } ?? term
83+
let newTerm = self._negative[pkg].flatMap { term.intersect(with: $0) } ?? term
8484

8585
if newTerm.isPositive {
86-
_negative[pkg] = nil
87-
_positive[pkg] = newTerm
86+
self._negative[pkg] = nil
87+
self._positive[pkg] = newTerm
8888
} else {
89-
_negative[pkg] = newTerm
89+
self._negative[pkg] = newTerm
9090
}
9191
}
9292

@@ -95,7 +95,7 @@ public struct PartialSolution {
9595
public func satisfier(for term: Term) throws -> Assignment {
9696
var assignedTerm: Term?
9797

98-
for assignment in assignments {
98+
for assignment in self.assignments {
9999
guard assignment.term.node == term.node else {
100100
continue
101101
}
@@ -114,24 +114,24 @@ public struct PartialSolution {
114114
public mutating func backtrack(toDecisionLevel decisionLevel: Int) {
115115
var toBeRemoved: [(Int, Assignment)] = []
116116

117-
for (idx, assignment) in zip(0..., assignments) {
117+
for (idx, assignment) in zip(0..., self.assignments) {
118118
if assignment.decisionLevel > decisionLevel {
119119
toBeRemoved.append((idx, assignment))
120120
}
121121
}
122122

123123
for (idx, remove) in toBeRemoved.reversed() {
124-
let assignment = assignments.remove(at: idx)
124+
let assignment = self.assignments.remove(at: idx)
125125
if assignment.isDecision {
126-
decisions.removeValue(forKey: remove.term.node)
126+
self.decisions.removeValue(forKey: remove.term.node)
127127
}
128128
}
129129

130130
// FIXME: We can optimize this by recomputing only the removed things.
131-
_negative.removeAll()
132-
_positive.removeAll()
133-
for assignment in assignments {
134-
register(assignment)
131+
self._negative.removeAll()
132+
self._positive.removeAll()
133+
for assignment in self.assignments {
134+
self.register(assignment)
135135
}
136136
}
137137

0 commit comments

Comments
 (0)